Bot Framework v4 welcome message in Webchat Channel - botframework

I am working with Bot framework v4 webchat, trying to send a welcome message when a bot is initialized. First i send an event activity to the bot from webchat channel and got a 200 status response from but i can't able to capture the event activity in OneventActivityAsync action.
I have go though the below given solution.
https://github.com/microsoft/BotFramework-WebChat/tree/master/samples/04.api/a.welcome-event
OneventActivityAsync Section:
if (turnContext.Activity.Name == "webchat/join")
{
await turnContext.SendActivityAsync("Welcome Message!");
}
Could anyone help me on this?
I really appreciate any help :)

Your approach should work for v3. But for v4 you need to handle this event in OnMembersAddedAsync method.
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITur
nContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.ChannelId != "webchat" && turnContext.Activity.ChannelId != "directline") {
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync("Welcome");
}
}
}
}
Br,
Pdeepa

Related

Kendo-ui JQuery not autostarting conversation with microsoft botframework

I want to use the Kendo-UI for JQuery as the front end for my bot.
This demo is what I'm trying to use. When you start the demo the bot automaticly greets you, but when I change the secret to mine it doesn't start the conversation. With my secret it just waits for user input before starting. When I test my bot with the bot framework emulator it does send a message when connecting.
I do not know why this happens. I did not change any code of the kendo demo, which makes me think that the problem lies with my bot framework. On the other hand the emulator does give me the desired outcome. I'm at a loss on how to solve my issue.
Below is the onMembersAddedAsync function of MyBot.cs, which to my understanding should send the greeting message when a new client connects.
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
//sends some messages to welcome the user + suggestedactions
await WelcomeNewUser(turnContext, cancellationToken);
}
}
}
I found a possible solution, instead of using the OnMembersAddedAsync function I send the welcome message from OnTurnAsync. I deleted the OnMembersAddedAsync because it doesn't do anything anymore.
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
await base.OnTurnAsync(turnContext, cancellationToken);
if (turnContext.Activity.Type == ActivityTypes.Message)
{
// Save any state changes that might have occured during the turn.
await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
}
else
{
// Send an initial welcome message
if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate && turnContext.Activity.MembersAdded.FirstOrDefault()?.Id == turnContext.Activity.Recipient.Id)
{
await WelcomeNewUser(turnContext, cancellationToken);
}
}
}

Skill bot sending proactive messages to a user

I have developed a bot which sends Proactive messages to user
I also cretaed bots which trigger skills.
I was trying to write something where a skills bot/Dialog would be able to send proactive message received via webhooks to the user and continue with the existing Skill dialog.
I am not able to quite understand that. If anyone could help me there.
I used the sample from here to create a simple Skill bot which saves the ConversationReference of the current Activity and calls a service in OnMessageActivityReceived()
// Save ConversationReference
var conversationReference = activity.GetConversationReference();
_conversationReferences.AddOrUpdate(conversationReference.User.Id, conversationReference, (key, newValue) => conversationReference);
// Calling external service
HttpResponseMessage responsemMsg = await client.PostAsync(RequestURI, stringContent);
if (responsemMsg.IsSuccessStatusCode)
{
var apiResponse = await responsemMsg.Content.ReadAsStringAsync();
//Post the API response to bot again
await turnContext.SendActivityAsync(MessageFactory.Text($"Message Sent {turnContext.Activity.Text}"), cancellationToken);
}
The called service eventually emits an event which calls an action in the NotifyController in my Skills Bot. It tries to grab the ConverstaionReference and send the activity using the TurnContext.
//I am using the Skill Bot Id for _appId parameter
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, async (context, token) =>
{
await context.SendActivityAsync(proMsg.eventName);
await context.SendActivityAsync(proMsg.context.ToString());
}, new System.Threading.CancellationToken());
This SendActivity fails and OnTurnError from the Skill Bot handles the exception.
I am not sure where excatly I am going wrong. I am new to the Bot framework learning.
My issue got resolved by using the ContinueConversation() overload with ClaimsIdentity and setting the right claims for audience, appid etc. It was basically authentication issue.
public virtual Task ContinueConversationAsync(ClaimsIdentity claimsIdentity, ConversationReference reference, string audience, BotCallbackHandler callback, CancellationToken cancellationToken);
This is how I created the ClaimsIdentity:
System.Collections.Generic.List<Claim> lclaim = new System.Collections.Generic.List<Claim>
{
new Claim(AuthenticationConstants.VersionClaim, "2.0"),
new Claim(AuthenticationConstants.AudienceClaim, <SkillBotId>),
new Claim(AuthenticationConstants.AppIdClaim, <SkillBotId>),
new Claim(AuthenticationConstants.AuthorizedParty, <SkillBotId>),
};
ClaimsIdentity ci = new ClaimsIdentity(lclaim);
async Task BotCallBack(ITurnContext turnContext, CancellationToken token)
{
<code to send activity back to parent bot>
}
await ((BotAdapter)this.botFrameworkHttpAdapter).ContinueConversationAsync(
ci,
conversationData.ConversationReference,
<ParentBotId>,
callback: BotCallBack,
default).ConfigureAwait(false);

welcome message duplicated in Facebook channel

I'm using the following pattern used in multiple samples and it works fine in WebChat and Emulator channels but when I connected my bot to the Facebook channel it sends a duplicate welcome message.
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
var welcomeText = "Hello and welcome!";
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText, welcomeText), cancellationToken);
}
}
}
Any ideas?
I haven't worked a lot with the OnMembersAdded, so I also still have some missing understanding there, and I haven't worked with facebook bots at all, so this is just a simple guess in case it can help - it might be that facebook is sending multiple users in the membersadded list in this case. Have you tried debugging and seeing what comes through? I would guess that possibly you don't need to do a "turnContext.SendActivityAsync" in every loop of the foreach - maybe just do a check if you need to send any message (i.e into a boolean) and then just send a single one after the loop if the boolean is true.
It seem to be a bug in the bot framework.
I was able to workaround it with changed condition:
if (!member.Id.EndsWith(turnContext.Activity.Recipient.Id))
The bug is reported here
https://github.com/microsoft/BotFramework-Services/issues/165

How to implement a welcome activity when the bot first starts - NLP is from Google Dialogflow

How to implement a welcome activity when the bot first starts - NLP is from Google Dialogflow.
I have designed the chatbot -intent, entities and NLP from Google Dialogflow and I have integrated successfully with the botframework webchat in a html file on referring this url.
The bot design and also the bot response is good to go. My most expected is am not getting the Bot response first here.
The welcome intent from Google Dialogflow has to get trigger from the following code as per the link given above.
But I am unable to get the bot trigger first here.
How to trigger the event of Google Dialogflow from the code.
I am expecting same like this
Note: Also referred this url
When a user joins WebChat, a conversation update activity will be sent to the bot. Once the activity is received, you can check if a member was added and send the welcome message accordingly.
If you are using the Activity Handler that was released in v4.3, you can simply add an onMembersAdded handler and send the welcome message from there.
class Bot extends ActivityHandler{
constructor() {
super();
this.onMembersAdded(async (context, next) => {
const { membersAdded } = context.activity;
for (let member of membersAdded) {
if (member.id !== context.activity.recipient.id) {
await context.sendActivity("Welcome Message!");
}
}
await next();
});
...
}
}
If you are not using the activity handler, in the bot's onTurn method, you can check if the incoming activity handler is a conversation update and if a member has been added.
async onTurn(turnContext) {
if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
if (turnContext.activity.membersAdded && turnContext.activity.membersAdded.length > 0) {
for (let member of turnContext.activity.membersAdded) {
if (member.id !== turnContext.activity.recipient.id) {
await turnContext.sendActivity("Welcome Message!");
}
}
}
} ...
}
For more details on sending welcome messages, please take a look at this sample.
Hope this helps!

Invokeresponse in botframework v4

How can I return an InvokeResponse in botframework v4 for C#? I need this to respond to compose extension activity messages. In the old framework this was done by returning in the response a composeExtension object from the controller.
How can this be done when implementing the IBot interface.
In the old framework there were MS Teams extensions, not available for the new framework version.
To respond to an invoke activity you have to set the "BotFrameworkAdapter.InvokeResponse" in turnContext.TurnState like in the below example
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
// do stuff
}
if (turnContext.Activity.Type == ActivityTypes.Invoke)
{
// do stuff
var invokeResponse = new InvokeResponse()
{
Body = response,
Status = (int)HttpStatusCode.OK
};
var activity = new Activity();
activity.Value = invokeResponse;
// set the response
turnCoontext.TurnState.Add<InvokeResponse>(InvokeReponseKey, activity);
}
}
For what I think you are asking:
There is an example of handling an invoke response in this sample. In your OnTurnAsync you need to catch the Invoke activity and do whatever it is you need to do with the activity like in the sample.
I'm unsure which SDK you are using as you did not include it in your question but, a simple example in C# (Node would be similar) might look like this:
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
//do stuff
}
if (turnContext.Activity.Type == ActivityTypes.Invoke)
{
//do stuff
}
}
From the BF SDK v4 code here:
https://github.com/Microsoft/botbuilder-dotnet/blob/4bb6b8d5faa4b252379ac331d6f5140ea27c177b/libraries/Microsoft.Bot.Builder/BotFrameworkAdapter.cs#L216
https://github.com/Microsoft/botbuilder-dotnet/blob/4bb6b8d5faa4b252379ac331d6f5140ea27c177b/libraries/Microsoft.Bot.Builder/BotFrameworkAdapter.cs#L285
what you do is use ITurnContext to "reply" with a fake activity of type ActivityTypesEx.InvokeResponse, setting Activity.Value to an InvokeResponse object with your desired status code and payload.

Resources