简体   繁体   English

如何使用 Nodejs 在 Twilio 中发送短信?

[英]How to send SMS masive in Twilio with Nodejs?

I would like to know how I can send a message to different numbers.我想知道如何向不同的号码发送消息。 I mean, send SMS as notifications to different numbers in the same String Array.我的意思是,将短信作为通知发送到同一字符串数组中的不同号码。 Something like:就像是:

body: "Hello Word!"
number:["+2222", "+2222", "+2222"]

Is it possible to do this with twilio?可以用 twilio 做到这一点吗?

It should be possible, if it is possible with mail, how is it done with telephone numbers?应该是可以的,如果用邮件可以,电话号码怎么做?

I am using nodeJs and had something like:我正在使用 nodeJs 并且有类似的东西:

updated code更新代码

    const sendBulkMessages = async(req, res) => {
    let messageBody = req.body;
    let numberList = req.body;
    var numbers = [];
    for (i = 0; i < numberList.length; i++) {
        numbers.push(JSON.stringify({
            binding_type: 'sms',
            address: numberList[i]
        }))
    }


    const notificationOpts = {
        toBinding: numbers,
        body: messageBody,
    };

    const response = await client.notify
        .services(SERVICE_SID)
        .notifications.create(notificationOpts)
        .then(notification => console.log(notification.sid))
        .catch(error => console.log(error));

    console.log(response);

    res.json({
        msg: 'Mensaje enviado correctamente'
    });
}

But it tells me an error that I did not send the body, when clearly I do.但它告诉我一个错误,我没有发送身体,而我显然发送了。

Someone could help me?有人可以帮助我吗? Please

It looks like you are trying to send these messages via a post request to an Express application.看起来您正试图通过发布请求将这些消息发送到 Express 应用程序。 The issue is that you are not extracting the data from the body of the request correctly.问题是您没有正确地从请求正文中提取数据。

If your POST request body is a JSON object that looks like this:如果您的POST请求正文是如下所示的 JSON 对象:

{
  "toBinding": ['+222', '+222'],
  "body": 'Hello'
}

Then you will need to make sure you are parsing the body of the request as JSON, normally by adding the Express body parser JSON middleware , and then extract the data from the request body like this:然后,您需要确保将请求的主体解析为 JSON,通常通过添加Express 主体解析器 JSON 中间件,然后从请求主体中提取数据,如下所示:

let messageBody = req.body.body;
let numberList = req.body.toBinding;

Here's the full script:这是完整的脚本:

const sendBulkMessages = async(req, res) => {
    let messageBody = req.body.body;
    let numberList = req.body.toBinding;
    var numbers = [];
    for (i = 0; i < numberList.length; i++) {
        numbers.push(JSON.stringify({
            binding_type: 'sms',
            address: numberList[i]
        }))
    }


    const notificationOpts = {
        toBinding: numbers,
        body: messageBody,
    };

    const response = await client.notify
        .services(SERVICE_SID)
        .notifications.create(notificationOpts)
        .then(notification => console.log(notification.sid))
        .catch(error => console.log(error));

    console.log(response);

    res.json({
        msg: 'Mensaje enviado correctamente'
    });
}

Having inspected your repo , it appears that your function was actually correct, but the way you were exporting and requiring functions wasn't.检查您的 repo后,您的功能似乎实际上是正确的,但您导出和要求功能的方式却不是。

You can't export functions like this:您不能像这样导出函数:

 module.exports = sendMessage, sendMessageWhatsapp, sendBulkMessages;

That only exports the first function, the others are ignored.那只导出第一个函数,其他的被忽略。 So I updated it to this:所以我将其更新为:

module.exports = { sendMessage, sendMessageWhatsapp, sendBulkMessages };

This exports an object containing the three functions.这将导出一个包含三个函数的对象。 Then, when you require the functions, you can't do this:然后,当您需要这些功能时,您不能这样做:

const sendMessage = require('./message');
const sendMessageWhatsapp = require('./message');
const sendBulkMessages = require('./message');

That requires the same function three times.这需要相同的功能三次。 With the update above, you can now do this:通过上面的更新,您现在可以执行此操作:

const {
  sendMessage,
  sendBulkMessages,
  sendMessageWhatsapp,
} = require("./message");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM