0

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();
Sahil
  • 7
  • 4
  • Can you share the SMS logs from Twilio console? Also how can you add `messagingServiceSid` statically to create request as it's generated from Twilio after successful creation. – sohail amar Oct 06 '21 at 14:21
  • Twilio developer evangelist here. I'm also interested in what you see in the SMS logs in Twilio. Can you share the context around this code too? You're using two callbacks in the `then` and a `catch`, could that be causing an issue in seeing the errors too? – philnash Oct 07 '21 at 00:43
  • In twilio console there are no error or success logs, but i find the issue, for this send sms i have a function and i am calling it, after that main callback was callng without handling response for this send sms function , so it directly give callback after the send sms function calling and that's why client.messages .create was not calling sometimes, so i handle it and now working properly. – Sahil Oct 07 '21 at 06:50
  • So we can close this question, thank you guys for your support. – Sahil Oct 07 '21 at 06:51

2 Answers2

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));
Sandeep Sherpur
  • 2,418
  • 25
  • 27
-1

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

sohail amar
  • 381
  • 2
  • 14
  • A messaging service represents a pool of numbers (and other features) and can be used in place of `from` in API requests like this. – philnash Oct 07 '21 at 00:40