SnowStorm
SnowStorm

Reputation: 385

How to send simple message to Microsoft Teams channel using csharp?

I can't find anything to send simple message to MS Teams channels, I'm using Csharp and don't need reply to messages.

Upvotes: 3

Views: 17260

Answers (4)

To add to the above, MessageCardModel on Github/NuGet has a model for sending adaptive cards to MS Teams

There's a playground at MessageCardPlayground which shows how the card formats when rendered in teams.

Simple creating a model like this and serializing to Json and posting to a WebHook channel results in some nice output.

// Create a variable like below
var card = new MessageCard
{
    Title = "This is the title",
    Text = "Message text",
    Sections = new[]
    {
        new Section
        {
            Text = "Section title",
            Actions = new[]
            {
                new OpenUriAction
                {
                    Name = "Open My Blog",
                    Type = ActionType.OpenUri,
                    Targets = new List<Target>
                    {
                        new Target
                        {
                            OS = TargetOs.Default,
                            Uri = "http://blog.poychang.net/"
                        }
                    }
                }
            }
        }
    }
};
// Now you get Json string to send or play in https://messagecardplayground.azurewebsites.net
var json = card.ToJson();

Full example of usage at this repo Logging.MSTeams

Upvotes: 1

KaranSingh
KaranSingh

Reputation: 620

Using C#, one way to send messages to teams channel is using webhook url. Below code will help sending message to teams.

string webhookUrl = "<enter Teams webhook url>";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(webhookUrl);
Message body = new Message();
body.text = "Hello World";
string serializeJson = JsonConvert.SerializeObject(body);
StringContent content = new StringContent(serializeJson,Encoding.UTF8,"application/json");
_ = await client.PostAsync(client.BaseAddress, content);

Declare a Message class

public class Message
{
    public string text { get; set; }
}

Webhook url can be set up by creating an Incoming webhook connection on desired Teams channel: enter image description here

Upvotes: 18

Trinetra-MSFT
Trinetra-MSFT

Reputation: 1015

You can send messages to Teams channel using webhook. You can post messages by setting up incoming webhook within channel. Please look at Post external request in Teams with incoming webhook. But you should specify your requirement you can achieve this with the Bot. A bot is also helpful to have conversation within Teams channel. Bot supported [Personal, Team, GroupChat] scope. Please go through the Conversation basic for more information how bot works within different scopes.

Upvotes: 2

Hilton Giesenow
Hilton Giesenow

Reputation: 10804

There are a few ways to accomplish this, depending on what your other broader requirements might be. Options are, for instance, webhooks or a bot, but please see my answer at Microsoft Teams: Is at all possible to create a app/connector/bot for broadcasting?

Upvotes: 1

Related Questions