[BotFramework]:如何解决:在 V4 开发的 C# WebChatBot 中,欢迎消息没有显示给用户,但在模拟器中显示?

Cha*_*N G 2 c# bots chatbot botframework

<!DOCTYPE html>
<html>
<head>
    <title>Avanade D365 F&O Assets BOT</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!--
      For demonstration purposes, we are using development branch of Web Chat at "/master/webchat.js".
      When you are using Web Chat for production, you should use the latest stable at "/latest/webchat.js".
      Or locked down on a specific version "/4.1.0/webchat.js".
    -->
    <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <style>
        html, body {
            height: 100%
        }

        body {
            margin: 0
        }

        #webchat {
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="webchat" role="main">
        <iframe src='https://webchat.botframework.com/embed/AssetsBot?s=<<given my code here as it is secret i have attached this removing the code>>' style='min-width: 400px; width: 100%; min-height: 500px;'></iframe>
    </div>
     <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <script>
               // We are using a customized store to add hooks to connect event
        const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            // When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
            dispatch({
              type: 'WEB_CHAT/SEND_EVENT',
              payload: {
                name: 'webchat/join',
                value: { language: window.navigator.language }
              }
            });
          }
          return next(action);
        });
        
        const styleOptions = {
            botAvatarImage: '<<Given Image URL, removed as these are project specific>>',
            botAvatarInitials: 'BF',
            userAvatarImage: '<<Given Image URL, removed as these are project specific>>',
            userAvatarInitials: 'WC',
            bubbleBackground: 'rgba(0, 0, 255, .1)',
            bubbleFromUserBackground: 'rgba(0, 255, 0, .1)'
        };
        window.WebChat.renderWebChat({
            directLine: window.WebChat.createDirectLine({ secret: '<<given my code here as it is secret i have attached this removing the code>>' }),

            // Passing "styleOptions" when rendering Web Chat
            styleOptions
        }, document.getElementById('webchat'));
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我使用 SDK 4 在 C# 中创建了聊天机器人,当用户在浏览器中打开 webchatbot 时,我试图显示欢迎文本。目前,欢迎文本在发布到 Azure 后显示在模拟器中,但不在 WebchatBot 打开的离子浏览器中显示。只有在我输入“嗨”或其他任何内容后,它才会显示欢迎消息。这不应该是它应该首先显示欢迎文本然后我可以输入 Hi 或其他任何内容以继续对话

问题:仅在我输入任何内容后才显示发布后,欢迎消息显示在模拟器中但不在网络聊天机器人中?在浏览器中打开 Webchatbot 后,应立即显示欢迎消息。

语言:C#

机器人 SDK:V4

Bot Builder 包:通过 Nuget 更新到 4.4.3

Bot Emulator:从 GitHub 版本下载并安装最新的 4.4.1

欢迎文本在 IBOT 类的 OnTurnSync 方法内的 ConversationUpdate 活动中调用。下面给出代码供参考。

请通过提供分步指导来帮助我,因为我是 BOT 和编码的新手?

我已经尝试过一些事情,例如:

  1. 在模拟器中调试但没有太大帮助

下面的代码我用过:

public const string WelcomeText = "Welcome!. This bot uses a custom dialog that executes a data driven flow.  Type anything to get started.";

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{            
    if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
    {
        if (turnContext.Activity.MembersAdded != null)
        {
            await SendWelcomeMessageAsync(turnContext, cancellationToken);
        }
    }
}

private static async Task SendWelcomeMessageAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
    foreach (var member in turnContext.Activity.MembersAdded)
    {
        if (member.Id != turnContext.Activity.Recipient.Id)
        {
            var reply = turnContext.Activity.CreateReply();
            reply.Text = WelcomeText;
            await turnContext.SendActivityAsync(reply, cancellationToken);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

预期结果:不仅在模拟器中发布后,还应在 WebchatBot 中显示欢迎文本。
实际结果:欢迎文本仅在模拟器中有效,但在 Webchatbot 中无效,只有在我输入任何内容后才会显示。

Nic*_*s R 8

这是一个关于欢迎用户的常见问题。

由信道引发的事件不是在每个通道上是相同的:在事件之间的一个主要区别Webchat,并Emulator是:

  • 在模拟器ConversationUpdate上,在对话开始时发送2 个事件(添加了机器人的 1 个,添加了用户的 1 个)
  • 在网络聊天中,ConversationUpdate用户发送了 1 条消息后才会发送关于用户的消息

因此,要绕过这种行为,您可以event使用一种称为backchannel. 有这个用例的样本上GitHub的仓库在这里

简而言之,您必须:

  • 在开始时从网络聊天发送事件
  • 在机器人端处理此事件并处理您的欢迎消息

  • 我遇到了同样的问题,但无法打开您共享的 git 存储库@Nicolas R (2认同)