node.js - Twilio Functions - posting to third party api? -
so, not familiar little confused. i'm trying use twilio functions create function posts incoming sms message third-party api. in general, how go that?
this have right now
exports.handler = function(context, event, callback) { var got = require('got'); var data = event.body; console.log("posting 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) }) }
here started, code twilio function. create new conversation @ scout.
note: event.body
, event.from
, etc. event
parameter contains information specific invocation of twilio function.
replace values auth
, mailbox id
, etc.
const https = require('https'); exports.handler = function(context, event, callback) { let twiml = new twilio.twiml.messagingresponse(); twiml.message("thanks. message has been forwarded scout."); let postdata = json.stringify( { "type": "email", "customer": { "email": "customer@example.com" }, "subject": "sms message " + 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" } ] } ); 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(); };
https://www.twilio.com/blog/2017/05/introducing-twilio-functions.html
wiki
Comments
Post a Comment