使用Microsoft Graph API C#将聊天消息发送到Microsoft Teams频道

问题描述:

我的目标很简单. 我想使用图形API将自动聊天消息发送到MS Teams频道.

My goal is simple. I want to send an automated chat message in to a MS Teams channel using the graph API.

这似乎是图形API的beta功能,仅在Microsoft.Graph.Beta中可用.

This seems to be beta feature of the graph API and is only avalible in the Microsoft.Graph.Beta.

我已经阅读了文档,并一直在尝试遵循以下示例:

I have read the docs and have been trying to follow this example:

https://docs.microsoft.com/en -us/graph/api/channel-post-messages ,我在我的Azure门户中设置了所有正确的权限.我不断收到我尝试过的未知错误":

https://docs.microsoft.com/en-us/graph/api/channel-post-messages, I have all the permissions set correct in my azure portal. I keep getting 'Unknown Error' I have tried:

var graphServiceClient = MicrosoftGraphService.GetGraphServiceClient();

            var chatMessage = new ChatMessage
            {
                Subject = null,

                Body = new ItemBody
                {
                    ContentType = BodyType.Text,
                    Content = messageText
                }

            };

            var response = await graphServiceClient.Teams["77f9c17f-54ca-4275-82d4-fff7esdacda1"].Channels["2007765c-8185-4cc7-8064-fb1b10f27e6b"].Messages.Request()
               .AddAsync(chatMessage);

我还试图查看我是否可以从团队中得到任何东西:

I have also tried to to see if I can get anything from teams:

var teams = await graphServiceClient.Teams["77f9c17f-54ca-4275-2sed4-ffsde59acda1"].Request().GetAsync();

我再次得到的是未知错误,我之前使用过GRAPH API来执行类似的操作,例如在组织中获取用户,所以我知道通用的设置是正确的.

Again all I get is Unknown error, I have used GRAPH API before to do things like get users in an organisation, so I know the genreal set up is correct.

世界上任何地方的互联网上的任何人都可以使用它吗?因为它让我发疯了

Has anyone on the Internet somewhere in the world got this to work?! becuase its driving me crazy

此处存在相同问题:

用户或组一切正常,但我无法从团队获得任何信息(unknownError)

Everything is ok with users or groups, but I can't get anything from Teams (unknownError)

所有ID均正确无误

这是我为该应用设置的授权:

Here are the authorizations I have set for the app :

  • 阅读所有用户的团队合作活动供稿
  • 阅读所有组
  • 将团队合作活动发送给任何用户
  • 获取所有团队的名单

这是我的代码(基于 microsoft daemon应用程序场景)

访问令牌没问题

var graphClient = new GraphServiceClient(
    "https://graph.microsoft.com/beta",
    new DelegateAuthenticationProvider(async (requestMessage) =>
    {
        requestMessage.Headers.Authorization =
            new AuthenticationHeaderValue("Bearer", result.AccessToken);
    }));

var chatMessage = new ChatMessage
{
    Subject = "Message de test",
    Body = new ItemBody
    {
        ContentType = BodyType.Html,
        Content = "Contenu de test"
    }
};

await graphClient.Teams["218a4b1d-84d5-48a2-97a0-023e4e4c3e85"].Channels["19:adbf8ddf37a049aa9f63a0f8ee0e8054@thread.tacv2"].Messages
    .Request()
    .AddAsync(chatMessage);

结果:

Token acquired
Code: UnknownError
Inner error:
    AdditionalData:
    request-id: e2e433d8-cedd-4401-b5b2-6f34cf5611cf
    date: 2020-03-30T12:14:15
ClientRequestId: e2e433d8-cedd-4401-b5b2-6f34cf5611cf

编辑(2020-04-01):

Edit(2020-04-01) :

目前尚无解决方案:页面底部(反馈部分)

No solution at the time being : there are answers to comments at the bottom of the page "Create chatMessage in a channel" in ms doc (feedback section)

到目前为止,似乎无法授予应用程序发送chatMessage的权限.

It seems that applications cannot be granted the permission to send chatMessages up to now.

RamjotSingh于2019年6月11日发表了评论

RamjotSingh commented on Jun 11, 2019 Contributor

@ pythonpsycho1337-如上面的权限表所述,应用程序 目前,此API仅不支持上下文.

@pythonpsycho1337 - As the permission table above notes, Application only context is not supported on this API at the moment.


RamjotSingh于2019年12月16日发表了评论

RamjotSingh commented on Dec 16, 2019 Contributor

支持应用程序权限是我们计划要做的事,但我们还没有日期.

Supporting application permissions is something we plan to do but we do not have a date yet.


RamjotSingh在一天前发表了评论

RamjotSingh commented a day ago Contributor

获得申请后,我们将在Microsoft Graph Blog上共享 此API的权限.由于这个问题的原始问题 被回答了.关闭它.

We will share on Microsoft Graph Blog once we have application permissions for this API. Since the original question for this issue was answered. Closing it.