Node.js 从bot调用外部restapi

Node.js 从bot调用外部restapi,node.js,rest,botframework,Node.js,Rest,Botframework,我试图调用rest API,方法是将参数作为用户的输入参数,然后从基于rest的web服务中获取一些示例信息。有人能帮忙吗?您可以这样做: bot.dialog('postApiCallDialog', [ (session) => {builder.Prompts.text(session, "First text input from the user")}, (session, results) => { session.dialogData.firstInpu

我试图调用rest API,方法是将参数作为用户的输入参数,然后从基于rest的web服务中获取一些示例信息。有人能帮忙吗?

您可以这样做:

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' })
如果您需要拨打put电话:

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请求库的文档以获取相同的信息。

“有人能帮忙吗?”>到目前为止您尝试了什么?Bot框架可以用多种语言创建。每种语言都有自己的方式来创建http消息。而不是考虑机器人如何调用RESTAPI。考虑如何在节点中调用API,或者如何在c#中调用API。很多关于谷歌搜索的信息,甚至搜索机器人用机器人框架构建的机器人只是一个web API。您可以像调用任何其他web API项目一样调用任何rest API。