2

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

}

Chris
  • 39
  • 6
  • hi @B.Lec thanks for your response. I'll give the HTML a go. – Chris May 09 '19 at 10:02
  • This seems related to this [StackOverflow Question](https://stackoverflow.com/questions/56056885/is-the-bot-framework-emulator-handling-new-members-differently-from-bot-framewor). Essentially, each Channel (The Emulator, Web Chat, Test in Web Chat, Teams, etc.) have their own way of handling adding members and sending welcome messages. For example, Test in Web Chat in Azure Portal will only message the user if the user messages first. – tdurnford May 09 '19 at 17:28

1 Answers1

1

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>
B. Lec
  • 312
  • 2
  • 13
  • hi B.Lec i didnt get very far with this, I dont think i have enouh knowledge to understand how to use the code. All im trying to do is place the bot onto a sharepoint site in an iframe – Chris May 22 '19 at 13:12
  • @Chris on your azure portal activate the "Direct Line" channel for your bot, use the sample I gave to create an html page, replace the `var secret = 'XXXXXX-BotSecret-XXXXXXX';` by one of the direct line secret key, test it, and if it works call it in your iframe src. Not sure if I am clear – B. Lec May 22 '19 at 15:45
  • yep, works well in a webpage - still working on the iframe element - many thanks – Chris Jun 06 '19 at 12:33