代码之家  ›  专栏  ›  技术社区  ›  Souvik Ghosh

将查询字符串参数传递到Directline Channel Bot框架

  •  0
  • Souvik Ghosh  · 技术社区  · 7 年前

    我使用Bot Builder SDK 4.x创建了一个bot。我可以使用模拟器和消息端点访问bot。- http://localhost:3978/api/messages . 我还将向消息传递端点传递一些查询字符串参数,如下所示- http://localhost:3978/api/messages?botid=HRbot 我可以在我的bot启动时访问它。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseDefaultFiles()
            .UseStaticFiles()
            .Use(async (context, next) =>
            {
                _config["BotId"] = context.Request.Query["botid"];
                await next.Invoke();
            })
            .UseBotFramework();
    }
    

    在将bot部署到Azure之后,我希望我的客户机使用消息端点并传递自己的查询字符串参数。因为bot需要嵌入到网页中,所以我可以使用网络聊天频道和脚本,也可以使用直接线路频道。它们都使用一个秘密密钥,没有端点。所以,我看不到将查询字符串参数传递到消息端点的任何选项。

    我看到我们可以通过一些 token 如下面所示,作为一些参数使用网络聊天的javascript-sdk直接进入频道。

    BotChat.App({
            bot: bot,
            locale: params['locale'],
            resize: 'detect',
            // sendTyping: true,    // defaults to false. set to true to send 'typing' activities to bot (and other users) when user is typing
            speechOptions: speechOptions,
            user: user,
            directLine: {
              domain: params['domain'],
              secret: params['s'],
              token: params['t'],
              webSocket: params['webSocket'] && params['webSocket'] === 'true' // defaults to true
            }
          }, document.getElementById('chatBot'));
    

    我不知道如何使用我的bot服务消息API为每个想要使用不同查询字符串参数的API的客户机提供服务。

    有什么帮助吗?如果我能澄清更多,请告诉我。

    2 回复  |  直到 7 年前
        1
  •  3
  •   Fei Han    7 年前
    < Buff行情>

    我如何使用我的bot服务消息API为每个希望使用不同查询字符串参数的API的客户机提供服务。

    < /块引用>

    Embeddedable Web Chat Control does not make request(s)to bot application endpoint directly,it is using the directline api.

    要从Web聊天客户端向bot应用程序传递附加信息,可以在启动bot chat时在 user:id:user_id,param:value_here code>property中指定附加参数。

    var userinfo=id:'you',userparam:'val11'
    
    BoTalk.app({)
    僵尸连接:僵尸连接,
    用户:用户信息,
    bot:id:'XXXbot',
    resize:'检测'
    },document.getElementByID(“bot”));
    < /代码> 
    
    

    然后,您可以通过bot应用程序中的activity.from.properties

    if(context.activity.type==activitytypes.message)
    {
    var uparam=context.activity.from.properties[“userparam”].toString();
    
    //这里是您的代码逻辑
    
    
    //返回给用户输入的内容。
    wait context.sendActivity($“turn state.turncount:您发送了'context.activity.text您传递的参数是uparam');
    }
    < /代码> 
    
    

    测试结果:

    更新:

    < Buff行情>

    在用户发送任何数据之前,我希望这些参数可用。

    < /块引用>

    您可以使用backchannel机制发送一个eventactivity and specifyfromproperty for passing the additional parameter,like below:。

    botconnection.postactivity({
    类型:“事件”,
    来自:
    }.subscribe(函数(id)console.log('您发送一个事件活动'););
    < /代码> 
    
    

    在bot应用程序中:

    else if(context.activity.type==activitytypes.event)
    {
    var uparam=context.activity.from.properties[“userparam”].toString();
    wait context.sendActivity($“您发送的参数是”uparam'“);
    }
    < /代码> 
    
    

    测试结果:

    Embeddable web chat control不直接向bot应用程序终结点发出请求,它正在使用Directline API。

    要从Web聊天客户端向bot应用程序传递附加信息,可以在user: { id: user_id, param: '{value_here}' }属性。

    var userinfo = { id: 'You', userparam: 'val11' };
    
    BotChat.App({
        botConnection: botConnection,
        user: userinfo,
        bot: { id: 'xxxbot' },
        resize: 'detect'
    }, document.getElementById("bot"));
    

    然后你可以得到你通过的值Activity.From.Properties在bot应用程序中。

    if (context.Activity.Type == ActivityTypes.Message)
    {
        var uparam = context.Activity.From.Properties["userparam"].ToString();
    
        // Your code logic here   
    
    
        // Echo back to the user whatever they typed.
        await context.SendActivity($"Turn {state.TurnCount}: You sent '{context.Activity.Text}; Parameter you passed is {uparam}'");
    }
    

    测试结果:

    enter image description here

    更新:

    我希望在用户发送任何数据之前这些参数是可用的。

    你可以使用the backchannel mechanism发送一个event活动和说明from用于传递附加参数的属性,如下所示:

    botConnection.postActivity({
        type: 'event',
        from: userinfo,
    }).subscribe(function (id) { console.log('you send an event activity'); });
    

    在bot应用程序中:

    else if (context.Activity.Type == ActivityTypes.Event)
    {
        var uparam = context.Activity.From.Properties["userparam"].ToString();
        await context.SendActivity($"Parameter that you sent is '{uparam}'");
    }
    

    测试结果:

    enter image description here

        2
  •  0
  •   K. Todorov    7 年前

    我对韩非给我的两个选择都有问题,他们不为我工作。经过一段时间的研究,对我有用的是他给出的后端解决方案和客户端的组合,我必须使用这里给出的示例- https://github.com/Microsoft/BotFramework-WebChat/tree/master/samples/15.d.backchannel-send-welcome-event .

    最后,我得到了这两段代码:

    C:

    else if (turnContext.Activity.Type == ActivityTypes.Event)
    {
          await turnContext.SendActivityAsync($"Received event");
          await turnContext.SendActivityAsync($"{turnContext.Activity.Name} - {turnContext.Activity.Value?.ToString()}");
    }
    

    和客户端:

    <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    
    <script>
    
        token = "your-token-here";
    
        (async function () {
            const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
                if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
                    dispatch({
                        type: 'WEB_CHAT/SEND_EVENT',
                        payload: {
                        name: 'start-chat',
                            value: {
                                "example_id": "12345",
                                "example_array": ["123", "456"]
                            }
                          }
                });
            }
            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>