【问题标题】:Unable to know what is wrong with my bot无法知道我的机器人出了什么问题
【发布时间】:2017-04-23 09:25:52
【问题描述】:

我一直在阅读机器人框架教程并制作了以下代码。

var builder = require('botbuilder');
var restify = require('restify');
//Create Connector
var connector = new builder.ChatConnector(
    {
        appId: process.env.MICROSOFT_APP_ID,
        appPassword: process.env.MICROSOFT_APP_PASSWORD
    }
);

//Create bot with Connector
var bot = new builder.UniversalBot(connector);



//Restify
//Create a server

var server = restify.createServer();

//listen to server
server.listen(process.env.port || process.env.PORT || 33333, function () {
    console.log('%s listening to URL %s', server.name, server.url);
});

server.post('/api/messages', connector.listen());

bot.dialog('/', [function (session) {
    session.beginDialog('/ensureProfile', session.userData.profile);
}, function (session, result) {
    session.userData.profile = result.response;
    console.log(result.response);
    session.send("I'm %(name) and my org is %(company)", session.userData.profile);
}]);


bot.dialog('/ensureProfile', [function (session, args, next) {
    session.dialogData.profile = args || {};
    if (!session.dialogData.profile.name) {
        builder.Prompts.text(session, "enter you name");
    }
    else {
        next();
    }
}, function (session, result, next) {
    if (result.response) {
        session.dialogData.profile.name = result.response;
        console.log(result.response + " is user name");
    }
    if (!session.dialogData.profile.company) {
        builder.Prompts.text(session, "Enter your company");
    }
    else {
        next();
    }
},
function (session, result) {
    if (result.response) {
        session.dialogData.profile.company = result.response;
        console.log(result.response + " is user name");
    }
    session.endDialogWithResult({ response: session.dialogData.profile });
}
]);

当我运行此程序时,机器人会按预期询问我的姓名和公司。我输入了详细信息,但最后它给了我以下错误。

session.sendBatch() sending 1 messages
SyntaxError: [sprintf] unexpected placeholder
    at Function.sprintf.parse (C:\Users\user\Desktop\Node Tutorial\node_modules\sprintf-js\src\sprintf.js:164
:23)
    at sprintf (C:\Users\user\Desktop\Node Tutorial\node_modules\sprintf-js\src\sprintf.js:19:34)
    at Object.vsprintf (C:\Users\user\Desktop\Node Tutorial\node_modules\sprintf-js\src\sprintf.js:174:24)
    at fmtText (C:\Users\user\Desktop\Node Tutorial\node_modules\botbuilder\lib\Message.js:261:46)
    at Message.text (C:\Users\user\Desktop\Node Tutorial\node_modules\botbuilder\lib\Message.js:49:33)
    at Session.createMessage (C:\Users\user\Desktop\Node Tutorial\node_modules\botbuilder\lib\Session.js:401:
36)
    at Session.send (C:\Users\user\Desktop\Node Tutorial\node_modules\botbuilder\lib\Session.js:144:26)
    at Array.<anonymous> (C:\Users\user\Desktop\Node Tutorial\sample.js:33:13)
    at SimpleDialog.waterfallAction [as fn] (C:\Users\user\Desktop\Node Tutorial\node_modules\botbuilder\lib\
dialogs\DialogAction.js:117:32)
    at SimpleDialog.dialogResumed (C:\Users\user\Desktop\Node Tutorial\node_modules\botbuilder\lib\dialogs\Si
mpleDialog.js:21:14)

我尝试使用 console.log 打印 json 并且响应正确,给了我这样的回复

name : userName
company : userCompany

请让我知道我哪里出错了,我该如何解决这个问题。这非常令人困惑。

谢谢

【问题讨论】:

    标签: node.js bots botframework


    【解决方案1】:

    根据sprintf docs 中的示例,您似乎必须使用类型说明符,即应该用%(key)s 代替%(key)

    session.send("I'm %(name)s and my org is %(company)s", session.userData.profile)
    

    【讨论】:

    • 你的意思是像我们使用%s,应该是`%(yourStringName)s'?
    • @Rakesh,是的,完全正确
    • 非常感谢。你救了我的一天。 :) 再次感谢
    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    相关资源
    最近更新 更多