我试图通过从用户获取参数作为输入参数来调用休息API,然后从基于休息的Web服务获取一些示例信息。任何人都可以帮忙吗?
答案 0 :(得分:1)
你可以这样做:
bot.dialog('postApiCallDialog', [
(session) => {builder.Prompts.text(session, "First text input from the user")},
(session, results) => {
session.dialogData.firstInput = results.response
builder.Prompts.number(session, "Second is the number input from the user")
},
(session, results) => {
session.dialogData.secondInput = results.response
// make the api call here with the inputs received from the user
// below example is for a post call
request.post('apiEndpoint', {
'auth': {
'user': 'abc',
'pass': 'xyz',
'sendImmediately': false
},
'json': {
input1: session.dialogData.firstInput,
input2: session.dialogData.secondInput
}
}, (error, response, body) => {
var data = JSON.parse(body);
// do stuff with data
// use session.send / session.endDialog
})
}
]).triggerAction({matches: 'postApiCall' })
如果您需要拨打电话:
request.put('apiEndpoint', {
'auth': {
'user': 'abc',
'pass': 'xyz',
'sendImmediately': false
}, 'json': {field1: session.dialogData.firstInput, field2: "zzz"} // change the value of field1 to the first input received from the user
}, function (error, response, body) {
// do stuff
})
获取
request.get(`apiEndpoint${session.dialogData.firstInput}`, { // some situation where your url is dependent on the input received from user
'auth': {
'user': 'abc',
'pass': 'xyz',
'sendImmediately': false
}
}, function (error, response, body) {
// do stuff
})
上述所有API调用中的授权可能因您的情况而异。您可以查看nodejs请求库的文档。