3

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)
    })
}
Alex Baban
  • 11,312
  • 4
  • 30
  • 44

1 Answers1

2

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": "customer@example.com"
            },
            "subject": "SMS message from " + String(event.From),
            "mailbox": {
                "id": "000000"
            },
            "status": "active",
            "createdAt": "2017-08-21T12:34:12Z",
            "threads": [
                {
                    "type": "customer",
                    "createdBy": {
                        "email": "customer@example.com",
                        "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

Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • Would it be possible to create a conversation from two messages? For example, when a customer sends a message, I want Twilio to auto-respond to ask for their email. Once the customer responds with that information, I would like the function to create a conversation combining the information from the two messages. Is that possible? –  Aug 21 '17 at 17:46
  • I'm not familiar with Help Scout API, but maybe updating a conversation or threads after you get the conversation id searching by phone number. If you need persistence, for example phone number matched to email, then Twilio functions might not be enough. – Alex Baban Aug 21 '17 at 17:57
  • Maybe that could work but I'm not sure either. Probably something I have to play with. Thanks for your help though! –  Aug 21 '17 at 18:08
  • I adapted this slightly to make POSTs to my own homemade API. Had to change the `Content-Type` header to `application/x-www-form-urlencoded`, then it worked. Thank you!! – Joe Coyle Nov 07 '19 at 21:01