'Twilio send sms with nodejs not sending message
I am using twilio to send sms using nodejs, but sometimes it not working, not going to catch or not going to then, above it calling, client also created but create not working.
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();
Solution 1:[1]
Just console error not 'error'
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));
Solution 2:[2]
As far as I have seen your code I think you have some issues in you create request.
- Remove messageserviceSid from create request
- Add From param in your create request
Please follow this create request from twilio documentation.
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));
check this link for more details twilio documentation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | sohail amar |