0

How do you send a new message to a Twilio Conversation in node.js?

I found this sample code from Twilio, but I don't know how to get messagingServiceSid for my Conversation.

// Download the helper library from https://www.twilio.com/docs/node/install
// Your Account Sid and Auth Token from twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'Revenge of the Sith was clearly the best of the prequel trilogy.',
     messagingServiceSid: 'MG9752274e9e519418a7406176694466fa',
     to: '+441632960675'
   })
  .then(message => console.log(message.sid));
Berry Blue
  • 15,330
  • 18
  • 62
  • 113
  • Just now seeing this after I've answered... do you want to use the [Conversations API](https://www.twilio.com/docs/conversations/quickstart) or [Messaging Services](https://www.twilio.com/docs/messaging/services)? In your question you say the former but your code shows the latter. Those are different Twilio offerings. – yvesonline Mar 22 '21 at 21:59
  • Is it possible to send a message using the Conversations API in node.js? I thought the example above was for that. – Berry Blue Mar 22 '21 at 22:23
  • Yes the different Twilio offerings can be sometimes confusing! To your question: Yes you can use the Conversations API with Node.js. I've added another example because it's such a different question. In essence you 1) create the conversation 2) add participants and 3) create messages. Just out of curiosity, any reason why you need to use the Conversations API? Conversations API has its strengths across channels with multiple participants. – yvesonline Mar 22 '21 at 23:40

2 Answers2

1

Two options:

1) Graphical via the Twilio Console

Twilio Console with Messaging Services menu

2) Programmatically via the Twilio Rest API

The Service Resource gets you started. You would also need to associate phone numbers with your messaging service, you can do this via the PhoneNumber Resource. Note that the API is currently in Public Beta.

yvesonline
  • 4,609
  • 2
  • 21
  • 32
1

Separate answer for the Conversations API:

First create a conversation and note down the conversation SID (starts with CH, here outputted with console.log(conversation.sid)):

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

client.conversations.conversations
                    .create({friendlyName: 'My First Conversation'})
                    .then(conversation => console.log(conversation.sid));

Then add an SMS participant to a Conversation (this is where you need your SMS-enabled Twilio number and the recipient number).

Now you can use the Conversation Message Resource to create messages which will be received by all participants of the conversation.

yvesonline
  • 4,609
  • 2
  • 21
  • 32