我在使用Microsoft的node.js botframework时遇到问题。
基本上,我想创建一个openURL()卡,这样当用户点击按钮时,除了在浏览器中打开URL之外,它还会触发另一个功能。
例如,在我的情况下,一旦点击openURL()卡上的按钮,URL将打开,并且将触发一系列session.send()。
我尝试一起使用 builder.CardAction.dialogAction()和 bot.beginDialogAction(),但我意识到我无法在没有交互的情况下打开网址用户,在新功能中。
答案 0 :(得分:2)
我尝试一起使用builder.CardAction.dialogAction()和bot.beginDialogAction(),但我意识到在新功能中没有来自用户的交互我无法打开URL。
您在这里使用builder.CardAction.dialogAction
和bot.beginDialogAction
是正确的,然后为避免与用户交互以打开网址,我们可以将网址作为参数发送到新对话框,例如:< / p>
var opn = require('opn');
//other relative codes...
bot.dialog('showCard', (session)=>{
var msg = new builder.Message(session);
msg.attachmentLayout(builder.AttachmentLayout.carousel);
msg.attachments([
new builder.HeroCard(session)
.title("Test 1")
.subtitle("This is test 1.")
.text("this is test 1.")
.buttons([
builder.CardAction.dialogAction(session, "openUrl", "http://www.google.com", "Test 1")
]),
new builder.HeroCard(session)
.title("Test 2")
.subtitle("This is test 2.")
.text("this is test 2.")
.buttons([
builder.CardAction.dialogAction(session, "openUrl", "http://www.bing.com", "Test 2")
])
]);
session.send(msg).endDialog();
}).triggerAction({
matches: /^show card$/i
});
bot.beginDialogAction('openUrl','/openUrl');
bot.dialog('/openUrl',(session, args)=>{
//your custom action goes here
session.send("get here");
//open the url, I used opn package here.
opn(args.data);
session.endDialog();
});
正如您所看到的,您可以在此处传递url作为参数,以便被调用的对话框直接设置为使用浏览器打开它。
答案 1 :(得分:0)
我不相信你可以直接在Bot Framework SDK中直接这样做,但你可以使用nodejs包OpenURL来实现这样的目的。
首先,您需要包含您的包裹:
var builder = require('botbuilder');
var ourl = require('openurl');
// other packages as necessary
然后,您需要设置行动:
// Set up the button we'll be putting on a card.
// This could be within another dialog, for example.
let urlButton = builder.CardAction
.dialogAction(session, "test", "something", "Test a URL");
let urlCard = new builder.HeroCard(session)
.text("Opens an URL")
.title("Demo")
.buttons([urlButton]);
let msg = new builder.Message(session).addAttachment(urlCard.toAttachment());
session.send(msg);
然后在这个例子中,您需要设置几个对话框。至少,我就这样做了:
let someMessages = (session) => {
session.send("This is one message.");
session.send("This is another message.");
session.send("A third message, to be sure..");
};
let openUrlDialog = bot.dialog("openSomeUrl", [
(session) => {
// A console message.
console.log("Fingers crossed!!!");
// Use OpenURL to open the desired url.
ourl.open("https://www.microsoft.com");
// Let's send our messages.
someMessages(session);
// End the dialog.
session.endDialog("This concludes the URL opening and messages test.");
}
]).triggerAction({matches: /test/gi});
let allElse = bot.dialog("/", (session, args) => {
if (session.message.text === "action?test=something") {
session.beginDialog("openSomeUrl");
return;
}
console.log("Default Dialog");
session.endDialog("Default Dialog was processed. Doublecheck.");
}, true); // replace itself in case default was already declared.
(如果不使用ES6 +,则将let
替换为var
s,将lambda语法替换为函数。