【问题标题】:How to make BOT initiate conversation如何让 BOT 发起对话
【发布时间】:2019-09-27 02:05:16
【问题描述】:

我创建了一个与新的多圈 QnAmaker 功能配合使用的 QnABot。 BOT 在与模拟器一起使用时可以很好地启动对话,但在 Iframe 或 Azure 测试环境中使用时则不行。任何人都可以帮助我了解我需要在代码中添加或更改什么以使其启动。澄清一下,当我在本地运行代码时,它可以工作。它不适用于 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);
            }
        }
    }
}

}

【问题讨论】:

  • 嗨@B.Lec 感谢您的回复。我会试试 HTML。
  • 这似乎与此StackOverflow Question 有关。本质上,每个频道(模拟器、网络聊天、网络聊天中的测试、团队等)都有自己的方式来处理添加成员和发送欢迎消息。例如,Azure 门户中的 Web 聊天中的测试只会在用户先发消息时才发消息给用户。

标签: c# botframework qnamaker


【解决方案1】:

你不能从服务器端做任何事情,你必须从客户端发起对话。在 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>

【讨论】:

  • 嗨 B.Lec 我对此并没有很了解,我认为我没有足够的知识来理解如何使用代码。我要做的就是将机器人放在 iframe 中的共享点站点上
  • @Chris 在您的 azure 门户上为您的机器人激活“直线”通道,使用我提供的示例创建一个 html 页面,将 var secret = 'XXXXXX-BotSecret-XXXXXXX'; 替换为直接线路密钥之一,测试它,如果它有效,请在你的 iframe src 中调用它。不知道我是否清楚
  • 是的,在网页中运行良好 - 仍在使用 iframe 元素 - 非常感谢
猜你喜欢
  • 2019-07-27
  • 1970-01-01
  • 2019-11-27
  • 2020-01-30
  • 2020-05-01
  • 2016-09-04
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多