我正在测试编写机器人与我们的团队聊天工具和我们的票务系统进行交互的想法。我有一个基本的机器人,它将获取系统中任务记录的状态并作出回应。我将该调用设置为customAction。我遇到的问题是僵尸程序将在您第一次与它交互时工作,但第二次它会发送默认消息。我有什么想法吗?
var restify = require("restify");
var builder = require("botbuilder");
var getSnStatus = require("./getSnStatus");
var botAppInfo = require("./botStuffs.json");
//setup the Restify server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log('listening to %s', server.name, server.url);
});
//create chat connector using the ms bot framework
var connector = new builder.ChatConnector({
appId: botAppInfo.AppID,
appPassword: botAppInfo.password
});
//listen for messages from users
server.post('/api/messages', connector.listen());
// setup bot and Register in-memory storage
var inMemoryStorage = new builder.MemoryBotStorage();
var bot = new builder.UniversalBot(connector, [
function(session){
session.send("Hello, If you want to check the status of something just ask for the status or state with a number.");
}
]).set('storage', inMemoryStorage);
//look for the state or status word and run a state check if that's found
//anywhere in the text sent by the user.
//TODO handle a response with no results.
bot.customAction({
matches: /status|state/gi,
onSelectAction(session, args, next) {
words = session.message.text.split(" ");
for (i = 0; i < words.length; i++) {
if (words[i].match(/\d+/)) {
console.log("Run using the word: " + words[i]);
getSnStatus(words[i], function(state) {
state.result.forEach(function(element) {
session.send("The state of " + element.number +
" is: " + element.state);
});
session.endDialog();
})
}
}
}
})
我得到的输出是这样的:
致电:州INC1671479
回复:INC1671479的状态为:打开
致电:州INC1671479
回复:您好,如果您想查看某些内容的状态,只需询问状态或带号码的状态。
我希望它会在第二次通话时再次触发我的自定义操作。
以下是确定问题后的工作代码。
var restify = require("restify");
var builder = require("botbuilder");
var getSnStatus = require("./getSnStatus");
var botAppInfo = require("./botStuffs.json");
//setup the Restify server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log('listening to %s', server.name, server.url);
});
//create chat connector using the ms bot framework
var connector = new builder.ChatConnector({
appId: botAppInfo.AppID,
appPassword: botAppInfo.password
});
//listen for messages from users
server.post('/api/messages', connector.listen());
// setup bot and Register in-memory storage
var inMemoryStorage = new builder.MemoryBotStorage();
var bot = new builder.UniversalBot(connector, []).set('storage', inMemoryStorage);
bot.dialog("status", (session, args, next)=>{
words = session.message.text.split(" ");
for (i = 0; i < words.length; i++) {
if (words[i].match(/\d+/)) {
console.log("Run using the word: " + words[i]);
getSnStatus(words[i], function(state) {
state.result.forEach(function(element) {
session.send("The state of " + element.number +
" is: " + element.state);
});
})
}
}
}).triggerAction({matches: /status|state/gi});
答案 0 :(得分:0)
我遇到的问题是僵尸程序会在您第一次与它交互时工作,但第二次它会发送默认消息。我有什么想法吗?
我认为这种行为是设计的,根据官方文档:Bind a customAction,一旦操作完成,控制权就会传回到堆栈顶部的对话框,机器人可以继续。
因此,在您的代码中,customAction
第一次完成后,默认情况下会转到根对话框,当第二次用户输入&#34;状态&#34;时,此对话框将处理用户的输入。
使用customAction
是为了执行某些快速操作请求而不操纵对话框堆栈。因此,对于您的方案,我认为Bind a triggerAction更好,例如:
bot.dialog('status', (session, args, next)=>{
var message = session.message.text;
//your work to get status goes here.
}).triggerAction({matches: /status|state/gi});