简体   繁体   English

如何主动向团队频道发送消息

[英]How to proactively send a message to a teams channel

I can't seem to figure out how to proactively message a MS teams channel using a Python Bot (botframework).我似乎无法弄清楚如何使用 Python Bot(机器人框架)主动向 MS 团队频道发送消息。

  1. An user installs my third-party MS teams bot, adding it to one of their Teams channels.用户安装我的第三方 MS 团队机器人,并将其添加到他们的团队频道之一。
  2. My Bot needs to send ad-hoc messages as part of an event from an unrelated back-end system.我的机器人需要从不相关的后端系统发送临时消息作为事件的一部分。

The botframework does not let you message channels at will, it needs a conversation reference. botframework 不会让你随意留言频道,它需要一个对话参考。 You can get a conversation reference in various ways, such as someone messaging the bot, or fetching the list of channels and constructing a conversationId from that.您可以通过多种方式获取对话参考,例如某人向机器人发送消息,或获取频道列表并从中构造一个conversationId

Reading the documentation阅读文档

The documentation will have you believe that it is in fact possible to send message at will, using the following steps: 该文档将使您相信实际上可以使用以下步骤随意发送消息:

  1. Get the user ID or team/channel ID (if needed).获取用户 ID 或团队/频道 ID(如果需要)。
  2. Create the conversation or conversation thread (if needed).创建对话或对话线程(如果需要)。
  3. Get the conversation ID.获取对话 ID。
  4. Send the message.发送消息。

For step 1, how/when do I get the channel ID if there are no events that my Bot has been added to a channel?对于第 1 步,如果没有我的机器人已添加到频道的事件,我如何/何时获取频道 ID?

For step 2, how do I create a conversation if I don't know what team channels there are?对于第 2 步,如果我不知道有哪些团队频道,如何创建对话?

Conclusion结论

Does someone know how to send a message to a MS Teams channel using a Python app/bot?有人知道如何使用 Python 应用程序/机器人向 MS Teams 频道发送消息吗? It should not require user interaction .它不应该需要用户交互 The app/bot gets added to a Teams channel, and it should immediately post a message inside this channel .应用程序/机器人被添加到 Teams 频道,它应该立即在此频道中发布一条消息

Turns out the issue was that my on_teams_members_added() was not getting called because I kept deleting the app within Teams instead of uninstalling.原来问题是我的on_teams_members_added()没有被调用,因为我一直在 Teams 中删除应用程序而不是卸载。

Make sure to:确保:

  1. Click on the... overflow menu next to the team name单击团队名称旁边的...溢出菜单
  2. Pick Manage Team选择管理团队
  3. Select the "Apps" tab Select “应用程序”选项卡
  4. Click the trash icon to remove the app from that team单击垃圾桶图标以从该团队中删除该应用程序
  5. Then try to install the app again然后尝试再次安装应用程序

With this code you can send a channel message when the Bot enters the channel:使用此代码,您可以在 Bot 进入频道时发送频道消息:

async def on_teams_members_added(  # pylint: disable=unused-argument
    self,
    teams_members_added: [TeamsChannelAccount],
    team_info: TeamInfo,
    turn_context: TurnContext,
):
    for member in teams_members_added:
        if member.id == turn_context.activity.recipient.id and team_info is not None:
            # bot entered a Teams channel
            await turn_context.send_activity("Hello channel! I have just been added.")

Your handler needs to inherit from TeamsActivityHandler .您的处理程序需要从TeamsActivityHandler继承。

I'm working on a sample for the pnp Teams samples repo on GitHub that I'm hoping to submit in the next few days.我正在为 GitHub 上的 pnp Teams 示例存储库制作一个示例,我希望在接下来的几天内提交该示例。 I haven't started on the documentation yet, but the code is fully functional, with both a C# and a Node.js version of the backend, which sends a -very- simple proactive message example (showing the most basic things you need) - hopefully it can be of use even though it's not in Python - see https://github.com/HiltonGiesenow/teams-dev-samples/tree/add-proactive-messaging-sample/samples/bot-proactive-messaging/src我还没有开始编写文档,但是代码功能齐全,有 C# 和 Node.js 版本的后端,它发送一个非常简单的主动消息示例(显示您需要的最基本的东西)-希望即使它不在 Python 中,它也可以使用 - 请参阅https://github.com/HiltonGisenow/teams-dev-samples/tree/add-proactive-messaging-sample/samples/bot-proactive-messaging/src

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将主动自适应卡发送到 MS Teams 频道 - How to send a proactive adaptive card to MS Teams Channel 如何向特定频道发送欢迎消息 - How to send a welcome message to a specific channel 如何通过 Rest API(传入 webhook)回复 Microsoft Teams 频道中的现有消息? - How to reply to a existing message in Microsoft Teams channel via Rest API(incoming webhook)? 如何在第一个文本频道discord.py中发送欢迎信息 - How to send welcome message in first text channel discord.py 如何获取频道中所有消息的列表,然后从该列表中随机选择一条消息作为消息发送? - How can I get a list of all the messages in a channel and then pick a random message from that list to send as a message? 如果频道最后一条消息是在 X 分钟前发送的,如何创建一个循环来发送消息? [不和谐.py] - How to create a loop to send a message if the channel last message was sent X minutes ago? [discord.py] 使用列表将消息随机发送到频道 - Send message to channel at random using lists 向每个服务器的系统通道发送消息 - Send a message to every server's system channel 每次具有特定角色的用户在任何频道发言时,如何让机器人发送消息 | Discord.py - How to have the bot send a message every time a user with a specific role speaks in any channel | Discord.py 如何才能将消息专门发送到用户的专用频道(DM)? - How can I send a message specifically to the user's private channel (DM)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM