5

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.

Hilton Giesenow
  • 9,809
  • 2
  • 10
  • 24
Muhammad Murad Haider
  • 1,357
  • 2
  • 18
  • 34
  • 1
    Do you want to initiate conversation with the user? To do this you need to store [user's information](https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/send-proactive-messages#obtain-necessary-user-information) and then use it later to initiate conversation. You could also try [Graph APIs](https://learn.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-beta&tabs=http) to send messages in channel. – Wajeed Shaikh Dec 17 '19 at 13:48
  • Initiating the conversation with the user is not really an issue, but i want to initiate conversation after a specific interval, e.g. after every 24 hours. So, i am finding ways to trigger the bot. – Muhammad Murad Haider Dec 17 '19 at 13:51
  • 1
    Please take a look at this [Sending Proactive Message](https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-proactive-message?view=azure-bot-service-4.0&tabs=csharp#send-proactive-message) documentation to externally trigger bot message. – Wajeed Shaikh Dec 17 '19 at 14:18
  • 1
    sorry, just checked the link, that seems to be all about storing conversation reference and then continuing conversation using conversation reference provided to the adapter. what i am searching for is to make bot send a sort of welcome message daily e.g. at 7 AM to all team members. Is this achievable? – Muhammad Murad Haider Dec 18 '19 at 04:17
  • 1
    Finally microsoft has added a documentation regarding the same https://learn.microsoft.com/en-us/microsoftteams/platform/sbs-send-proactive. Please have a look to get practical understanding of how proactive messages work – Harkirat Saluja Oct 04 '21 at 07:53
  • Hey @HarkiratSaluja, In this link they are triggering the welcome message by requesting using url in browser . I want the same behaviour but the user won't be opening the browser and pasting the url, I want them to just see the message. Any suggestions on how I can achieve this programmtically ( I mean the url opening in the browser be coded rather than being done manually every time ) ? – catBuddy Feb 26 '23 at 03:25

2 Answers2

8

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

Hilton Giesenow
  • 9,809
  • 2
  • 10
  • 24
  • 1
    Thanks for such detailed answer @Giesenow. My bot is deployed in Azure app service like normal bots and have that app id and password, In my scenario, that's basically a Microsoft Team's team, where a user can be a new member and might not have interacted with the bot ever before. So, Azure function won't be having any conversation reference, to, from information but just need to fetch team information daily (member id, name etc) and send message to all members, any insights on this that how an Azure function can be designed for this? – Muhammad Murad Haider Dec 23 '19 at 07:10
  • 2
    Ahh, ok. What I posted above is to send the message itself in 24 hours, but you need 1st the conversation id (conversation type is "personal" for direct msgs) because your bot has never had a chat with the user. A bot can't just start a chat with another user, it needs permission. I've not done this before, but you need to get the user info in the channel, like [this](https://doumer.me/get-user-info-with-your-chat-bot-in-microsoft-teams/) and then actually "install" the bot for the user programmatically like [C# Sample](https://docs.microsoft.com/en-us/graph/teams-proactive-messaging#c-sample) – Hilton Giesenow Dec 23 '19 at 12:10
  • I haven't tested the "C# Sample" yet, so just a note, the "AppId" they use could be either the Bot's AppId OR, most likely, the TEAMS App Id (the one in the manifest file, or listed in your app settings if you're using App Studio. – Hilton Giesenow Dec 23 '19 at 12:19
  • I've been playing with this more out of curiosity, and I'm actually struggling to create a conversation id, using the 'c# sample', as well as some other options. I'm curious if anyone has actually managed to get this to work? – Hilton Giesenow Dec 24 '19 at 17:14
  • Great comment, @HiltonGiesenow. It seems you're more familiar with bot integration on Teams :) I just wanted to draw your attention to the link you posted for the "C# Smaple". The link is returning a 404 page not found. Please, if possible, update it. I would love to check it out. – Bloggrammer Feb 24 '21 at 11:46
  • Hi, if you download for instance "JIRA Cloud" app in microsoft teams, you will see that it defaults to the chat tab and you see a welcome message already. This is without any user interaction so basically on first launch you see a welcome message. I think @HiltonGiesenow mentions about first user interaction unlike my case. – catBuddy Feb 26 '23 at 03:10
3

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

Muhammad Murad Haider
  • 1,357
  • 2
  • 18
  • 34
  • above is not a valid JSON body. what headers are needed? plz provide more details as I am trying to do the same thing. – Moblize IT Oct 22 '20 at 06:10
  • yes, JSON body could be invalid, i think its missing an opening brace. Anyway you can use online JSON validators. For headers, you need to provide authorization headers containing access token with appropriate permissions. For the sake of POC you can try this endpoint in graph explorer – Muhammad Murad Haider Nov 20 '20 at 07:26
  • FYI for anyone who is confused about this - You can only send the message as a user. You can't send a message as a bot using graph. Sending messages as a bot has to go through the bot framework API. – Anthony Feb 06 '23 at 17:46