简体   繁体   English

以编程方式向 Microsoft Teams 中的机器人发送消息

[英]Programmatically sending a message to a bot in Microsoft Teams

I have created a proactive bot that basically asks certain questions to a user when a user starts conversation with the bot.我创建了一个主动机器人,当用户开始与机器人对话时,它基本上会向用户提出某些问题。 The bot is deployed in Microsoft Teams environment.该机器人部署在 Microsoft Teams 环境中。 Is there any way that i can send automated message to a bot in a channel?有什么方法可以向频道中的机器人发送自动消息? I know messages can be sent using powershell by utilizing webhook url exposed by a particular team or using MS Flow.我知道可以通过使用特定团队公开的 webhook url 或使用 MS Flow 使用 powershell 发送消息。 But I want to mention bot (eg @mybothandle) in the message so the bot starts asking questions by itself than requiring the user to start the conversation (by mentioning the bot manually) but not finding the way to mention.但是我想在消息中提及机器人(例如@mybothandle),以便机器人开始自行提问,而不是要求用户开始对话(通过手动提及机器人)但找不到提及的方式 Your suggestions are welcome.欢迎您提出建议。

Basically you want to message the user directly at a specific point in time (like 24 hours later).基本上,您希望在特定时间点(例如 24 小时后)直接向用户发送消息。 I'm doing this in a few different bots, so it's definitely possible.我正在几个不同的机器人中这样做,所以这绝对是可能的。 The link that Wajeed has sent in the comment to your question is exactly what you need - when the user interacts with your bot, you need to save important information like the conversation id, conversation type, service url, and To and From info. Wajeed 在评论中针对您的问题发送的链接正是您所需要的 - 当用户与您的机器人交互时,您需要保存重要信息,例如对话 ID、对话类型、服务 URL 以及收件人和发件人信息。 You can store this, for instance, in a database, and then you can actually have a totally separate application make the call AS IF IT WAS your bot.例如,您可以将其存储在数据库中,然后您实际上可以让一个完全独立的应用程序进行调用,就好像它是您的机器人一样。 In my bots, for example, I have the bot hosted in a normal host (eg Azure Website) but then have an Azure Function that sends the messages, for example, 24 hours later.例如,在我的机器人中,我将机器人托管在普通主机(例如 Azure 网站)中,然后有一个 Azure 函数来发送消息,例如,24 小时后。 It just appears to the user as if it was a message from the bot, like normal.它只是在用户看来就像是来自机器人的消息,就像正常一样。

You will also need the Microsoft App ID and App Password for your bot, which you should have already (if not, it's in the Azure portal).你还需要你的机器人的 Microsoft 应用 ID 和应用密码,你应该已经有了(如果没有,它在 Azure 门户中)。

In your "sending" application, you're going to need to create an instance of Microsoft.在您的“发送”应用程序中,您将需要创建一个 Microsoft 实例。 Bot.Connector.ConnectorClient, like follows: Bot.Connector.ConnectorClient,如下所示:

var Connector = new ConnectorClient(serviceUrl, microsoftAppId: credentialProvider.AppId, microsoftAppPassword: credentialProvider.Password);

You also need to "trust" the service url you're calling, like this:您还需要“信任”您正在调用的服务 url,如下所示:

MicrosoftAppCredentials.TrustServiceUrl(serviceURL);

Then you create an instance of Microsoft.Bot.Schema.Activity, set the required properties, and send it via the connector you created:然后你创建一个 Microsoft.Bot.Schema.Activity 的实例,设置所需的属性,并通过你创建的连接器发送它:

 var activity = Activity.CreateMessageActivity();

 activity.From = new ChannelAccount([FromId], [FromName];
 activity.Recipient = new ChannelAccount([ToId], [ToName]);
 activity.Conversation = new ConversationAccount(false, [ConversationType], [ConversationId]);
 activity.Conversation.Id = [ConversationId];

 activity.Text = "whatever you want to send from the bot...";

 Connector.Conversations.SendToConversationAsync((activity as Activity)).Wait();

All the items in square braces are what you get from the initial conversation the user is having with the bot, except that the From and To are switched around (when the user sends your bot a message, the user is the FROM and your Bot is the TO, and when the bot is sending you switch them around.方括号中的所有项目都是您从用户与机器人的初始对话中获得的内容,除了 From 和 To 被切换(当用户向您的机器人发送消息时,用户是 FROM 而您的机器人是TO,当机器人发送时,您可以切换它们。

Hope that helps希望有帮助

To all Future Visitors, Microsoft Graph API (Beta) now provides a way to send message and mention the bot/user using following endpoint:对于所有未来访问者,Microsoft Graph API(测试版)现在提供了一种使用以下端点发送消息和提及机器人/用户的方法:

 https://graph.microsoft.com/beta/teams/{group-id-for-teams}/channels/{channel-id}/messages

Method: POST方法: POST

Body :身体

"body": {
    "contentType": "html",
    "content": "Hello World <at id=\"0\">standupbot</at>"
  },
  "mentions": [
    {
      "id": 0,
      "mentionText": "StandupBot",
      "mentioned": {
        "application": {
                            "id": "[my-bot-id]",
                            "displayName": "StandupBot",
                            "applicationIdentityType": "bot"
                        }
      }
    }
  ]
}

However, there is a bug that bot doesn't respond when receives the message: Bot is not responding to @Mention when sending message using Graph API但是,有一个 bot 在收到消息时没有响应的错误: Bot is not respond to @Mention when sent message using Graph API

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM