【问题标题】:Twilio send sms with nodejs not sending messageTwilio用nodejs发送短信不发送消息
【发布时间】:2021-10-06 09:59:04
【问题描述】:

我正在使用 twilio 使用 nodejs 发送短信,但有时它不工作,不去捕捉或不去然后,在它调用之上,客户端也创建但创建不工作。

 const accountSid = globals.twilioAccountSid; 
     const authToken = globals.authToken; 
     const client = require('twilio')(accountSid, authToken); 
         
    client.messages 
      .create({ 
        body: params.message,
        messagingServiceSid: globals.messagingServiceSid,      
        to: params.country_code+params.phone, 
      }) 
      .then(message => console.log(message),error => console.log('error'))
      .catch(e => { console.error('Got an error:', e.code, e.message); })


      .done();

【问题讨论】:

  • 你能分享来自 Twilio 控制台的 SMS 日志吗?另外,如何静态添加messagingServiceSid 来创建请求,因为它是在成功创建后从 Twilio 生成的。
  • 这里是 Twilio 开发人员传道者。我也对您在 Twilio 的 SMS 日志中看到的内容感兴趣。你能分享一下这段代码的上下文吗?您在 thencatch 中使用了两个回调,这是否也会导致在查看错误时出现问题?
  • 在 twilio 控制台中没有错误或成功日志,但我发现了问题,对于这个发送短信,我有一个函数并且我正在调用它,之后调用了主回调而不处理对此的响应发送短信功能,所以它直接在发送短信功能调用后给出回调,这就是为什么client.messages .create有时没有调用,所以我处理它现在正常工作。
  • 所以我们可以关闭这个问题,谢谢大家的支持。

标签: node.js express twilio twilio-api


【解决方案1】:

就我看到您的代码而言,我认为您在创建请求时遇到了一些问题。

  • 从创建请求中删除 messageserviceSid
  • 在您的创建请求中添加 From 参数

请遵循 twilio 文档中的此创建请求。

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
client.messages
  .create({
     body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
     from: '+15017122661',
     to: '+15558675310'
   })
  .then(message => console.log(message.sid));

查看此链接了解更多详情twilio documentation

【讨论】:

  • 消息服务代表一个数字池(和其他功能),可以在类似这样的 API 请求中代替from
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 2015-08-08
  • 1970-01-01
相关资源
最近更新 更多