在用户开始与我的机器人聊天之前,如何向机器人发送一些初始数据? [英] How can I send some initial data to the bot before the user start chatting with my bot?

查看:64
本文介绍了在用户开始与我的机器人聊天之前,如何向机器人发送一些初始数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,在用户开始聊天之前,如何代表用户向机器人发送一些数据.

My problem is, How can I send some data on behalf of user to the bot before user start chatting.

因为不同的客户端将具有不同的终结点,所以我希望bot首先获取该终结点并将其保存为UserState,然后再使用此终结点进行以后的API调用.

because different client will have different endpoint, I would like bot to get this endpoint first and save it as UserState, then use this endpoint for making API calls later.

我正在使用" https://github.com/microsoft/BotFramework-WebChat作为我的客户端的此网络聊天,它使用秘密创建了直接路线,是否可以在下面的html文件中添加发布活动以发送一些数据?

I'm using "https://github.com/microsoft/BotFramework-WebChat" this web chat as my client side, it create the directline using the secret, is that possible i add a post activity in the html file below to send some data?

谢谢!

<!DOCTYPE html> <html> <body>
    <div id="webchat" role="main"></div>
    <script src="Scripts/Directline.js"></script>
    <script>
        window.WebChat.renderWebChat({
            directLine: window.WebChat.createDirectLine({
                token: 'my secret'
            }),
            locale: 'en-US',
            botAvatarInitials: 'Bot',
            userAvatarInitials: 'ME',
        },
        document.getElementById('webchat'));
    </script> </body> </html>

推荐答案

您可以在Web Chat的商店中添加自定义中间件,当DirectLine连接完成时,该中间件可以将包含必要数据的事件发送到bot.请参见下面的代码段.

You can add a custom middleware to Web Chat's store which can send an event containing the necessary data to the bot when the DirectLine connection is fulfilled. See the code snippets below.

网络聊天

const store = window.WebChat.createStore({},
  ({ dispatch }) => next => action => {

    if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
      // Send event to bot with custom data
      dispatch({
        type: 'WEB_CHAT/SEND_EVENT',
        payload: {
          name: 'webchat/join',
          value: { data: { username: 'TJ'}}
        }
      })
    }
    return next(action);
  });        


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

Bot-C#SDK

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using  Newtonsoft.Json.Linq;

namespace Microsoft.BotBuilderSamples.Bots
{
    public class EchoBot : ActivityHandler
    {
        protected override async Task OnEventAsync(ITurnContext<IEventActivity> context, CancellationToken cancellationToken)
        {
            if (context.Activity.Name == "webchat/join") {
                var data = JObject.Parse(context.Activity.Value.ToString()).GetValue("data");
                var user = JObject.Parse(data.ToString()).GetValue("username");
                await context.SendActivityAsync($"Hi, {user}!");
            }
        }

    }
}

有关更多详细信息,请参见

For more details, take at the Send Backchannel Welcome Event Web Chat sample.

希望这会有所帮助!

这篇关于在用户开始与我的机器人聊天之前,如何向机器人发送一些初始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆