0

如何在 node.js 中向 Twilio Conversation 发送新消息?

我从 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));
4

2 回答 2

1

对话 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));

然后将 SMS 参与者添加到对话(这是您需要启用 SMS 的 Twilio 号码和收件人号码的地方)。

现在,您可以使用对话消息资源来创建将由对话的所有参与者接收的消息。

于 2021-03-22T23:38:20.690 回答
1

两种选择:

1) 通过 Twilio 控制台进行图形化

带有消息服务菜单的 Twilio 控制台

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

服务资源让您开始。您还需要将电话号码与您的消息服务相关联,您可以通过PhoneNumber Resource执行此操作。请注意,该 API 目前处于公开测试阶段。

于 2021-03-22T21:57:02.453 回答