Bot框架获取嵌入式聊天控制页面的ServiceUrl [英] Bot framework get the ServiceUrl of embedded chat control page

查看:98
本文介绍了Bot框架获取嵌入式聊天控制页面的ServiceUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将聊天控件嵌入到许多网站中,并且想要获得我嵌入的网站的网址,以便我的机器人可以获取与网站URL匹配的数据。但是,当我嵌入从WebChat生成的iframe时,总是得到相同的ServiceUrl,即



我注意到,当我用Bot Framework Emulator测试我的机器人时,它总是返回发送者的确切网址(如果是本地testingm,它将返回



<现在,您需要准备好机器人来监听和发送事件。 Node.js 中有一个示例。 / code>,显示了操作方法。



将其移植到 C#一样简单作为侦听和发送到事件类型的活动。为此的示例代码(使用前面提到的HTML页面的事件):

 公共异步任务< HttpResponseMessage> Post([FromBody]活动活动)
{
if(activity.Type == ActivityTypes.Event&&
string.Equals(activity.Name, buttonClicked,StringComparison.InvariantCultureIgnoreCase ))
{
ConnectorClient连接器= new ConnectorClient(new Uri(activity.ServiceUrl));

//将我们的回复返回给用户
活动回复= activity.CreateReply(我看到您刚刚按下了该按钮);
等待连接器。Conversations.ReplyToActivityAsync(reply);
}

if(activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

//将我们的回复返回给用户
var reply = activity.CreateReply();
reply.Type = ActivityTypes.Event;
reply.Name = changeBackground;
reply.Value = activity.Text;
等待连接器。Conversations.ReplyToActivityAsync(reply);
}
其他
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
返回响应;
}

底线,在HTML页面中,您必须将事件发送到bot,以及页面网址和bot必须监听该事件才能获取值


I want to embed the chat control to many websites and I want to get the Url of the website that I've embedded in order to my bot can get the Data matching with the Website URL. However, when I embed the iframe generated from WebChat, I always get the same ServiceUrl and that's https://webchat.botframework.com/, it isn't the Url of the website, so how can I embed the chat control to any website and my bot can get the website Url not the Url of the WebChat or DirectLine.

Here's what I've tried:Direct-Line chat control

Here's the result I've tested with my published bot:

I've noticed that, when I've tested my bot with the Bot Framework Emulator, it always return the exact Url of the sender (in case of local testingm, it will return http://localhost:XXXX/). So how can we do like this?

解决方案

I think a way to achieve this would be by using BackChannel, which adds the ability for a bot to communicate with a page that embeds the bot through WebChat. It will a allow you to:

  • Send events to a page that hosts an instance of a WebChat
  • Listen for events from the page that hosts an instance of a WebChat

The first piece is, of course, the HTML page, which will contain what you put together, plus the logic to send/listen to events. The sample page with the basic logic can be found here and below is the image with the events related code.

Now, you need to prepare your bot to listen and send events. There is a sample in Node.js, that shows how to do that.

Porting that in C#, is as easy as listen and sending to activities of type Event. A sample code for that (using the events of the HTML page mentioned before):

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Event &&
        string.Equals(activity.Name, "buttonClicked", StringComparison.InvariantCultureIgnoreCase))
    {
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        // return our reply to the user
        Activity reply = activity.CreateReply("I see that you just pushed that button");
        await connector.Conversations.ReplyToActivityAsync(reply);
    }

    if (activity.Type == ActivityTypes.Message)
    {
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        // return our reply to the user
        var reply = activity.CreateReply();
        reply.Type = ActivityTypes.Event;
        reply.Name = "changeBackground";
        reply.Value = activity.Text;
        await connector.Conversations.ReplyToActivityAsync(reply);
    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

Bottom line, in your HTML page you will have to send an event to the bot, with the page URL and the bot will have to listen to that event to get the value

这篇关于Bot框架获取嵌入式聊天控制页面的ServiceUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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