如何在 Twilio 对话中发送消息

How to send a message in a Twilio Conversation

如何向 node.js 中的 Twilio 对话发送新消息?

我从 Twilio 找到了这个示例代码,但我不知道如何为我的对话获取 messagingServiceSid

// 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));

两个选项:

1) 图形通过 Twilio 控制台

2) 以编程方式通过 Twilio Rest API

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。请注意,API 目前处于 Public 测试版。

Conversations API 的单独答案:

先创建一个对话,记下对话的SID(以CH开头,这里以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));

然后 add an SMS participant to a Conversation(这是您需要启用 SMS 的 Twilio 号码和收件人号码的地方)。

现在您可以使用 Conversation Message Resource 创建消息,对话的所有参与者都会收到这些消息。