Suhel
Suhel

Reputation: 1

Schedule whatsapp messages on twilio

I want to schedule a whatsapp message to send it the users at a specific time. Users are from worldwide.

So lets condiser, I want to send a whatsapp message on monday at 2 PM of their time, as users are from different countries, we need to consider their 2PM.

So, I can do that, is twilio have any kind of features ?

Upvotes: 0

Views: 160

Answers (1)

IObert
IObert

Reputation: 3851

Yes, you can schedule messages with Twilio. You'll need to specify the UTC time when the message should be delivered as Twilio won't be able to know in which time zone the user lives.

The Message Scheduling feature currently requires that this time is more than one hour and less than 7 days ahead.

// create a Twilio client
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

async function sendScheduledSms() {
  // schedule message to be sent 61 minutes after current time
  const sendWhen = new Date(new Date().getTime() + 61 * 60000);

  // send the SMS
  const messagingServiceSid = process.env.TWILIO_MESSAGING_SERVICE_SID;
  const message = await client.messages.create({
    from: messagingServiceSid,
    to: '+1xxxxxxxxxx',  // ← your phone number here
    body: 'Friendly reminder that you have an appointment with us next week.',
    scheduleType: 'fixed',
    sendAt: sendWhen.toISOString(),
  });

  console.log(message.sid);
}

sendScheduledSms();

Upvotes: 0

Related Questions