简体   繁体   English

如何在 Node.js 中使用 Twilio 代理

[英]How to use Twilio Proxy in Node.js

I've been trying for 3 days to properly setup a backend that will connect two phones anonymously for voice calling, with the help of Twilio.在 Twilio 的帮助下,我已经尝试了 3 天来正确设置一个后端,该后端将匿名连接两部手机进行语音通话。 This is what I'm trying to do:这就是我想要做的:

1

I've watched the live coding example and it doesn't work, at all.我看过现场编码示例,但它根本不起作用。

  1. I've installed the gem using `gem install twilio-ruby -v 5.25.0'我已经使用 `gem install twilio-ruby -v 5.25.0' 安装了 gem
  2. Entered the REPL进入REPL
  3. And started to follow the video:并开始关注视频:

$ irb $ irb

require 'twilio-ruby' client = Twilio::REST::Client('...', '...') client.service.create(friendly_name: 'Test 1 ') NoMethodError: undefined method `service' for # require 'twilio-ruby' client = Twilio::REST::Client('...', '...') client.service.create(friendly_name: 'Test 1 ') NoMethodError: undefined method `service' for #

Ok ok, the video is from 2 years ago, it's fair that the SDK changed.好吧好吧,视频是2年前的,SDK改了是公平的。 So I followed the interactive tutorial that is available in the website, this one.所以我遵循了网站上提供的交互式教程,这个教程。

Before continuing... there is a dedicated page about the new Proxy feature, and in one of the sections there is the following image:在继续之前......有一个关于新代理功能的专用页面,在其中一个部分中有下图:

在此处输入图片说明

This is clearly Python, so here I go... I've installed the Python SDK, started a REPL and tried to follow the code... it doesn't work.这显然是 Python,所以我开始了……我已经安装了 Python SDK,启动了 REPL 并尝试遵循代码……它不起作用。 Damn Twilio, update your things!该死的 Twilio,更新你的东西! (Or am I the stupid here?)... (还是我是这里的傻瓜?)...

Anyway... let's jump into the node.js tutorial , I'm sure at least the interative tutorial is working (so I thought).无论如何...让我们进入node.js 教程,我确定至少交互式教程正在运行(所以我想)。

So, I went to my console and created a service there, then I wrote the code:所以,我去了我的控制台并在那里创建了一个服务,然后我写了代码:

import twilio from 'twilio'

const client = twilio('accountSid', 'authToken')

async function test() {
  // create session
  const session = await client.proxy.services('<MY_SERVICE>')
            .sessions
            .create({uniqueName: 'MyFirstSession'})

  // add participant 1 to session
  client.proxy.services('<MY_SERVICE>')
            .sessions(session.sid)
            .participants
            .create({ friendlyName: 'Me', identifier: '<my_number>'})

  // add participant 2 to session
  client.proxy.services('<MY_SERVICE>')
            .sessions(session.sid)
            .participants
            .create({ friendlyName: 'Me 2', identifier: '<my_number_2>'})
}

test()

Then what?然后呢?

The docs say:文档说:

If your Twilio Phone Numbers are voice capable, you're now ready for a proxied voice conversation. Following the names from the previous steps, get Alice to make a call to her Proxy Identifier number. Twilio's Proxy service will then make a call from Bob's Proxy Number to his real number and connect the two calls.

It doesn't work... I've tried to call to the proxy identifier number, and it doesn't work.它不起作用......我试图调用代理标识符号,但它不起作用。

I'm not sure what to do anymore, I've tried to reach out to them, nobody replies...我不知道该怎么办了,我试图与他们联系,但没有人回复......

I looked at the "node.js tutorial" and I can see how that can be confusing.我查看了“node.js 教程”,我可以看到它是如何令人困惑的。

Here is what you can do to get this working and get a better understanding of how it works:您可以采取以下措施来使其工作并更好地了解其工作原理:


Prerequisites:先决条件:

  • You have a Twilio account and a Twilio purchased phone number.您有一个 Twilio 帐户和一个 Twilio 购买的电话号码。 This Twilio phone number is going to be the "proxy" number.这个 Twilio 电话号码将成为“代理”号码。
  • You have two other phone numbers from where you can make or receive calls (or texts), these are your phone numbers.您还有另外两个电话号码,您可以从中拨打或接听电话(或短信),这些是您的电话号码。


Step 1. 第1步。

  • login into your Twilio account and make note of your "ACCOUNT SID" and "AUTH TOKEN", they are on the "Dashboard".登录您的 Twilio 帐户并记下您的“帐户 SID”和“授权令牌”,它们位于“仪表板”上。 ( https://www.twilio.com/console ) ( https://www.twilio.com/console )
  • go to the phone numbers page ( https://www.twilio.com/console/phone-numbers/incoming ) and verify that you have a phone number there.转到电话号码页面 ( https://www.twilio.com/console/phone-numbers/incoming ) 并确认您在那里有电话号码。 It's okay if you just make note of the number because in this guide I'll tell you how to configure the main things via the console, not via Node.js code.如果您只记下数字也没关系,因为在本指南中,我将告诉您如何通过控制台配置主要内容,而不是通过 Node.js 代码。

Step 2.第2步。

  • still at Twilio, go to the "Proxy Dashboard" ( https://www.twilio.com/console/proxy ) and here "Add New Proxy Service" (there is a plus red round sign).仍然在 Twilio,转到“代理仪表板”( https://www.twilio.com/console/proxy )和这里“添加新代理服务”(有一个红色的加号圆形标志)。 Give it a name, click "Create" then on the next page, you don't need to fill anything, just scroll down and click on "Save"给它一个名字,点击“创建”然后在下一页,你不需要填写任何东西,只需向下滚动并点击“保存”
  • now you have a service which shows on the "Services" page ( https://www.twilio.com/console/proxy/services )现在您有一项显示在“服务”页面上的服务 ( https://www.twilio.com/console/proxy/services )
  • take a note of the service SID (KSxxxxx...)记下服务 SID (KSxxxxx...)

Step 3.第 3 步。

  • while still on the "Services" page ( https://www.twilio.com/console/proxy/services ), to the right side you can see "PROXY NUMBERS", it's a link, click on it to tell the service to use your Twilio number for this "Proxy Service" you just created.仍在“服务”页面( https://www.twilio.com/console/proxy/services )上时,在右侧您可以看到“PROXY NUMBERS”,这是一个链接,单击它可以告诉服务将您的 Twilio 号码用于您刚刚创建的“代理服务”。
  • on the next page you'll see "This Service has no Proxy Numbers" and a button "Add Numbers", click on it and "Assign" your Twilio number.在下一页上,您将看到“此服务没有代理号码”和一个“添加号码”按钮,单击它并“分配”您的 Twilio 号码。
  • click "Close" after you see the message that the number has been successfully assigned.看到号码分配成功的提示后,点击“关闭”。

Step 4.第四步。

  • now you need to create a session for this service, go back to "Proxy Dashboard" ( https://www.twilio.com/console/proxy ) and you'll see your service listed现在您需要为此服务创建一个会话,返回“代理仪表板”( https://www.twilio.com/console/proxy ),您将看到您的服务已列出
  • to the right again, there is a link for "SESSIONS", click on it再次向右,有一个“SESSIONS”链接,点击它
  • you'll see the message "You have no Sessions" and a button to "Create new Session", click on it and enter a name, you can select also the "MODE" (text, voice, or both)您将看到“您没有会话”消息和“创建新会话”按钮,单击它并输入名称,您还可以选择“模式”(文本、语音或两者)
  • click on "Create" then scroll down and click on "Save"单击“创建”,然后向下滚动并单击“保存”
  • make note of the session SID (KCxxxxx...)记下会话 SID (KCxxxxx...)
  • now you have a service and a session现在你有一个服务和一个会话

Step 5.第 5 步。

This is the step where you add the participants, which is about your two phone numbers.这是您添加参与者的步骤,这是关于您的两个电话号码。 This part can't be done on the Twilio's console as far as I know.据我所知,这部分无法在 Twilio 的控制台上完成。 I did it running Node.js code.我在运行 Node.js 代码时做到了。

You need to have Node.js installed on your computer ( https://nodejs.org/en/ ), then:您需要在您的计算机上安装 Node.js ( https://nodejs.org/en/ ),然后:

  • create a new folder新建一个文件夹
  • in Terminal, change directory on the new folder在终端中,更改新文件夹上的目录
  • run npm init --yes运行npm init --yes
  • run npm install twilio运行npm install twilio
  • create a new file index.js创建一个新文件index.js

here is the code for index.js这是 index.js 的代码

const accountSid = 'ACxxxxx...'; const authToken = 'xxxxx...'; const client = require('twilio')(accountSid, authToken); client.proxy.services('KSxxxxx...') .sessions('KCxxxxx...') .participants .create({ friendlyName: 'Alice', identifier: '+19997775555' }) .then(participant => console.log(participant.proxyIdentifier));

In the code above, replace the values for在上面的代码中,替换值

  • accountSid // your Twilio account sid accountSid // 你的 Twilio 账户 sid
  • authToken // your Twilio auth token authToken // 您的 Twilio 身份验证令牌
  • KSxxxxx... // the service id KSxxxxx... // 服务id
  • KCxxxxx... // the session id KCxxxxx... // 会话 ID
  • +19997775555 // your first phone number +19997775555 // 您的第一个电话号码

Next run this code to add the first participant, in the Terminal run node index.js接下来运行此代码以添加第一个参与者,在终端运行node index.js

Last thing is to add the second participant, in the code above change the Alice to let's say Bob and also replace your first phone number with your second phone number, then run node index.js again.最后一件事是添加第二个参与者,在上面的代码中,将Alice更改为Bob ,并将your first phone number替换为您的第二个电话号码,然后再次运行node index.js


After you added the second participant, you're ready.添加第二个参与者后,您就准备好了。 Now you can try to call or text your Twilio phone number (which acts as a proxy) from any of your numbers (phones) and the other of your numbers (phones) will receive a text or ring.现在,您可以尝试从您的任何号码(电话)拨打您的 Twilio 电话号码(充当代理)或发送短信,而您的其他号码(电话)将收到短信或铃声。


Note: the things that you did on the Twilio console, can be done with Node.js code, read the tutorial again and try to understand the steps and how the code works. 注意:您在 Twilio 控制台上所做的事情,可以使用 Node.js 代码完成,再次阅读教程并尝试理解步骤以及代码的工作原理。 If you need more help you can always ask another more specific question here. 如果您需要更多帮助,您可以随时在这里提出另一个更具体的问题。

Good Luck!祝你好运!

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

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