Вызов внешнего REST API от бота - PullRequest
0 голосов
/ 16 мая 2018

Я пытаюсь вызвать API отдыха, принимая аргумент в качестве входного параметра от пользователя, а затем получая некоторую примерную информацию из веб-службы на основе отдыха. Может ли кто-нибудь помочь?

1 Ответ

0 голосов
/ 16 мая 2018

Вы можете сделать это:

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 для того же самого.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...