发送一条Microsoft Teams聊天消息

huangapple go评论72阅读模式
英文:

Sending a chat message with Microsoft Teams

问题

有没有办法发送 Microsoft Teams 的消息到一个频道或聊天,而不需要应用的最终用户被要求登录?我尝试做的是这样的事情:

  1. 用户在我的网络应用上执行某些操作
  2. 在没有用户干预的情况下,我的网络应用发送一条消息到 Microsoft Teams 的聊天或频道

我尝试以我的身份进行测试(计划稍后使用服务帐户),但在阅读一些资料后,我发现使用应用程序 API 权限是不可能的,如此处所记录的:

发送一条Microsoft Teams聊天消息

这是我有的代码,如果我不使用应用程序权限的话,它会起作用:

var tenantId = "...";
var clientId = "...";
var clientSecret = "...";

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);

var graphClient = new GraphServiceClient(clientSecretCredential);

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

await graphClient.Teams["..."].Channels["..."].Messages.Request().AddAsync(chatMessage);

它会引发这个异常:

Microsoft.Graph.ServiceException: 'Code: Unauthorized
Message: Message POST is allowed in application-only context only for import purposes. Refer to https://docs.microsoft.com/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams for more details.
Inner error:
    AdditionalData:
    date: 2023-02-16T04:46:02
    request-id: 3cbd9dc8-a86a-43e9-a5fe-e9e9b3433566
    client-request-id: 3cbd9dc8-a86a-43e9-a5fe-e9e9b3433566
ClientRequestId: 3cbd9dc8-a86a-43e9-a5fe-e9e9b3433566

是否有可能在没有用户干预的情况下发送消息到 Teams 的聊天/频道?

英文:

Is there anyway to send a teams message to a channel or chat that doesn't involve the end user of an app being asked to sign in? What I am trying to do is something like this:

  1. User does something on my web app
  2. Without user interaction, my web app sends a message to a chat or channel on Microsoft Teams

I tried to send it as myself for testing (planned on using a service account later), but after some reading, I've come to find out isn't possible using application API permissions, as documented here:

发送一条Microsoft Teams聊天消息

Here is the code I have that would work if I wasn't using application permissions:

var tenantId = "...";
var clientId = "...";
var clientSecret = "...";

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);                        

var graphClient = new GraphServiceClient(clientSecretCredential);

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

await graphClient.Teams["..."].Channels["..."].Messages.Request().AddAsync(chatMessage);

It throws this exception:

Microsoft.Graph.ServiceException: 'Code: Unauthorized
Message: Message POST is allowed in application-only context only for import purposes. Refer to https://docs.microsoft.com/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams for more details.
Inner error:
	AdditionalData:
	date: 2023-02-16T04:46:02
	request-id: 3cbd9dc8-a86a-43e9-a5fe-e9e9b3433566
	client-request-id: 3cbd9dc8-a86a-43e9-a5fe-e9e9b3433566
ClientRequestId: 3cbd9dc8-a86a-43e9-a5fe-e9e9b3433566

Is it possible to send a message to a teams chat/channel without user interaction?

答案1

得分: 1

根据文档,应用程序权限仅支持迁移目的。为此,您的团队频道必须都处于已迁移状态。但是,您可以在委派上下文(已登录用户)中使用此图形 API。

参考文档:https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=csharp

使用主动消息,您可以在团队聊天/频道中发送消息,无需用户交互。

参考文档:https://learn.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bot-conversations/bots-conv-proactive

英文:

As per the documentation, application permissions are supported only for migration purposes. For this, your team and channel both must be created in a migrated state. However, you can use this graph API in a delegated context (with signed-in user).

Reference Document: https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=csharp

Using proactive messages you can send a message to a teams chat/channel without user interaction.
Reference Document: https://learn.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bot-conversations/bots-conv-proactive

huangapple
  • 本文由 发表于 2023年2月16日 14:18:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定