Bot获取频道数据

问题描述 投票:0回答:2

团队,我使用Bot框架SDK4开发了Bot。我正在使用Directline渠道与我的漫游器进行通信。我的要求基于“ requestWelcomeDialog”消息上的channeldata,我必须显示欢迎消息。

我的机器人客户端代码:

BotChat.App({
    botConnection: botConnection,
    user: userOption,
    bot: { id: model.botId, name: model.botName },
    resize: 'window',
    speechOptions: speechOptions,
    locale: 'en',
    sendTypingIndicator: true,
}, document.getElementById('BotChatElement'));
PostBotConfiguration();
botConnection
    .postActivity({
        from: user,
        name: 'requestWelcomeDialog',
        type: 'event',
        value: { 'BotType': 'abcd' }
    })
    .subscribe(function (id) {
        setWCScreenChatPosition();
        model.botRender = true;
        console.log('"trigger requestWelcomeDialog" sent');
    });

在上面的代码中,我将BotType发送为'abcd'。我正在尝试从我的机​​器人中读取此值。

我在机器人程序中的代码。

protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
    Utility util = new Utility();
    try
    {
        foreach (var member in membersAdded)
        {
            if (member.Id != turnContext.Activity.Recipient.Id)
            {
                BotChannelData cdata = new BotChannelData();
                turnContext.Activity.TryGetChannelData(out cdata);
            }
        }
    }
    catch
    {
    }
}

在这种情况下,我总是会得到null引用异常。

我可以知道我在其中缺少什么吗?

javascript c# botframework bots direct-line-botframework
2个回答
0
投票

第一个问题是您正在使用Bot Chat。 Bot Chat是Web Chat v3,已弃用。您应该根据the repo中的说明使用Web Chat v4。

第二个问题是您试图使用OnMembersAddedAsync响应自定义事件,该事件仅由对话更新活动触发。您可以按照this issuethis sample中的说明查看如何发送和响应欢迎事件。等效的C#看起来像这样:

if (turnContext.Activity.Name == "webchat/join")
{
    await turnContext.SendActivityAsync("Back Channel Welcome Message!");
}

0
投票

[如果使用直接电话渠道,则应使用Web Chat v4。因为现在不推荐使用Web Chat v3。您可以从this official sample获取发送欢迎消息的代码。

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Web Chat: Send welcome event</title>

    <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"></div>
    <script>
      (async function () {

        const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' });
        const { token } = await res.json();

       const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
         if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
           dispatch({
             type: 'WEB_CHAT/SEND_EVENT',
             payload: {
               name: 'webchat/join',
               value: { language: window.navigator.language }
             }
           });
         }
         return next(action);
       });

        window.WebChat.renderWebChat({
          directLine: window.WebChat.createDirectLine({ token }),
        store
        }, document.getElementById('webchat'));

        document.querySelector('#webchat > *').focus();
      })().catch(err => console.error(err));
    </script>
  </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.