如何使BOT发起对话

时间:2019-05-09 09:09:28

标签: c# botframework qnamaker

我创建了一个与新的多匝QnAmaker功能配合使用的QnABot。与仿真器一起使用时,BOT可以很好地启动对话,但在Iframe或Azure测试环境中使用时,BOT不能启动对话。谁能帮助我了解我需要添加或更改代码以使其启动的原因。澄清一下,当我在本地运行代码时,它可以工作。在iframe或类似产品中不起作用

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

}

1 个答案:

答案 0 :(得分:0)

您不能从服务器端执行任何操作,而必须从客户端发起对话。在Azure测试环境或Iframe(直线)上,当您发送第一条消息时就完成了。

这是嵌入机器人的html页面示例

<!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>