在“摘要”(即最后一个if语句)的瀑布对话框的末尾,我想自动进行发布请求,而无需在Postman中进行API调用,eventListener是这样吗?如何包含它?
async summaryStep(step) {
if (step.result) {
// Get the current profile object from user state.
const userProfile = await this.userProfile.get(step.context, new UserProfile());
userProfile.name = step.values.name;
//the same for other step values(email, doctor, date)
let msg = `you want a date with dr. ${userProfile.doctor} , and your name is ${userProfile.name}.`;
if (userProfile.date !== -1) {
msg += `you have an appointment the: ${userProfile.date}.`;
}
await step.context.sendActivity(msg);
let msg1 = `"${userProfile.date}"`;
if (msg1) {
let z = JSON.stringify(userProfile.name);
//and also the other rows to go in the database(email, doctor, date)
var name = JSON.parse(z);
//and also the other rows to go in the database(email, doctor, date)
//this actually works but only if i use postman
var urlencoded = bodyparser.urlencoded({ extended: false });
app.post('/id', urlencoded, (req, res) => {
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
mysqlConnection.query("INSERT INTO users(name, email, doctor, date) VALUES('" + userProfile.name + "','" + userProfile.password + "','" + userProfile.doctor + "','" + userProfile.date + "')", function (err, result, rows) {
if (err) throw err;
console.log("Yeah! record inserted");
console.log(name);
res.send(result);
});
});
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Listening on port ${port}..`));
}
} else {
await step.context.sendActivity('Thanks. Your profile will not be kept. Push enter to return Menu');
}
return await step.prompt(CONFIRM_PROMPT3, `is that true? ${step.result}`, ['yes', 'no']);
// this if statement should "fire" the post request...
if (step.result == 'yes') {
return await step.context.sendActivity(`we will contact you soon ${userProfile.password}.`);
}
return await step.endDialog();
}
答案 0 :(得分:1)
根据我的理解,您想知道如何从Azure机器人异步功能调用POST API。请尝试使用您的async summaryStep
函数中的以下代码,根据您的要求发送发帖请求。
var rp = require('request-promise');
var options = {
method: 'POST',
uri: 'http://localhost:8080/id',
body: {
fieldCount:0,
affectedRows:1,
//your other body content here...
},
json: true,
headers: {
'content-type': 'application/json' //you can append other headers here
}
};
await rp(options)
.then(function (body) {
console.log(body)
})
.catch(function (err) {
console.log(err)
});
}
希望有帮助。 一种 nd如有任何其他担忧或误解,请随时让我知道。
答案 1 :(得分:1)
答案是将您的DataBinder.Eval(Container.DataItem, "rent_amount","{0:C0}")
API端点移动到您的机器人已在服务器上运行的app.post
文件中。只需旋转一个新的“服务器”和“端口”以使端点可用。然后,在您的index.js
(在我的示例中为summaryStep
)中,使用Axios,请求承诺或您拥有的东西进行API调用以发布数据。按下API后,数据将被传入并处理。
在下面的代码中,当API被命中时,传入的数据将用于发回机器人的axiosStep
中。在您的情况下,您传入的数据将用于数据库调用,您可以在其中使用返回的响应sendActivity
。
您的代码如下所示。请注意,为示例起见,sendActivity
操作已简化。您将需要更新post操作以进行mySql查询。此示例还利用了服务器的Restify(Bot Framework bot的标准),并使用了与bot相同的端口,但这可以轻松更新为使用Express和/或其他端口。
希望有帮助!
index.js
post
mainDialog.js
[...]
const conversationReferences = {};
const bodyParser = require('body-parser');
server.post('/id', async (req, res) => {
const { conversationID, data, name } = req.body;
const conversationReference = conversationReferences[ conversationID ];
await adapter.continueConversation(conversationReference, async turnContext => {
var reply = `${ data }. Thanks, ${ name }`;
await turnContext.sendActivity(reply);
});
res.writeHead(200);
res.end();
});