Reputation: 49
I've created a QnABot that works with the new multiturn QnAmaker capability. The BOT initiates conversation fine when used with the emulator but not when used in an Iframe or in the Azure test environment. Can anyone help me understand what I need to add or change in the code to make it initiate. To clarify, when I run the code locally it works. It doesn't work in Iframes or similar
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
// Greet anyone that was not the target (recipient) of this message.
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Welcome to the IBC Leave Bot, I can help you answer questions about your leave.\n\n Type HELP to get some ideas about what to ask me"), cancellationToken);
}
}
}
}
}
Upvotes: 2
Views: 790
Reputation: 352
You can't do anything from server side, you have to initiate a conversation from client side. On Azure test environment or Iframe (directline) it is done when you send your first message.
Here is a sample of html page embedding a bot
<!DOCTYPE html>
<html>
<head>
<title>
chatbot
</title>
<meta charset="UTF-8">
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
</head>
<body>
<div id="bot" />
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
var userId = guid().toUpperCase();
var userName = 'User-' + Math.floor((1 + Math.random()) * 10000);
var secret = 'XXXXXX-BotSecret-XXXXXXX';
var user = {
id: userId,
name: userName
};
var bot = {
id: 'Demo-WebAppBot',
name: ' Demo ChatBot'
};
var botConnection = new BotChat.DirectLine({
secret: secret,
webSocket: true
});
console.log("Init bot component");
BotChat.App({
botConnection: botConnection,
user: user,
bot: bot,
resize: 'detect'
}, document.getElementById("bot"));
<!-- Conversation is initiated here by sending a dummy message to the bot -->
botConnection.postActivity({ type: "event", from: user, name: "firstMessage", value: "ping" }).subscribe(id => console.log("Conversation updated"));
</script>
</body>
</html>
Upvotes: 1