创建一个 HTTP POST 到 Twilio 函数(发送短信)
Create a HTTP POST to Twilio Functions (send SMS)
我创建了一个 Twilio 函数,我想用它向通过我的频道获得的应用程序订阅者发送我的会员推荐 link。
它适用于静态到/从数字,但是我想使 "to" 字段成为一个动态变量,可以通过 HTTP/Webhook POST 当Zapier 检测到我的 Mailchimp 邮件列表的新订阅者并将他们的 phone 号码作为变量传递。
我也不清楚我需要做什么来验证正在制作 POST 的客户端 (Zapier),因为我不希望向世界开放该功能,如果可以分享任何见解的话对此,我们将不胜感激 - 我是一个非常缺乏经验的程序员,正在努力快速学习!
@philnash - 感谢您的建议,慢慢实施!
非常感谢!
exports.handler = function(context, event, callback) {
const appCodes = ['code1', 'code2', 'code3', 'code4']
var smsBody = refCode ();
function refCode () {
return appCodes[Math.floor((Math.random() * appCodes.length))];
};
context.getTwilioClient().messages.create({
to: '+11112223333', // How do I make this dynamic from HTTP/Zapier Webhook POST???
from: '+1444555666',
body: `Get the App: ${smsBody}`
}).then(msg => {
callback(null, msg.sid);
}).catch(err => callback(err));
}
这里是 Twilio 开发人员布道者。
我假设 Zapier webhook 正在发送详细信息,包括 phone 号码,作为 POST 请求的正文。
请求正文中的所有参数都出现在传递给您的处理程序的 event
对象上。您可能想要 运行 打印出 event
对象的内容的测试,以查看您通过了什么。你可以这样做:
exports.handler = function(context, event, callback) {
for (let key in event) {
console.log(`${key}: ${event[key]}`);
}
// ... rest of the function
}
然后,当您找出存储号码的参数时,您可以在调用中使用它来创建消息。
如果有帮助请告诉我。
试试这个:
exports.handler = function(context, event, callback) {
for (let key in event) {
console.log(`${key}: ${event[key]}`);
}
// ... rest of the function
callback(null, 'complete');
};
感谢大家的宝贵意见,由衷感谢!我能够使用以下代码解决此问题:
exports.handler = function(context, event, callback) {
const appCodes = ['code1', 'code2', 'code3', 'code4']
var smsBody = refCode ();
var subNum = event.primaryPhone || 'There is no subscriber number'; // primaryPhone sent via HTTP post to twilio function
function refCode () {
return appCodes[Math.floor((Math.random() * appCodes.length))];
};
context.getTwilioClient().messages.create({
to: `${subNum}`, // parameters & values recieved from HTTP POST are available within the twilio functions "event" context
from: '+1444555666',
body: `Get the App: ${smsBody}`
}).then(msg => {
callback(null, msg.sid);
}).catch(err => callback(err));
}
我创建了一个 Twilio 函数,我想用它向通过我的频道获得的应用程序订阅者发送我的会员推荐 link。
它适用于静态到/从数字,但是我想使 "to" 字段成为一个动态变量,可以通过 HTTP/Webhook POST 当Zapier 检测到我的 Mailchimp 邮件列表的新订阅者并将他们的 phone 号码作为变量传递。
我也不清楚我需要做什么来验证正在制作 POST 的客户端 (Zapier),因为我不希望向世界开放该功能,如果可以分享任何见解的话对此,我们将不胜感激 - 我是一个非常缺乏经验的程序员,正在努力快速学习!
@philnash - 感谢您的建议,慢慢实施!
非常感谢!
exports.handler = function(context, event, callback) {
const appCodes = ['code1', 'code2', 'code3', 'code4']
var smsBody = refCode ();
function refCode () {
return appCodes[Math.floor((Math.random() * appCodes.length))];
};
context.getTwilioClient().messages.create({
to: '+11112223333', // How do I make this dynamic from HTTP/Zapier Webhook POST???
from: '+1444555666',
body: `Get the App: ${smsBody}`
}).then(msg => {
callback(null, msg.sid);
}).catch(err => callback(err));
}
这里是 Twilio 开发人员布道者。
我假设 Zapier webhook 正在发送详细信息,包括 phone 号码,作为 POST 请求的正文。
请求正文中的所有参数都出现在传递给您的处理程序的 event
对象上。您可能想要 运行 打印出 event
对象的内容的测试,以查看您通过了什么。你可以这样做:
exports.handler = function(context, event, callback) {
for (let key in event) {
console.log(`${key}: ${event[key]}`);
}
// ... rest of the function
}
然后,当您找出存储号码的参数时,您可以在调用中使用它来创建消息。
如果有帮助请告诉我。
试试这个:
exports.handler = function(context, event, callback) {
for (let key in event) {
console.log(`${key}: ${event[key]}`);
}
// ... rest of the function
callback(null, 'complete');
};
感谢大家的宝贵意见,由衷感谢!我能够使用以下代码解决此问题:
exports.handler = function(context, event, callback) {
const appCodes = ['code1', 'code2', 'code3', 'code4']
var smsBody = refCode ();
var subNum = event.primaryPhone || 'There is no subscriber number'; // primaryPhone sent via HTTP post to twilio function
function refCode () {
return appCodes[Math.floor((Math.random() * appCodes.length))];
};
context.getTwilioClient().messages.create({
to: `${subNum}`, // parameters & values recieved from HTTP POST are available within the twilio functions "event" context
from: '+1444555666',
body: `Get the App: ${smsBody}`
}).then(msg => {
callback(null, msg.sid);
}).catch(err => callback(err));
}