Microsoft Bot Framework - 无法发送消息并结束对话

时间:2016-04-02 19:01:32

标签: javascript node.js exception bots botframework

我开始使用MS的Bot Frameword,我正在尝试构建一个简单的机器人。

问题在于,如果我在对话框中发送消息,我就无法结束该对话框。

这是我的代码:

我的机器人的入口点文件

var restify = require('restify');
var builder = require('botbuilder');
require('dotenv').load();
var fs = require('fs');

//Project modules
var consts = require('./modules/consts');
var u = require('./modules/utils');
var ds = require('./modules/dialogs');

if (process.env.config == consts.PRODENV) {
    u.dlog('Production environment; loading HTTPS');
    var server = restify.createServer({
        key: fs.readFileSync(process.env.keyPath || './ssh/key'),
        certificate: fs.readFileSync(process.env.certPath || './ssh/cert')
    });
} else {
    u.dlog('Development environment; loading HTTP');
    var server = restify.createServer();
}

/*Definindo variáveis*/
//Lista de comandos
var commands = {
    evento: {
        pattern: 'evento',
        action: builder.DialogAction.beginDialog('evento')
    },
    eventos: {
        pattern: 'eventos',
        action: builder.DialogAction.beginDialog('eventos')
    },
    convidados: {
        pattern: 'convidados',
        action: builder.DialogAction.beginDialog('convidados')
    },
    cancelar: {
        pattern: 'cancelar',
        action: builder.DialogAction.endDialog()
    }
}

//Objeto de bot principal
var bot = new builder.BotConnectorBot();
//Roteador de comandos
var cm = new builder.CommandDialog();

//Adicionando comandos
for (command in commands) {
    cm.matches("/?%s(.*)".replace('%s', commands[command].pattern), commands[command].action);
}

//Diálogo inicial
cm.onDefault(function(session) {
    session.send('Hey there!');
});

//Rota padrão
bot.add('/', cm);

//Adicionando diálogos
for (dialog in ds) {
    bot.add(dialog, ds[dialog].flow);
}

//Inicializando servidor
if (process.env.config == consts.PRODENV) {
    server.use(bot.verifyBotFramework({ appId: 'events-organizer-bot', appSecret: process.env.appSecret }));
}
server.post(process.env.uri || '/', bot.listen());
server.listen(process.env.port || 8080, function() {
    u.dif(function() {
        console.log('%s listening to %s', server.name, server.url);
    })
})

dialogs.js

module.exports = {
    "evento": {
        flow: function(session) {
            session.send('*evento*: implementando');
            session.endDialog();
        }
    },
    "eventos": {
        flow: function(session) {
            session.send('*eventos*: implementando');
            session.endDialog();
        }
    },
    "convidados": {
        flow: function(session) {
            session.send('*convidados*: implementando');
            session.endDialog();
        }
    }
}

我从该代码获得的异常:

Session Error: builder is not defined

我知道我可以在不启动对话框的情况下发送消息,但这些对话框会变得更复杂(当然)。如果我想确认用户发送到对话框然后结束它的信息怎么办?

-----------------------------编辑1 ---------------- -------------

我在更新后重新编写代码,但我仍然遇到异常。

以下是代码:

切入点:

var restify = require('restify');
var builder = require('botbuilder');
require('dotenv').load();
var fs = require('fs');

//Project modules
var consts = require('./modules/consts');
var u = require('./modules/utils');
var ds = require('./modules/dialogs');

if (process.env.config == consts.PRODENV) {
    u.dlog('Production environment; loading HTTPS');
    var server = restify.createServer({
        key: fs.readFileSync(process.env.keyPath || './ssh/key'),
        certificate: fs.readFileSync(process.env.certPath || './ssh/cert')
    });
} else {
    u.dlog('Development environment; loading HTTP');
    var server = restify.createServer();
}

/*Definindo variáveis*/
//Lista de comandos
var commands = {
    evento: {
        pattern: 'evento',
        action: builder.DialogAction.beginDialog('evento')
    },
    eventos: {
        pattern: 'eventos',
        action: builder.DialogAction.beginDialog('eventos')
    },
    convidados: {
        pattern: 'convidados',
        action: builder.DialogAction.beginDialog('convidados')
    },
    cancelar: {
        pattern: 'cancelar',
        action: builder.DialogAction.endDialog()
    }
}

//Objeto de bot principal
var bot = new builder.BotConnectorBot();
//Roteador de comandos
var cm = new builder.CommandDialog();

//Adicionando comandos
for (command in commands) {
    cm.matches("/?%s(.*)".replace('%s', commands[command].pattern), commands[command].action);
}

//Diálogo inicial
cm.onDefault(function(session) {
    session.send('Hey there!');
});

//Rota padrão
bot.add('/', cm);

//Adicionando diálogos
for (dialog in ds) {
    bot.add(dialog, ds[dialog].flow);
}

//Inicializando servidor
if (process.env.config == consts.PRODENV) {
    server.use(bot.verifyBotFramework({ appId: 'events-organizer-bot', appSecret: process.env.appSecret }));
}
server.post(process.env.uri || '/', bot.listen());
server.listen(process.env.port || 8080, function() {
    u.dif(function() {
        console.log('%s listening to %s', server.name, server.url);
    })
})

dialogs.js

module.exports = {
    "evento": {
        flow: function(session) {
            //session.send('*evento*: implementando');
            session.endDialog('*evento*: implementando');
        }
    },
    "eventos": {
        flow: function(session) {
            //session.send('*eventos*: implementando');
            session.endDialog('*eventos*: implementando');
        }
    },
    "convidados": {
        flow: function(session) {
            //session.send('*convidados*: implementando');
            session.endDialog('*convidados*: implementando');
        }
    }
}

调用默认对话框很好,但调用任何其他对话框会在我的脸上引发Session Error: Maximum call stack size exceeded个删除。

如果我没有将任何参数传递给enDialog方法,那么没有什么不对。

1 个答案:

答案 0 :(得分:0)

很抱歉延迟...我不知道你为什么会得到那个特殊的例外但是现在有一个问题与发送邮件有关,然后无法在同一时间结束对话呼叫。我有一个修复/改进,允许您作为调用endDialog()的一部分传递消息。让我尝试推出一个解决此问题的0.7.0更新。