Microsoft Bot Framework - how to send responses from a 3rd party API

231
July 28, 2017, at 08:37 AM

Using Microsoft Bot Framework, how can proxy an API between incoming and outgoing bot messages?

I've replaced:

server.post('/api/messages', connector.listen());

with my own implementation that takes req.body.text and sends it to a separate endpoint.

How can I send that endpoints response back to the chat?

server.post('/api/messages', (req, res, next) => {
  request.post('endpoint', {
    json: {"text": req.body.text},
  }, (error, response, body) => {
    // how to send body as an outgoing chat message?
  })
})

Update:

To point out why Ezequiel Jadib's answer won't work, I've added the complete code. req is not defined within the bot's callback function.

const restify = require('restify')
const builder = require('botbuilder')
const request = require('request')
// Setup Restify Server
const server = restify.createServer()
server.use(restify.plugins.bodyParser())
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url) 
})
// Create chat connector for communicating with the Bot Framework Service
const connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
})
server.post('/api/messages', connector.listen())
const bot = new builder.UniversalBot(connector, function (session) {
  request.post('endpoint', {
    json: {"text": req.body.text}
  }, (error, response, body) => {
    session.send(body)
  })
})
Answer 1

You can intercept messages using the middleware functionality of the Bot Builder Node.js SDK.

From the article Develop with Node.js > Messages > Intercept Messages:

The middleware functionality in the Bot Builder SDK enables your bot to intercept all messages that are exchanged between user and bot. For each message that is intercepted, you may choose to do things such as save the message to a data store that you specify, which creates a conversation log, or inspect the message in some way and take whatever action your code specifies.

You can define the middleware function that handles the intercepted message by configuring the bot.use() method.

Example:

bot.use({
    botbuilder: function (session, next) {
        // this function handles incoming messages sent to your bot
        next();
    },
    send: function (event, next) {
        // this function handles outgoing messages to your user(s)
        next();
    }
});

For reference, you can find a working example bot that demonstrates the middleware functionality here: https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/capability-middlewareLogging

Answer 2

First, I think you should go through the documentation for Node.js as this question seems to be related to the foundations of the SDK.

Then, I think that you are calling to your endpoint in the wrong place. Instead of doing it on the post, you should do it in the function of the UniversalBot that is basically where the user messages will be received.

You can try something like:

var bot = new builder.UniversalBot(connector, function (session) {
    request.post('endpoint', {
      json: {"text": session.message.text},
    }, (error, response, body) => {
        session.send("You said: %s", "your endpoint response");
   })
});
READ ALSO
Is there a Node JS equivalent to Bash exec?

Is there a Node JS equivalent to Bash exec?

There are times when I want to exit a NodeJS script and hand over all control of the terminal to another program just like Bash's exec commandThis behavior is shown below using the simple t

327
How do I use request.js to send text as the request body?

How do I use request.js to send text as the request body?

I am attempting to use the requestjs library for Node to send a post request with plain text as the body, but I'm finding that the server I'm sending to is receiving an empty body, or perhaps a body consisting of an empty object (logging the body yields...

217
Automated deployment of Node web application to cloud machine

Automated deployment of Node web application to cloud machine

I'm brand new to CI/CD and need some guidance on how to deploy a small web application to a cloud machine

158
p5.js canvas drawing path with ellipses

p5.js canvas drawing path with ellipses

I am working on a real time canvas drawing webapp using socketio, node

285