Ephemeral Responses in Slack for BotFramework - botframework

I'm new to the Botframework, but am making some great headway. I've got a bot up and running and it works great for DM chats in Slack, but I'd like my bot's responses to be only to asking user in a chatroom/group instead being broadcast to the group.
Reading up on Slack, it looks like if I set the response_type to "ephemeral", this should solve the problem. Sounds potentially easy enough, but no idea if we have the ability to override this in the dialog context / activity. Any suggestions?

If you take a look at the NodeJS SDK Documentation for Sessions and scroll down to the Cards section, you will find a handy little function of the Message class
Message.sourceEvent()
From the same documentation page:
It’s not practical for the SDK to support every card or attachment
format supported by the underlying chat service so we have a
Message.sourceEvent() method that you can use to send custom messages
& attachments in the channel native schema.
Which means that you can use Message.sourceEvent() to do exactly what you are trying do: alter the structure of your outgoing message object.
Here is a code snippet for your specific case that I tried out:
//test slack source event messaging
var msg = new botbuilder.Message(session).sourceEvent({
slack: {
response_type: "ephemeral",
text: "This is a test Slack message.",
attachments: [
{
text: "Test inlaid message."
}
]
}
});
When you pass the current session to the Message() class, it populates all the address info in the Message so you don't have to do any of that. You can see for yourself here in the documentation for the Message's constructor method.
Then, you just call sourceEvent() and pass in an object, with the name of the channel you are constructing a message for (in this case slack), and then fill out the desired message data. This allows you to define your response_type: "ephemeral".
Documentation for sourceEvent() here.

Related

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/

app_mention not firing in private message

I have a bot with event scopes app_mention and messages.im. It also has OAuth scopes app_mentions.read, channels.history, chat.write, groups.history, and im.history.
If I # mention my bot in a public channel, a thread off of a public channel, or directly message my bot I get an event. But if I'm in a private message with someone else and # mention my bot, I don't get any events from the #mention.
What is the correct event scope to enable to get the event?
you can't unfortuantly. app_mention only works in conversations the bot would have access to. You would need each user to directly give you access to im history and monitor every message event for a mention of the bot itself (probably either some regex or similar to look for the <#bot_name>.
I know there was a link_names part of the json object being sent for message events but you'd still need to check the message body to make sure its your bot being mentioned and not another 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

Need suggestions in getting the conversation details

I am creating a bot using MS Bot framework - NodeJs. The below information needs to be captured for logging (Using the bot.use method i.e. IMiddleware).
Receive:
a. UserId
b. UserInput (text)
c. ConversationId
Send:
1. Name of Intent or dialog name that handled this (that handled the user input text)
2. Bot output text
3. ConversationId
4. UserId
I am unable to get the required detail for the 'send'. Can anyone provide me some suggestions on this.
Thanks.
I believe your main struggle is to log the name of intent or dialog. You won't know it in your send middleware if you haven't captured it during the routing phase. Once the Bot Framework figured out where to send the incoming message, it just invokes that function.
These two articles may help you get what you want. Just recently I played with capturing the conversation's breadcrumbs and also logging a full transcript:
http://www.pveller.com/smarter-conversations-part-3-breadcrumbs/
http://www.pveller.com/smarter-conversations-part-4-transcript/
If you need to build a reliable capture engine, I would suggest that you didn't use the session.privateConversationData like I did and instead built your own storage/log infrastructure to push the events to. Just stream them out with a timestamp and conversationId and reconcile on the other end later. The asynchronous nature of everything the bot framework does internally will be haunting you along the way so that's why. Plus, once you scale out beyond testing on a few users and your bot spans multiple processes, you will be out of the single-threaded event loop.

Resources