简体   繁体   English

如何在 Twilio 对话中发送消息

[英]How to send a message in a Twilio Conversation

How do you send a new message to a Twilio Conversation in node.js?如何向 node.js 中的 Twilio 对话发送新消息?

I found this sample code from Twilio, but I don't know how to get messagingServiceSid for my 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));

Two options:两种选择:

1) Graphical via the Twilio Console 1)通过 Twilio 控制台图形化

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

2) Programmatically via the Twilio Rest API 2)以编程方式通过 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 .您还需要将电话号码与您的消息服务相关联,您可以通过PhoneNumber Resource执行此操作。 Note that the API is currently in Public Beta.请注意,API 目前处于公开测试阶段。

Separate answer for the Conversations API :对话 API的单独答案:

First create a conversation and note down the conversation SID (starts with CH , here outputted with console.log(conversation.sid) ):首先创建一个对话并记下对话 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));

Then add an SMS participant to a Conversation (this is where you need your SMS-enabled Twilio number and the recipient number).然后将 SMS 参与者添加到对话(这是您需要启用 SMS 的 Twilio 号码和收件人号码的地方)。

Now you can use the Conversation Message Resource to create messages which will be received by all participants of the conversation.现在,您可以使用对话消息资源来创建将由对话的所有参与者接收的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM