简体   繁体   English

点击打开浏览器窗口

[英]Open browser window on click

I am using botframework 3.9 and for some reasons I can't upgrade now. 我正在使用botframework 3.9,由于某些原因,我现在无法升级。 I would like to know if there is a way to open a new browser window where I can render a page or fire a JavaScript function. 我想知道是否有一种方法可以打开新的浏览器窗口,以便在其中呈现页面或触发JavaScript函数。 Here is how I am opening link: 这是我打开链接的方式:

await context.PostAsync(@"please [click here](http://www.example.com/)");

This does renders the link however, I wanna open a link in JavaScript window so I can close the window programmatically or if possible if I can fire some JavaScript function. 这确实会呈现链接,但是,我想在JavaScript窗口中打开链接,以便我可以以编程方式关闭该窗口,或者如果可以的话,可以触发一些JavaScript函数。

This is actually much easier than you think. 实际上,这比您想象的要容易得多。 If you have a look at the WebChat README you can see there are many ways WebChat can be customized. 如果您查看WebChat自述文件,您会发现有很多方法可以自定义WebChat。 Pay particular attention to sample 11 , which you can demo here . 请特别注意示例11 ,您可以在此处进行演示。 The body of that page looks like this: 该页面的主体如下所示:

 <div id="webchat" role="main"></div> <script> (async function () { // In this demo, we are using Direct Line token from MockBot. // To talk to your bot, you should use the token exchanged using your Direct Line secret. // You should never put the Direct Line secret in the browser or client app. // https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' }); const { token } = await res.json(); // We are creating our own version of Redux store, which include middleware and optional initial state. const store = window.WebChat.createStore( {}, ({ dispatch }) => next => action => { if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') { // After connected, we will send a message by dispatching a Redux action. dispatch({ type: 'WEB_CHAT/SEND_MESSAGE', payload: { text: 'sample:backchannel' } }); } else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') { // When receiving an activity of type "event" with name of "sample:backchannel", prompt it out. const { activity } = action.payload; if (activity.type === 'event' && activity.name === 'sample:backchannel') { alert(JSON.stringify(activity, null, 2)); } } return next(action); } ); window.WebChat.renderWebChat({ directLine: window.WebChat.createDirectLine({ token }), // We will use a custom version of Redux store, which we added middleware to handle backchannel messages. store }, document.getElementById('webchat')); document.querySelector('#webchat > *').focus(); })().catch(err => console.error(err)); </script> 

You can see in this sample that WebChat has been modified to respond to certain activities from the bot by opening a popup window using JavaScript's alert function. 您可以在此示例中看到,通过使用JavaScript的alert功能打开弹出窗口,已经对WebChat进行了修改,以响应机器人的某些活动。 The modification is done by creating a store and then passing that store as an argument to renderWebChat . 通过创建存储并将该存储作为参数传递给renderWebChat来完成renderWebChat

Rather than opening an alert window, you want to open a window you can close. 您想要打开一个可以关闭的窗口,而不是打开警报窗口。 This could be achieved if you modify the store to look like this: 如果将商店修改为如下所示,则可以实现此目的:

let windows = {};

const store = window.WebChat.createStore(
    {},
    ({ dispatch }) => next => action => {
        if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
            const { activity } = action.payload;

            if (activity.type === 'event') {
                let url = activity.value;

                if (activity.name == 'open' && !windows[url]) {
                    windows[url] = window.open(url);
                }

                if (activity.name == 'close' && windows[url]) {
                    windows[url].close();
                    windows[url] = null;
                }
            }
        }

        return next(action);
    }
);

You don't have to implement it this way, but I've implemented it so that when WebChat receives an event activity named open it will open a window and when it receives an event activity named close it will close a window. 您不必以这种方式实现它,但是我已经实现了它,这样,当WebChat收到一个名为open的事件活动时,它将打开一个窗口,而当它收到名为close的事件活动时,将关闭一个窗口。 It even keeps track of multiple windows so you can choose which window to close. 它甚至可以跟踪多个窗口,因此您可以选择关闭哪个窗口。

I've set up a bot that sends open and close events when the user types "open [url]" or "close [url]". 我设置了一个当用户键入“ open [url]”或“ close [url]”时发送打开和关闭事件的机器人。 The bot code looks like this: 机器人代码如下所示:

var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

var text = activity.Text;
var words = text.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
var firstWord = words.FirstOrDefault().ToLower();
var secondWord = words.Length > 1 ? words[1] : "https://stackoverflow.com/";
Activity reply = null;

switch (firstWord)
{
    case "open":
    case "close":
        reply = activity.CreateReply();
        reply.Type = ActivityTypes.Event;
        reply.Name = firstWord;
        reply.Value = secondWord;
        break;
    default:
        reply = activity.CreateReply("Try \"open ...\" or \"close ...\"");
        break;
}

await connector.Conversations.SendToConversationAsync(reply);

Hopefully you can use this information and modify it to suit your needs. 希望您可以使用此信息并对其进行修改以适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM