首页 > 解决方案 > Twilio Classic Functions,您如何使用标签和 SMS?

问题描述

Twilio 功能问题:标签未按预期工作。

我正在尝试为向我的号码发送“keyword1”、“keyword2”、“keyword3”等短信的用户创建唯一的短信订阅列表。

我通过一个经典函数拦截“传入消息”事件来做到这一点。

我想轮询关键字,然后在订阅列表时将标签分配给用户。

然后,当我广播 SMS 时,我希望能够只发送给那些用我的关键字标记的用户。

例如,要捕获关键字“test”并将标签“test”分配给用户,我使用下面的代码。

然后向所有带有“test”标签的用户发送消息,我使用了“sendtest”命令。

我可以广播到“所有”标签,它会正常工作,但如果我只想发送给带有标签 ['test'] 的用户,那么不会报告错误并且系统告诉我它是成功的,但没有订阅者会接收任何消息。

我想知道我尝试定义标签的方式是否有问题?看起来数据格式应该是某种 STRING[] 数组,我猜是 ['test','two','three']。(如果我能确认这是正确的)。但是我注意到根据 twilio 提供的工作示例,如果我将通知参数设置为字符串 IE:tags: 'all',那么此语法适用于广播到所有标签。但是,其他任何事情都将不起作用。

是否有一些技巧可以让标签工作,或者在尝试通过经典功能界面过滤通知时它们根本不起作用?

class TestCommand extends Command {
  run(callback) {
    // Create a new SMS Notify binding for this user's phone number
    //and try to tag the user with keyword 'test'
    notify.bindings.create({
      identity: this.fromNumber,
      bindingType: 'sms',
      address: this.fromNumber,
      tags: ['test']
    }).then((response) => {
      callback(null, 'test Message success')
    }).catch(err => {
      callback(err, 'test message fail')
    })
  }
}

class BroadcastTestCommand extends Command {
  run(callback) {
    // Check if sender is in list of admins, stored in the system environment
    // as a comma-separated string
    if (adminNumbers.indexOf(this.fromNumber) < 0) {
      return callback(null, 'broadcast Not Authorized')
    }

    // Create a new SMS Notify binding for this user's phone number
    //only notify users who are tagged with 'test'
    notify.notifications.create({
      tag: ['test'],
      body: this.commandText
    }).then((response) => {
      callback(null, 'broadcast test Success')
    }).catch(err => {
      console.log(err)
      callback(err, 'broadcast test Fail')
    })
  }
}

// Handle incoming SMS commands ####################
exports.handler = (context, event, callback) => {
  // Get command text from incoming SMS body
  let cmd = event.Body || ''
  cmd = cmd.trim().split(' ')[0].toLowerCase()

  // Default to help command
  let cmdInstance = new HelpCommand(event, context)

  // Choose other commands as appropriate
  switch(cmd) {
    case 'test': cmdInstance = new TestCommand(event, context); break;
    case 'sendtest': cmdInstance = new BroadcastTestCommand(event, context); break;

  }

  // Execute command
  cmdInstance.run((err, message) => {
    let twiml = new twilio.twiml.MessagingResponse()
    if (err) {
      console.log(err)
      message = 'There was a problem with your request. Try again!'
    }
    twiml.message(message)
    callback(null, twiml)
  })
}

标签: bindingnotificationstagstwiliosms

解决方案


好吧,其他任何人都在这个问题上拉扯头发。.这里关于绑定的 Twilio 文档会让你失望 https://www.twilio.com/docs/notify/api/binding-resource

因为参数不是“标签”,而是“标签”

这有效...

class TestCommand extends Command {
  run(callback) {
    // Create a new SMS Notify binding for this user's phone number
    //and try to tag the user with keyword 'test'
    notify.bindings.create({
      identity: this.fromNumber,
      bindingType: 'sms',
      address: this.fromNumber,
      tag: ['test']
    }).then((response) => {
      callback(null, 'test Message success')
    }).catch(err => {
      callback(err, 'test message fail')
    })
  }
}

推荐阅读