使用 Microsoft Graph API C# 将聊天消息发送到 microsoft Teams 频道 [英] Sending a chat message to a microsoft Teams channel using Microsoft Graph API C#

查看:77
本文介绍了使用 Microsoft Graph API C# 将聊天消息发送到 microsoft Teams 频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已阅读文档并一直在尝试遵循此示例:

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

var graphServiceClient = MicrosoftGraphService.GetGraphServiceClient();var chatMessage = 新的 ChatMessage{主题=空,正文 = 新的 ItemBody{ContentType = BodyType.Text,内容 = 消息文本}};var response = await graphServiceClient.Teams["77f9c17f-54ca-4275-82d4-fff7esdacda1"].Channels["2007765c-8185-4cc7-8064-fb1b10f27e6b"].Messages.Request().AddAsync(chatMessage);

我也试着看看我能不能从团队那里得到什么:

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

再次,我得到的只是未知错误,我以前使用过 GRAPH API 来做一些事情,比如在组织中获取用户,所以我知道类型设置是正确的.

有没有人在世界某个地方的互联网上让这个工作?!因为它把我逼疯了

解决方案

同样的问题:

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

所有 ID 均正确且经过检查

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

  • 阅读所有用户的团队合作活动供稿
  • 阅读所有组
  • 向任何用户发送团队合作活动
  • 获取所有团队的列表

这是我的代码(基于 microsoft daemon app 场景)

访问令牌没问题

var graphClient = new GraphServiceClient("https://graph.microsoft.com/beta",新的 DelegateAuthenticationProvider(async (requestMessage) =>{requestMessage.Headers.Authorization =new AuthenticationHeaderValue("Bearer", result.AccessToken);}));var chatMessage = 新的 ChatMessage{主题 = "测试消息",正文 = 新的 ItemBody{ContentType = BodyType.Html,Content = "继续测试"}};等待 graphClient.Teams["218a4b1d-84d5-48a2-97a0-023e4e4c3e85"].Channels["19:adbf8ddf37a049aa9f63a0f8ee0e8054@thread.tacv2"].Messages.要求().AddAsync(chatMessage);

结果:

获得令牌代码:未知错误内部错误:附加数据:请求 ID:e2e433d8-cedd-4401-b5b2-6f34cf5611cf日期:2020-03-30T12:14:15ClientRequestId:e2e433d8-cedd-4401-b5b2-6f34cf5611cf

编辑(2020-04-01):

暂时没有解决方案:页面底部有评论的答案在频道中创建 chatMessage"(反馈部分)

目前看来,应用程序无法获得发送聊天消息的权限.

<块引用>

RamjotSingh 评论于 2019 年 6 月 11 日的贡献者

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

<小时><块引用>

RamjotSingh 评论于 2019 年 12 月 16 日的贡献者

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

<小时><块引用>

RamjotSingh 一天前发表评论 贡献者

一旦我们有应用程序,我们将在 Microsoft Graph 博客上分享此 API 的权限.由于这个问题的原始问题被回答了.关闭它.

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

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, 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();

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

解决方案

Same problem here :

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

All IDs are correct and checked

Here are the authorizations I have set for the app :

  • Read all users' teamwork activity feed
  • Read all groups
  • Send a teamwork activity to any user
  • Get a list of all teams

Here is my code (based on microsoft daemon app scenario)

The access token is ok

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);

And the result :

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

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)

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

RamjotSingh commented on Jun 11, 2019 Contributor

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


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 commented a day ago Contributor

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.

这篇关于使用 Microsoft Graph API C# 将聊天消息发送到 microsoft Teams 频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆