我一直在浏览机器人框架教程,并制作了以下代码。
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
请让我知道我哪里出错了,我该如何解决这个问题。这非常令人困惑。
由于
答案 0 :(得分:3)
根据sprintf docs中的示例,您似乎必须使用类型说明符,即%(key)
而不是%(key)s
:
session.send("I'm %(name)s and my org is %(company)s", session.userData.profile)