首页 > 解决方案 > Twilio 函数 - 传入参数并格式化 SMS 正文以包含参数

问题描述

我正在使用 Twilio 创建一个“出勤热线”,员工可以在其中提供有关他们缺勤原因的信息,然后 Twilio 将向主管和人力资源发送单独的、精心策划的消息。

为此,我在 Twilio Studio 中创建了一个 Flow,我想使用 Twilio 函数来处理发送大量 SMS 消息,通知用户新缺席。

我将参数传递给函数,如名称、部门、班次、原因等,目的是通过 SMS 共享这些值。

我很难将所有这些不同的值正确地放入邮件正文中。

exports.handler = function(context, event, callback) {

 // Create a reference to the user notification service

 const client = context.getTwilioClient();

 const service = client.notify.services(
   context.TWILIO_NOTIFICATION_SERVICE_SID
 );


 const notification = {
   toBinding: JSON.stringify({
    binding_type: 'sms', address: '+1XXXXXXXXXX',
    binding_type: 'sms', address: '+1XXXXXXXXXX',

  }),

  body: 'New Attendance Notification',
        event.question_name,
        event.question_dept,
        event.question_reason,
        event.contactChannelAddress,


 };

 console.log(notification);

 // Send a notification
 return service.notifications.create(notification).then((message) => {
   console.log('Notification Message',message);
   callback(null, "Message sent.");
 }).catch((error) => {
   console.log(error);
   callback(error,null);
 });
};

现在我知道上面消息的“正文”不起作用,但我有点迷茫......

下面的文字是我希望我的 SMS 消息在发送时如何读出。

New Attendance Notification
Name: event.Name
Dept: event.Dept
Reason: event.Reason
Contact: event.ContactChannelAddress

我想要完成的事情是否可能?

标签: twiliotwilio-functionstwilio-studio

解决方案


像这样的东西...

exports.handler = function(context, event, callback) {

 // Create a reference to the user notification service

 const client = context.getTwilioClient();

 const service = client.notify.services(
    context.TWILIO_NOTIFICATION_SERVICE_SID
 );


 const bindings = [
    '{ "binding_type": "sms", "address": "+14071234567" }',
    '{ "binding_type": "sms", "address": "+18021234567" }'
    ];

 const notice = `New Attendance Notification\nName: ${event.question_name}\nDept: ${event.question_dept} \nReason: ${event.question_reason}\nContact: ${event.contactChannelAddress} \n`;

 // Send a notification
  service.notifications.create({ toBinding: bindings, body: notice }).then((message) => {
    console.log('Notification Message',message);
    callback(null, "Message sent.");
  })
  .catch((error) => {
   console.log(error);
   callback(error,null);
 });
};

推荐阅读