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

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);
}
}

Related

How to send message activity from Web Chat Bot to Microsoft Teams channel

I have created a Echo Bot in c# using QnA maker which is working absolutely fine now I wanted to achieve a scenario where if user ask any question and bot unable to find related answer than this question must be sent on Microsoft Teams channel where except will reply to the same and that message will sent to the user.
So, Is there any way to send message user message to Microsoft Teams for expert reply. If you have any sample code for the scenario please feel free to mention.
As per your current requirement this is kind of handoff or human live agent connect.
The following way you can achieve posting a message in ms team ( Go through this article Send proactive messages to Teams channels and users ).
Send proactive messages to Teams channels and users ( Microsoft Bot Framework v4 )
The user should be part of ms teams ( Azure AD valid users ).
Suggestions : If you are using domain bot then human live agent or handoff concept is the best approach otherwise you can integrate bin search api or any other third party api for unanswered question.
As per your requirement you can use Graph API to send message to channel using below code
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var chatMessage = new ChatMessage
{
Body = new ItemBody
{
Content = "Hello World"
}
};
await graphClient.Teams["{team-id}"].Channels["{channel-id}"].Messages
.Request()
.AddAsync(chatMessage);
Please go through this documentation for more info.

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/

Unable to send message to Teams via the ConnectorClient bot connector

I am attempting to send a message to a Teams Channel from within a proactive 3rd party WebHook callback that resides in my Teams Bot which is triggered externally some time after my bot conversation has ended.
My config.ServiceURL is 'https://smba.trafficmanager.net/amer/' which I got from my bot while conversing in a session.
My config.MicrosoftAppId value is the ApplicationID assigned to my bot App registration in Azure.
My config.MicrosoftAppPassword is a Client secret I created for the registered bot App.
My ChannelInfo value is the value I discovered by enumerating all the target team's channels in another application. I replaced the actual values with 'f's.
In Azure I added the following permission to my registered App: Delegate - Group.ReadWrite.All
When I attempt to create the conversation I get a Forbidden response. If I change my ChannelInfo below to a bogus value I receive a Not Found response as expected so at least the ChannelInfo appears to be a valid value.
Is there another permission I need to add? Have I missed step? Is there something I did wrong?
Here is the code:
AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json");
var credentials = new MicrosoftAppCredentials(config.MicrosoftAppId, config.MicrosoftAppPassword);
if (!MicrosoftAppCredentials.IsTrustedServiceUrl(config.ServiceURL))
{
MicrosoftAppCredentials.TrustServiceUrl(config.ServiceURL);
}
var serviceUri = new Uri(config.ServiceURL);
var botConnector = new ConnectorClient(serviceUri, credentials);
Activity message = (Activity)Activity.CreateMessageActivity();
message.Text = "Hello World";
var conversationParameters = new ConversationParameters
{
Bot = message.From,
IsGroup = true,
ChannelData = new TeamsChannelData
{
Channel = new ChannelInfo("19:ffffffffffffffffffffffffffffffff#thread.skype")
},
Activity = (Activity)message
};
var conversationResponse = await botConnector.Conversations.CreateConversationAsync(conversationParameters);
await botConnector
.Conversations
.SendToConversationAsync(message);
Your code worked fine for me. Here's a few things you can try:
Ensure you sideloaded your bot into Teams via App Studio or manifest upload and that you're not just talking with the bot by appId
Ensure your bot is installed to the Team you want it to message
Add the domain your bot is hosted on to the Valid Domains section of the bot manifest
Esure you're sending the message to the correct conversation by changing .SendToConversationAsync(message); to .SendToConversationAsync(conversationResponse.Id, message);
Also note that the Microsoft.Bot.Builder.Teams package has been deprecated now that Bot<->Teams functionality has been rolled directly into the Bot Framework SDK. I highly recommend migrating away from it.
You may want to send proactive messages as shown in this sample. The other Teams Samples are numbers 50-60.
Let me know if you want to stick with that package and I can continue trying to provide support.

Bot Framework v4 - Bot Initiate Conversation

I am developing a bot for Microsoft Teams using the Bot Framework SDK v4 for NodeJS. Is there a way that the bot can automatically initiate a conversation in a channel, rather than user initiating the conversation? My bot works fine when the user initiates the conversation. Any suggestions on how I can proceed with this?
MS Teams calls that a "Proactive Message" (note: Bot Framework generally defines a "proactive message" as sending a user a message not related to current conversation, that you have a reference for. Teams lumps a few things into this category). You can read more about how to use proactive messaging from the official Teams docs. Or, more specifically, creating a channel conversation.
The gist of it is that you need to capture a conversationUpdate and check for a new member added to the conversation or fetch the team roster, then you send the proactive message.
Note: For MS Teams, the user or team will have to add the bot first:
Bots can create new conversations with an individual Microsoft Teams user as long as your bot has user information obtained through previous addition in a personal or team scope. This information enables your bot to proactively notify them. For instance, if your bot was added to a team, it could query the team roster and send users individual messages in personal chats, or a user could #mention another user to trigger the bot to send that user a direct message.
Some developers come across 401: Unauthorized errors when using proactive messaging, especially if the bot was restarted for some reason and the bot is attempting to re-initiate a proactive message. You can read more about preventing that by using trustServiceUrl from this Sample (this is my branch, which is being used to submit a Pull Request to update the Proactive Sample with trustServiceUrl info).
You can initiate a brand new conversation using the connector Client in the Botframework V4 and the Teams Extensions V4. In nodejs, you will find a solution in one of the comments for this Github Issue. For anyone looking for a solution in C#, here is a detailed blog post about accomplishing this in C# version of the botframework.
in nodejs :
var conversationReference = TurnContext.getConversationReference(context.activity)
connectorClient = await createConnectorClient(context)
var conversationParameters = {
isGroup: true,
bot: conversationReference.bot,
channelData: (await teamsCtx.getTeamsChannelData()),
tenantId: teamsCtx.tenant.id,
activity: MessageFactory.text("Queue Summary Placeholder") as Activity
} as ConversationParameters
await connectorClient.conversations.createConversation(conversationParameters)
In C#
ConnectorClient _client = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), await GetMicrosoftAppCredentialsAsync(turnContext), new HttpClient());
var channelData = turnContext.Activity.GetChannelData<TeamsChannelData>();
var conversationParameter = new ConversationParameters
{
Bot = turnContext.Activity.Recipient,
IsGroup = true,
ChannelData = channelData,
TenantId = channelData.Tenant.Id,
Activity = MessageFactory.Text(message)
};
var response = await _client.Conversations.CreateConversationAsync(conversationParameter);
Really we need to know when you want the bot to send the message, the bot framework TeamsActivityHandler class provides multiple methods that you can utilise for example:
onMembersAdded(BotHandler): Registers an activity event handler for the members added event, emitted for any incoming conversation update activity that includes members added to the conversation.
Learn more about the events / methods you can utilise here.
I ended up figuring it out and i wrote a bot controller that i can invoke on demand with the following code.
var conversationParameters = new ConversationParameters
{
IsGroup = true,
ChannelData = new TeamsChannelData
{
// this needs to come from the teams context.
Channel = new ChannelInfo(channelId),
},
Activity = (Activity)MessageFactory.Attachment(attachment)
};
// your service url may differ.
MicrosoftAppCredentials.TrustServiceUrl(String.IsNullOrEmpty(serviceUrl) ? constantServiceUrl : serviceUrl, DateTime.MaxValue);
var response = connectorClient.Conversations.CreateConversationAsync(conversationParameters).GetAwaiter().GetResult();

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

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.

Resources