所以,我对此并不是很熟悉所以我有点困惑。我试图使用Twilio函数创建一个函数,将传入的短信息发布到第三方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)
})
}
答案 0 :(得分:1)
这是让你入门的东西,Twilio函数的代码。这将在Help Scout上创建一个新对话。
注意:event.Body
和event.From
等。event
参数包含有关Twilio函数特定调用的信息。
替换为auth
,mailbox 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