Search code examples
node.jstwiliotwilio-functions

Twilio Functions - posting to third party api?


So, I am not very familiar with this so I am a little confused. I'm trying to use Twilio Functions to create a function that posts an incoming sms message to a third-party API. In general, how would I go about that?

This is what i have right now

exports.handler = function(context, event, callback) {
  var got = require('got');
  var data = event.Body;
  console.log("posting to helpscout: "+requestPayload);
  got.post('https://api.helpscout.net/v1/conversations.json', 
    {
      body: JSON.stringify(data), 
      'auth': {
        'user': process.env.API_KEY,
        'pass': 'x'
      },
      headers: {
        'Content-Type': 'application/json' 
      }, 
      json: true
    })
    .then(function(response) {
     console.log(response.body)
     callback(null, response.body);
    })
    .catch(function(error) {
      callback(error)
    })
}

Solution

  • Here is something to get you started (the code for the Twilio function). This will create a new conversation at Help Scout.

    Note: The event parameter contains information about the specific invocation of the Twilio function (an incoming sms message). It has things like event.Body, event.From, etc.


    const https = require('https');
    
    exports.handler = function(context, event, callback) {
    
        let twiml = new Twilio.twiml.MessagingResponse();
        twiml.message("Thanks. Your message has been forwarded to Help Scout.");
    
        let postData = JSON.stringify(
            {
                "type": "email",
                "customer": {
                    "email": "[email protected]"
                },
                "subject": "SMS message from " + String(event.From),
                "mailbox": {
                    "id": "000000"
                },
                "status": "active",
                "createdAt": "2017-08-21T12:34:12Z",
                "threads": [
                    {
                        "type": "customer",
                        "createdBy": {
                            "email": "[email protected]",
                            "type": "customer"
                        },
                        "body": String(event.Body),
                        "status": "active",
                        "createdAt": "2017-08-21T12:34:12Z"
                    }
                ]
            }
        );
    
    
        // replace with your Help Scout values
        let postOptions = {
            host: 'api.helpscout.net',
            port: '443',
            path: '/v1/conversations.json',
            method: 'POST',
            auth: '1234567890abcdef:X',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': Buffer.byteLength(postData)
            }
        };
    
        let req = https.request(postOptions, function(res) {
            res.setEncoding('utf8');
            res.on('data', function(chunk) {
                console.log(chunk);
                callback(null, twiml);
            });
        });
    
        req.write(postData);
        req.end();
    
    };
    

    Docs: https://www.twilio.com/blog/2017/05/introducing-twilio-functions.html