This package will help you to send a message to a channel in Microsoft Teams.
The first step you need to do is create an Incoming WebHook in your Teams channel. Just open a teams channel and click on the .. button on top right corner and select Connectors.
On the new screen, search for Incoming WebHooks and click on Add. Copy the generated URL.
To install the package, run the following command in the root of your project:
dotnet add package Teams.WebHooks --version 1.0.0
Moving to your code, first we`ll need to create a card like this:
var card = new Message();
card.ThemeColor = "8625D2";
card.Summary = "Some summary here";
A card can be composed of many sections and we need at least one section to send a message.
card.Sections.Add(new Section
{
ActivityTitle = "Section Title here",
ActivitySubtitle = "Section Subtitle here",
ActivityImage = "https://some-image.png",
Markdown = true,
});
You can have as many facts as you want, they will be shown in a separeted place of the card.
card.Sections.Add(new Section
{
ActivityTitle = "Section Title here",
ActivitySubtitle = "Section Subtitle here",
ActivityImage = "https://some-image.png",
Markdown = true,
Facts = new List<Fact>()
{
new()
{
Name = "Assigned to",
Value = "some@email.com"
},
new()
{
Name = "Due date",
Value = "27/07/2021"
},
new()
{
Name = "Status",
Value = "Active"
}
},
});
Cards can have potential actions, which are actions that can be taken when clicking on the buttons.
card.PotentialAction.Add(new PotentialAction
{
Name = "Comentar",
Inputs = new List<Input>()
{
new() {Id = "email", Title = "Email here", Type = "TextInput", IsMultiline = false},
new() {Id = "date", Title = "Date", Type = "DateInput", IsMultiline = false},
new InputMultiChoice()
{
Id = "mult", Title = "Data", Type = "DateInput", IsMultiline = false, Choices =
new List<InputMultiChoiceItem>()
{
new() {Display = "item 1", Value = "1"},
new() {Display = "item 2", Value = "2"},
}
},
},
Actions = new List<ActionItem>()
{
new()
{
Body = "comment={{email.value}}", // Capture the value of the input
Name = "Aprove",
Target = "https://your-api-endpoint"
}
}
});
var result = await MessageClient.SendAsync(YOUR_WEBHOOK_URL, card);
Console.WriteLine(result);