Sending a message to users as a bot in bot channel of Microsoft Teams using the Graph API - botframework

I have created a bot by following the steps mentioned in the doc.I have authenticated user using oauth 2.0 (auth code grant) as mentioned in the doc and in reverse I got a access token. But when I send message to channel in the teams using (/teams/{id}/channels/{id}/messages) API the message was sent on behalf of me. But I want my bot as the sender of message. Here is the image of the message that I have sent using the above API. and is there any way to send direct message to user as a bot?

Instead of using the Graph, there's another approach using the Bot Framework itself, to send a message to a team channel, a group chat, or a 1-1 conversation. The code doesn't even need to live inside the bot itself, it just needs to leverage the bot framework under the covers (for example, I have several Azure Functions that pro-actively message users). This idea is called "Proactive messaging" and you can read more about it in the docs here.
You do need to get certain fields when the user first installs the bot though, or any time the bot receives a message. I've described that more at Programmatically sending a message to a bot in Microsoft Teams. You haven't said what language you're using, but there are examples for a bunch of them - I can send you links if you let me know what you're using.

Related

Bot Framework Composer Sending Direct Messages

I have a lot of questions about getting started. I currently have a sample bot I built in Composer that works for my teams/slack channels and works in the testing Bot Framework Emulator without issue.
I am not sure how to make the bot send a direct message/private message to a user in a channel instead of it replying directly in the channel itself. Any one have any ideas of how to accomplish this?
You can send messages from the bot to any user (or channel) by using the Azure Bot Service REST API (using the Send an HTTP Request action in Composer as described here. As you'll see in the documentation, the main limitation is that the recipient must have had a previous conversation with your bot from which you've recorded the conversationID (and activityID if you want to reply to a thread).

How to send message to all the members of the group on Installing teams bot

I have created a teams bot and had a service written in .NET core to handle events and user's messages to reply accordingly.
When I install a bot in a group, I need to send personal message(one-to-one i.e between bot and the user) to all the members of that group on installation. I am trying to do that in OnConversationUpdateActivityAsync event handler (which gets fired when I install the bot). But in this event I am getting information of the user who is installing the bot, not the other members which are added in that group, also I am not getting any information of the channel(channelId and members etc.) in which the bot is getting installed.
Any different approach or solution will work.
Thanks in Advance.
You haven't said if you want the bot to message the users privately (like 1-1 between the bot and user) or just send each person a personal message inside the group chat, but in both cases, Proactive Messaging is your correct approach. If you want to send a message inside the group chat itself, see this sample.
If you want to send the users messages directly, 1-1, they need to have the bot installed as a personal app already. It's possible to do this automatically, but it's a bit more work, and requires Microsoft Graph. The proactive messaging is a bit different too - you get the list of members as per the previous sample, but see here for how to get the required 1-1 conversation details, and how to send the actual message. This last link also has documentation on how to get started, and some background reading (at the bottom of the page).
#Hilton is correct, You need to specify in which scope you want to notify user 1:1 or directly in Group chat?
App should be installed in user scope if notifying user on installation, You can proactively install the App in User/Group Chat/ team scope using Graph API. To notify users in Teams or Group chat, You can fetch the list of members using List conversation members API, When you install the App using Graph API Bot received converstionUpdate, You can save the conversationReference and use it for proactively notifying.

How to find chat conversation with bot

we are working on MS Bot Framework bot. We are integrating with directline and MS teams. In certain cases we need to download conversation history. Within directline we are using standard DL endpoints to retrieve the conversation. However these are not available for teams channel. Therefore we are trying to get chat messages using Graph API by using
/me/chats/{id}/messages
And here's our issue - to get the chat ID we are trying to iterate through chats returned by
/me/chats
And then look up conversation members by
/me/chats/{id}/members
However chat with Bot contains only user and not the bot so it appears it is a single member chat. As workaround we always sort chats by last updated timestamp descending and assuming first single member conversation is the one we are looking for however this is tricky and unreliable and also very slow some times. Is there some better way to get conversation history from MS teams channel?
Using Graph API, you can access information related to channels in Microsoft Teams. You can refer to list channel messages which retrieves the list of messages (without the replies) in a channel of a team. If you want to get the replies for the message, then you need to call list message replies or get message reply API.
GET /teams/{id}/channels/{id}/messages
One more way is to subscribe to the channel using change notifications API which makes you get all the messages of the channel to the notification endpoint.

Can user resume a conversation in a different channel? (Bot Framework)

Let's think of the following example:
1) I have a certain bot deployed on Azure
2) Bot can be talked via Facebook Messenger and via Skype
3) A certain user talks to the bot via Facebook Messenger and then he leaves.
4)A couple of minutes ago the same user resumes the conversation with the bot, but via Skype.
Is this possible? I assume Bot Framework doesn't have anything included for this, hence, that this isn't posible (as conversations are independent and state changes depending on the channel). Is there any way to identify a user (via some authentication method maybe), and then making this logic again?
Do any of you know any workaround for this?
Thanks in advance!
The Bot Framework Connector service is a component which provides a single API for your bot to communicate across multiple client services such as Skype, Email, Slack. Every bot and user has an account within each channel.
The channel account contains an identifier (id) and other informative bot non-structural data, like an optional name.
And there us unique conversation ID created for each conversation of each user for each channel. And you can customize your channel capabilities as described here.
Regards,
Jyo

Multi-Tenant Bot with Unique Channel for every customer

We are building a bot which would require each of our customers to have their own SMS channel (unique phone number for them). From everything we can tell with the documentation of the bot framework is there is no way to do that. We want each of our customers to be able to add the bot to their own SMS number through twilio. Is this possible?
From everything we can tell with the documentation of the bot framework is there is no way to do that.
Yes you're right.
We want each of our customers to be able to add the bot to their own SMS number through twilio. Is this possible?
No, actually it's not possible :)
Shortly, BotFramework consist of three parts :
your bot logic which is a Web API service that exposes an endpoint
the channels (Skype, Facebook messenger, SMS etc)
the bot connector, where you create your bot by specifing the endpoint and one or multiple diiferent channels that the bot can use for conversations.
Because each channel can be associate only one time per endpoint hence per bot then you can't do multitenancy through the bot connector.
One solution is to create the multitenancy through the Web API service side by creating an endpoint for each customer. Maintenanbility will be difficult because of multiple endpoints you'll end up with the same number of bots into the bot director to manage. Each bot wil be associate to your customer number.
Updates: (I didn't test it but I'm pretty sure it can work)
Another solution is to use DirectLine channel and its Rest API that it exposes. If you opt for this solution it means that you will need to do a lot of things to make that work correctly after creating Direct Line channel:
you must create a client application to communicate with Direct Line channel through its REST API.
each customer can have their own number that you manage through Twilio
each message that a customer send using their SMS number and received via Twilio should trigger a call to the service of your client application
your client application will start a new conversation with Direct Line channel when a SMS is received.
each conversation started by your client application must take care about the SMS number who started the conversation by saving the state. Bot data per conversation and per user can help a lot.
when you receive a reply from the Direct Line Channel, your client application read the SMS number stored into the bot data and use this number to answer to the associated customer.
Hope it helps.
You can definitely do this.
You implement ICredentialProvider which is responsible for looking up appPassword by AppId and validating that the password is correct. This means you can have a database of bot AppId/Passwords that one endpoint services, and you look up the appid/secret and validate calls.
[ BotAuthentication(CredentialProviderType=typeof(SimpleCredentialProvider))]
Here is a simple sample implementation
https://docs.botframework.com/en-us/csharp/builder/sdkreference/d3/dde/_credential_provider_8cs_source.html

Resources