简体   繁体   中英

Send message from Controller to Microsoft Teams

I'm trying to send some message from my API (built in .NET Core) to a teams channel. According to the documentation, I need to use this code, but I don't know how to create the authProvider Object.

Grenter code hereaphServiceClient graphClient = new GraphServiceClient( authProvider );

var chatMessage = new ChatMessage {
    Body = new ItemBody {
        Content = "Hello World"
    }
};

await graphClient.Teams["{team-id}"].Channels["{channel-id}"].Messages
    .Request()
    .AddAsync(chatMessage);

I´ve been reading the documentation but I don't undetstand what I have to do.

This is my controller

[HttpGet]
[Route("SendMessage")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> SendMessageToTeams(string message) {
    try {
        GraphServiceClient graphClient = new GraphServiceClient(authProvider);
        var chatMessage = new ChatMessage {
            Body = new ItemBody {
                Content = "Hello World"
            }
        };
        await graphClient.Teams["xxx"].Channels["xxx"].Messages
                         .Request()
                         .AddAsync(chatMessage);
        return Ok();
    }
    catch (Exception ex) {
        return BadRequest();
    }
}

Could you help me with this please.

Thanks you.

There are actually a number of ways to do this and which to choose depends on your wider application requirements. However, for the most simple way to send a message to a channel, I would suggest looking at Incoming Webhooks - see more at https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM