O'Reilly logo

Building Bots with Node.js by Madan Bhintade, Eduardo Freitas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Receiving SMS bot logic

So far we've implemented the basic bare-bones template for our Twilio bot app and also made all the necessary configurations in order to have our solution wired up with Twilio and also easily deployable to Azure.

Let's now explore how we can make our bot reply to incoming messages. In order to do this, we'll need to have a POST endpoint on our Node/Express app. Let's examine the following code:

app.post('/receive', function (req, res) { 
  var twiml = new twilio.TwimlResponse(); 
  twiml.message('Hi, this is TwilioBot'); 
 
  res.writeHead(200, {'Content-Type': 'text/xml'}); 
  res.end(twiml.toString()); 
}); 

We can see here that in order to reply, we create a TwiML response and send that as the response of POST / receive the HTTP endpoint. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required