Twilio函数 – 张贴到第三方API?

所以,我对这个不是很熟悉,所以我有点困惑。 我试图使用Twilio函数来创build一个函数,传入一个短信到第三方API。 一般来说,我会怎么做呢?

这就是我现在所拥有的

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) }) } 

这里是让你开始,Twilio函数的代码。 这将在Help Scout上创build一个新的对话。

注意: event.Bodyevent.Fromevent参数包含有关Twilio函数特定调用的信息。

replace为您的authmailbox id等的值

 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" } ] } ); 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