通过REST在Teams频道中开始新对话

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

Starting a new conversation in a Teams channel via REST

问题

根据我所了解,与机器人进行交互需要使用两个必要的API:

  • Graph - 访问Microsoft Graph端点,但不提供对Teams中对话的访问权限。
  • Bot Framework - 该API中的端点提供对对话的访问权限。

除了找到机器人用户ID的困难之外,问题出在机器人在频道中创建新对话。这个链接表示应该是可能的,但经过数小时的尝试后,我并没有取得成功。

文档中的服务URL与我的请求不匹配。它说 https://smba.trafficmanager.net/teams/,但我在其他地方看到 https://smba.trafficmanager.net/amer/https://smba.trafficmanager.net/apis/。究竟应该使用哪一个?

问题的关键在于JSON数据中所需的字段。这个链接旨在帮助创建频道对话,但并没有解释数据。在那一部分有以下评论:

或者,您可以使用REST API并向/conversations资源发出POST请求。

这会导致到这里。不幸的是,该页面并没有解释它是如何工作的,因为示例JSON数据是不完整的,并且不是针对频道的。它似乎只与新的群组聊天相关。

最终,我请求一个示例JSON请求,用于 POST /v3/conversations 端点,它将创建一个新的团队中的对话(清楚地说,conversationType = channel)。这是一个用于新的群组聊天的外观,但在频道中不起作用:

{
    "bot": {
        "id": "12345678",
        "name": "机器人的名称"
    },
    "isGroup": false,
    "members": [
        {
            "id": "1234abcd",
            "name": "接收者的名称"
        }
    ],
    "topicName": "新闻提醒"
}
英文:

As far as I know, there are two APIs that are required to interact with bots

  • Graph - Access to Microsoft Graph endpoints, which do not provide access to conversations in Teams.
  • Bot Framework - The endpoints in this API provide access to conversations.

Moving past the difficulty of finding bot-user IDs, the problem becomes the creation of a new conversation in a channel by the bot. This link says it should be possible, but I have not been successful after hours of attempts.

The service URL in the documentation does not match my requests. It says https://smba.trafficmanager.net/teams/, but I see https://smba.trafficmanager.net/amer/ and https://smba.trafficmanager.net/apis/ in other places. Which one is it?

The crux of the issue is the required fields in the JSON data. This link is meant to help with the creation of channel conversations, but it does not explain the data. In that section is the following comment:
> Alternatively, you can use the REST API and issue a POST request to /conversations resource.

which leads to here. Unfortunately, that page does not explain how this works because the sample JSON data is incomplete and not targeted at channels. It appears to be related only to new group chats.

Ultimately, I am requesting an example JSON request to the POST /v3/conversations endpoint, which will create a new conversation in a Team (conversationType = channel, to be clear). This is how it is supposed to look for a new group chat, which does not work with a channel:

{
    "bot": {
        "id": "12345678",
        "name": "bot's name"
    },
    "isGroup": false,
    "members": [
        {
            "id": "1234abcd",
            "name": "recipient's name"
        }
    ],
    "topicName": "News Alert"
}

答案1

得分: 1

你正在尝试做的是称为“主动消息”的操作 - 基本上,它是让你的机器人创建系列中的第一条消息,而不是在通常情况下成为“接收方”。这绝对需要一个机器人来实现,但听起来你已经有一个,所以很好。实际上,在 Teams 的环境中,你从来没有真正启动一个与机器人的“新”对话,它只是“继续”现有的对话,所以你想要发布到该对话的终端点。我在一些其他答案中已经涵盖了这一点(我希望做得不错),所以请参考这里获取更多信息:https://stackoverflow.com/questions/71547766/sending-proactive-messages-from-an-outside-process-to-organizational-users-via-t/71550069#71550069。如果你仍然遇到问题,请告诉我,我会尽力提供进一步帮助。

英文:

What you're trying to do is called "Proactive Messaging" - basically, it's having your bot create the first message in a series, rather than being the 'recipient' in the usual case. You definitely will need a bot for this, but it sounds like you have one already so that's good. In essence, in a Teams context, you're never really starting a 'new' conversation with the bot, it's just 'continuing' an existing one, so you're wanting to post to the endpoint of that converation. I've covered (I hope well) in some other answers, so please refer here for more: https://stackoverflow.com/questions/71547766/sending-proactive-messages-from-an-outside-process-to-organizational-users-via-t/71550069#71550069 . If you're still stuck, let me know and I'll try help further.

答案2

得分: 0

以下是已翻译的内容:

我成功地获取了一个JSON对象来创建一个对话。顺便说一下,我已经在这上面忙了好几天。

以下的JSON对象将在一个团队的"General"频道中创建一个新的对话:

{
    "activity": {
        "type": "message",
        "text": "got here"
    },
    "bot": {
        "id": "{{botID}}",
        "name": "{{botName}}"
    },
    "channelData":{
      "teamsChannelId":"{{teamID}}",
      "teamsTeamId":"{{teamID}}",

      // "General"频道的频道ID就是团队ID
      // 如果你想要消息发送到其他地方,请使用另一个频道ID
      "channel":{"id":"{{teamID}}"},

      "team":{"id":"{{teamID}}"},
      "tenant":{"id": "{{tenantID}"}
    },
    "isGroup": true,
    "tenantId": "{{tenantID}}"
}

以下是回复的方式:

POST {{urlBase}}/v3/conversations/{{conversationId}}/activities

{
    "bot": {
        "id": "{{botID}}",
        "name": "{{botName}}"
    },
    "text": "reply test",
    "type": "message"
}

其中 urlBase 大致是 https://smba.trafficmanager.net/amer,而 conversationId 大致是 19:c25ae532345659b67313c54e1310fdb6@thread.tacv2;messageid=2456564642569

你可以在这里找到大量有用的信息。该链接有点隐藏和晦涩,但其中包含了你可能需要的大部分低级调用信息。

不幸的是,似乎无法添加对话的标题/主题,这完全破坏了我的用例。所有的工作对我来说都是徒劳的,因为最简单的部分丢失了,但至少我弄清楚了。希望这对某人有帮助。

英文:

I managed to get a JSON object to create a conversation. I have been at this for days, by the way.

The following JSON object will create a new conversation in the General channel of a Team:

{
    "activity": {
        "type": "message",
        "text": "got here"
    },
    "bot": {
        "id": "{{botID}}",
        "name": "{{botName}}"
    },
    "channelData":{
      "teamsChannelId":"{{teamID}}",
      "teamsTeamId":"{{teamID}}",

      // The channel ID of the General channel is the team ID
      // Use another channel ID here if you want the message to go elsewhere
      "channel":{"id":"{{teamID}}"},

      "team":{"id":"{{teamID}}"},
      "tenant":{"id": "{{tenantID}}"}
    },
    "isGroup": true,
    "tenantId": "{{tenantID}}"
}

Here is the way to reply:

POST {{urlBase}}/v3/conversations/{{conversationId}}/activities

{
    "bot": {
        "id": "{{botID}}",
        "name": "{{botName}}"
    },
    "text": "reply test",
    "type": "message"
}

where urlBase is something like https://smba.trafficmanager.net/amer
and conversationId is something like 19:c25ae532345659b67313c54e1310fdb6@thread.tacv2;messageid=2456564642569
.

A ton of helpful information can be found here. That link is buried and completely obscure, but it has most of what you'll want for low-level calls.

Unfortunately, adding a title/subject to the conversation seems to be impossible, which completely breaks my use case. All that work yielded nothing for me because the easiest part is missing, but at least I figured it out. I hope this helps someone.

huangapple
  • 本文由 发表于 2023年2月7日 00:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364322.html
匿名

发表评论

匿名网友

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

确定