Programmatically sending a message to a bot in Mic

2020-01-29 03:18发布

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. 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. But I want to mention bot (e.g. @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. Your suggestions are welcome.

2条回答
淡お忘
2楼-- · 2020-01-29 03:36

To all Future Visitors, Microsoft Graph API (Beta) now provides a way to send message and mention the bot/user using following endpoint:

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

Method: 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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-01-29 03:38

Basically you want to message the user directly at a specific point in time (like 24 hours later). 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. 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 (e.g. Azure Website) but then have an Azure Function that sends the messages, for example, 24 hours later. 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).

In your "sending" application, you're going to need to create an instance of Microsoft. Bot.Connector.ConnectorClient, like follows:

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

You also need to "trust" the service url you're calling, like this:

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:

 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.

Hope that helps

查看更多
登录 后发表回答