How to send message to another user via bot and come back to original conversation with first user without losing the context? - botframework

I am trying to build a bot that can address the requests placed by the user. However, the bot needs to ask permission of the user's manager for the same. So this is the flow:
User places a request to the bot.
Bot informs user's manager to either approve or reject the request
Based on the response from manager, bot either address the request or does not and informs the user.
I am able to make a 1:1 conversation between bot and user using the PromptDialog, and perform steps 1 and 3. However, I am not sure how to send message to another user for approval or rejection and continue the earlier conversation with the first user. I am using C# for this bot. Any ideas on how could I do this?
Thanks
Niyati

After sending the message to a second user using the following code and storing in the inbox of the first user, you can send the stored result, again using the above code, to the first user and follow their conversations.
string recipientId ="123456789"; // For Example
string serviceUrl = "https://telegram.botframework.com"; // For Example
var connector = new ConnectorClient(new Uri(serviceUrl));
IMessageActivity newMessage = Activity.CreateMessageActivity();
newMessage.Type = ActivityTypes.Message;
newMessage.From = new ChannelAccount("<BotId>", "<BotName>");
newMessage.Conversation = new ConversationAccount(false, recipientId);
newMessage.Recipient = new ChannelAccount(recipientId);
newMessage.Text = "<MessageText>";
await connector.Conversations.SendToConversationAsync((Activity)newMessage);
The code comes from here.

check this page, specifically the start conversation section.

You could keep a context stack for each user, pushing an item on top of the stack for each message sent by the bot and matching context in FIFO order for each message recieved. Now this context stack goes into a map identified by the userId/userKey.
Bot-context is a library which does exactly this. The related blog post.

Related

How to get message id of sent message Bot Framework (Teams channel)?

I am using Bot Framework SDK for Javascript. My bot is connected to the Teams channel. Right now I am saving every outgoing and incoming message from my bot to the DB.
But I want also to save reactions of user to my messages. That is why I am using TeamsActivityHandler and onReactionsAdded method (link). In the docs there is written that replyToId field of turnContext is the id of message user is reacting to.
But when I am sending message to the user via turnContext.sendActivity() I don't know the internal id which will be given to this message on Teams side, that is why I can not pair reaction to the message stored in my db.
So my question is, How can I get the id of the message after sending it via turnContext.sendActivity() which will be later send in replyToId field to onReactionsAdded handler?
In other words I want to collect feedback (via reactions) on the messages my bot is sending to the user and save them to my DB (messages and reactions).
Actually it turns out (after some tries) that turnContext.sendActivity() returns ResourceIdentifier which contains one field id and that id is the id which will be given to the message on the Teams side.
EDIT:
For some ResourceIdentifier is empty when message sent to Teams is for example hero card. So this does not completly work.
You can access the activity param after the await command to get this ID. So if we have our response in a variable reply (could be text, hero card etc...) we can get the ID after the await (inside the override async Task OnMessageActivityAsync method)
await turnContext.SendActivityAsync(reply, cancellationToken);
string responseMsgId = reply.Id;

How can i ignore user messages in MS Bot framework using direct line channel

I have created a bot in one scenario it will call an API and it will take same time to get the output from that API, if in between user type anything it will start working on the text which user sent recently. I want till API output is not received, if user sent any messages it will get ignored.
If your bot is integrated into some app then you can actually disable the send button until you receive an answer for the previous question.
I am setting one session when user is requesting to talk with Live Agent, Then i am checking if that session is in progress and any new message is coming from user then i am just ignoring them.

Microsoft Bot Framework: Determining user's Web Chat session availability

My scenario is to send an event or message to user's session to check if he is active or not.
I have the conversationId, Bot Channel Account and user connection details already.
I followed the same steps as mentioned here.
The issue that see is, irrespective of whether user is active or not, i always get ConversationId as the response, which does'nt help me to determine the session availability. I think, my event or message is getting ignored if session is not available.
Can somebody help me here please?
Adding more information :
My requirement is to update the status of all the inactive users.
I have all the respective conversationIds of the sessions that these users were part of.
To determine whether user is still active on web chat(meaning, he has'nt closed the webchat window), I am sending an event activity from bot to that user's session by providing the conversationId that we have for that session.
Below is the sample code snippet.
var eventActivity = Activity.CreateEventActivity();
eventActivity.From = user.Bot.ChannelAccount;
eventActivity.Recipient = user.Connection.ChannelAccount;
eventActivity.Conversation = user.Connection.ConversationAccount;
eventActivity.Value = userSession;
eventActivity.Name = message.Name;
var connector = new ConnectorClient(new Uri(user.Connection.ServiceLink));
var response = await connector.Conversations.SendToConversationAsync((Activity)eventActivity).ConfigureAwait(false);
userSession here contains two properties UPN and ConversationId that uniquely identifies the session.
Also, This UserSession's ConversationId is same as user.Connection.ConversationAccount.ConversationId.
Below image contains the User.Bot and user.Connection objects.
Bot&Connection_Objects

How to enable bot to find Skype group chat it was added to previously?

I am creating a bot with Microsoft Bot Framework that is supposed to, when receives notification from CI server, notify about build events participants of a particular chat group in Skype.
I don't quite get it, when I've added Skype bot to the chat, it has received an activity that presumably would have allowed me to save some Id at that stage. But since I need the bot to be proactive and post messages based on external stimuli, I would need to know the reference to that group chat permanently, including after re-deployment. But after redeployments, I don't have a conversation reference.
In theory, what bit of data, given that I save it during add time, would enable me to proactively send messages at any given point in time?
If it is ok that all participants "join" the conversation by writing first to the bot and if your bot accepts messages in similar Post method
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
string rawActivity = JsonConvert.SerializeObject(activity);
Save(rawActivity);
}
Then you are able to send messages to that conversation from your bot any time by using following code. You can restart or even redeploy your bot in the meantime. I have tested about one week as maximum time between consecutive messages.
public void MethodInvokedByExternalEvent(string externalMessage)
{
var activity = JsonConvert.DeserializeObject<Activity>(GetStoredActivity());
var replyActivity = activity.CreateReply(externalMessage);
ResourceResponse reply = null;
using (var client = new ConnectorClient(new Uri(activity.ServiceUrl)))
{
reply = client.Conversations.ReplyToActivity(replyActivity);
}
}

Send custom metadata to New Conversation call

We are using the Direct Line API to carry conversations between an app and a bot. When we create a new conversation, we would like to include some metadata about the current user. The idea is that the bot would recognize the user and send a personalized message to the user. Is there a way have this behavior happen?

Resources