How can I turn my bot into a MS Teams app - botframework

I made a normal bot by using Microsoft Bot Framework and has deployed it to the Azure portal.
How can I possibly make it a Teams app other than channeling to Teams, for example, make it a Teams app package.
I checked some sample code on Github and noticed that general bots are a bit different from Teams bots, for example, general bots extend ActivityHandler but Teams bots extend TeamsActivityHandler. May I ask how can I turn my bot into a Teams app? Do I need to alter the code of the bot I made a lot?
Thanks

With a few exceptions, you don't really need to make changes to your bot code to deploy to Teams channel. However, I do think there are a few things you should be aware of and consider in your development. First of all, I'm going to assume you have or know how to turn on the channel from the Bot Service. Once you have done that, you can test your bot in Teams without even creating a Teams app by pasting the Microsoft App ID into the chat To: field (obviously it's not recommended to share this ID for general testing).
The main change you probably need is to remove mentions. These will mess with QnA Maker and/or LUIS as they are included in the query string. I have been doing this as the first step in the onMessage handler. My current bots use regex for this, e.g.
if (context._activity.text) ( // Make sure there is activity text before trying to replace
context._activity.text = context._activity.text.replace(/(#|<at>)((Bot Name)|(Teams App Manifest Name))(<\/at>)? ?/g, '');
}
However, I have also seen that the TurnContext object can do this via TurnContext.removeRecipientMention(context.activity); I've not actually tried that myself, though. If it works it would be very helpful in case you find yourself changing bot names as I have done in the past...
The other main change I made to my bots was creating Teams-specific adaptive cards with menu buttons. By default, Action.Submit will work for web channels but NOT Teams channel. A typical action would look like
{
"type": "ActionSet",
"actions": [
{
"type": "Action.Submit",
"title": "Get Order Status",
"data": "Get Order Status"
}
]
}
But Teams can't handle this and will error out on button click (at least when using standard Activity handler, not sure if it is the same if using TeamsActivityHandler.) Instead, you should check the channel before displaying cards with Action.Submit actions and display an alternative card instead. For example
if (context.activity.channelId == 'msteams') {
var welcomeCard = CardHelper.GetMenuCardTeams(welcomeMessage,'Y','Y');
} else {
var welcomeCard = CardHelper.GetMenuCard(welcomeMessage,'Y','Y');
}
And then your actions for Teams instead look like
{
"type": "ActionSet",
"actions": [
{
"type": "Action.Submit",
"title": "Get Order Status",
"data": {
"msteams": {
"type": "imBack",
"value": "Get Order Status"
}
}
}
]
}
I've tried combining these and it doesn't work well. You can add something to your handler to make Teams cards work in web, but the text will not be inserted into the chat like a typical button and it will instead be essentially like a backchannel event. I like this method much better.
Other than that you should be able to run your bot as-is, except for attachments as noted in your separate question. I have not gotten that to work and I believe it may be related to not using TeamsActivityHandler but I'm not sure.
Hopefully this helps. Go ahead and give it a try and you can create a new issue with any specific problems you face once the bot is operating in Teams.

Related

Hide some input messages to MS teams bot

I am building a bot that takes some sensitive inputs. As I would like to add this bot to an MS team/group, how do I hide/mask user's input to the bot from other users. Is that possible?
Currently it's not possible to send masking data to bot from compose message area. Instead you can use adaptive card to send masking data to bot.
{
"type": "Input.Text",
"id": "secretThing",
"style": "password"
}
Microsoft will always focus on customer’s feedback and experience, some new features would be added to the services based on customers' feedback in the future, we also recommend you give your new idea in Teams UserVoice here if this needs to be consider as a future request.

Welcome Message not firing for Microsoft Bot (using framework v4) hosted in LivePerson using Direct Line (NOT WebChat)

First, there has been plenty written on this subject in the context of WebChat, and there are fixes out there that show implementation. Here's a link that is pretty good:
https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/
This problem is NOT a WebChat problem, it's a Direct Line problem that uses a 3rd party named LivePerson to host the bot which lives in Azure, and is connected via Direct Line. Hence, I do not control the client code (like I would if I were writing/hosting the bot using html/JavaScript). However, the take away here is "do not use conversationUpdate", which I am using in my bot, but please read on...
I'm hosting my Microsoft v4 Bot in LivePerson using Direct Line. The bot uses Adaptive Dialogs to welcome the user and ask them a question before the user sends any input using the OnConversationUpdateActivity():
public RootDialog(IConfiguration configuration, IMiddlewareApiFacade middlewareApi, IBotTelemetryClient telemetryClient) : base(nameof(RootDialog))
{
var rootDialog = new AdaptiveDialog(nameof(RootDialog))
{
...
Triggers = new List<OnCondition>()
new OnConversationUpdateActivity()
{
Actions = WelcomeUserSteps("${Greeting()}")
}
...
}
private static List<Dialog> WelcomeUserSteps(string message)
{
return new List<Dialog>()
{
// Iterate through membersAdded list and greet user added to the conversation.
new Foreach()
{
ItemsProperty = "turn.activity.membersAdded",
Actions = new List<Dialog>()
{
// Note: Some channels send two conversation update events - one for the Bot added to the conversation and another for user.
// Filter cases where the bot itself is the recipient of the message.
new IfCondition()
{
Condition = "$foreach.value.name != turn.activity.recipient.name",
Actions = new List<Dialog>()
{
new SendActivity(message),
new BeginDialog(nameof(WelcomeDialog))
}
}
}
}
};
}
}
}
This works fine when running the bot locally using the Emulator or running the bot from Test Web Chat in Azure, but it does not work in LivePerson.
I've successfully hooked up and tested the connection to the bot from LivePerson via Direct Line:
However, when the bot is started, and it's accessed via LivePerson's chat, the welcome message does not fire (there should be a welcome message then a question from the bot where the red square is):
Looking at LivePerson's docs, they have an "The Welcome Event" section that talks about the bot greeting the users for bots configured as as "chat" (which is how this bot is configured in LivePerson)
Looking closer at how a chat is considered started for chat conversation bots, the docs state:
A chat conversation is considered started when the chat is routed to an agent. Best practice is for the agent to provide the first response. In this scenario, there is no text from the consumer to parse, thus the default ‘WELCOME’ event is utilized as a start point for the bot to prompt the user to provide input and progress the conversation. Ensure you have an ‘entry point’ in your bot that responds to the default ‘WELCOME’ action send by a new chat customer.
Then this code:
{
// ...
"type": "message",
"text": "",
"channelData": {
"action": {
"name": "WELCOME"
}
}
}
FYI: an "agent" in the context of LivePerson can mean an actual person OR a bot. Both are considered "agents", and when you add a new agent to LivePerson, one of the types available is "bot". So agent does not mean person in this example.
I'm not too sure how my bot (which uses bot framework v4 and Adaptive Dialogs) needs to be configured/implemented to have an entry point that responds to this WELCOME message.
I do know that I cannot use conversationUpdate (or in adaptive dialog speak, OnConversationUpdateActivity()), but I'm not too sure which adaptive dialog (or otherwise) I need to use somehow intercept the json WELCOME message to sent to my bot by LivePerson... OnEventActivity()? OnMessageActivity()? Something else?
Thanks!
The answer is summed up in the blog post I wrote after I figured it out:
https://www.michaelgmccarthy.com/2021/03/13/sending-a-welcome-message-in-the-v4-bot-framework-via-direct-line-and-liveperson/

How can a microsoft botframework bot mention a user in an adaptivecard?

Following the Schema explorer at adaptivecards.io, there is no schema available for a bot mentioning a user (and teams sending him the proper notification that he was mentioned afterwards).
See https://adaptivecards.io/explorer/ for reference.
We want to use the bot for proactive messages to our users. If a user gets mentioned in one other solution, we want to transfer that mentioning event to the teams channel conversation and leverage the ability of teams to notify the user appropriately.
Question: Is there a way to add a valid teams #mention to a channel member in a bot message using adaptivecards?
Mentioning users can be done in AdaptiveCards according to this documentation: https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-format?tabs=adaptive-md%2Cconnector-html#mention-support-within-adaptive-cards
To include a mention in an Adaptive Card your app needs to include the following elements
<at>username</at> in the supported adaptive card elements
The mention object inside of an msteams property in the card content, which includes the Teams user id of the user being mentioned
The mention object looks similar to this:
{
"msteams": {
"entities": [{
"type": "mention",
"text": "<at>John Doe</at>",
"mentioned": {
"id": "8:orgid:{org-ID-of-the-user}",
"name": "John Doe"
}
}]
}
}
The ID to mention a user needs to be in this format: 8:orgid:{org-ID-of-the-user}

Can Botframework add a reaction to a user message?

Can bot add a reaction to a user message?
I tried to send an activity like this:
{
"type": "messageReaction",
"reactionsAdded": [{ "type": "like" }],
"replyToId": 1579278444192
}
on this URL - /v3/conversations/{conversationId}/activities/{activityId}
It depends entirely on the channel you are wanting to implement this on. If channel x (Facebook, Slack, etc.) sends "reactions" as part of the activity and the service allows you to scope to it, then it is possible to return bot responses based on those.
The Botbuilder-Samples GitHub 25.message-reaction Javascript sample demonstrates how this is achieved for Teams. The C# version can be referenced here. You would need to adjust the code to look for the appropriate context/activity data points and filter on those to send a response back.
Hope of help!

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. 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.
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
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

Resources