简体   繁体   English

Twilio 电话会议

[英]Twilio Conference Call

I was able to implement basic Voice Conferencing but I feel my implementation may be lacking.我能够实施基本的语音会议,但我觉得我的实施可能有所欠缺。

client = Client('ACxxxxxxxx', '34xxxxxxxxx')

@app.route('/', methods=["GET", "POST"])
def home():
    form = ConferenceList() #form made using flask-wtf
    if form.validate_on_submit():
        contact_1 = form.data['contact_1'] #callee1
        contact_2 = form.data['contact_2'] #callee2
        from_ = form.data['from_'] #caller

        response = VoiceResponse()
        with Dial() as dial:
            if from_ == MODERATOR:
                dial.conference(
                   'Conf',
                   start_conference_on_enter=True,
                   end_conference_on_exit=True
                   )
            else:
                dial.conference('Conf', start_conference_on_enter=False)
        response.append(dial)

        '''here I feel could be a bottleneck'''
        add_user(contact_1, conference_name='Conf', label='laed#1')
        add_user(contact_2, conference_name='Conf', label="consumer")
        return Response(str(response), 'text/xml')
    
    return render_template('hello.html', form=form)


def add_user(contact, conference_name, label):
    participant = client.conferences(conference_name).\
            participants.create(
                label=label, #label for participant
                beep='onEnter',
                record=True,
                from_='from_', #same as above
                to=str(contact)
            )

if __name__ == '__main__':
    app.run(debug=True, port=8000)

Basically the submit button triggers the / endpoint and the conference starts.基本上,提交按钮触发/端点,会议开始。

I feel there could be an issue with this implementation as I plan on cleaning it up and pushing to production (salesperson can make a conference call to leads on the app).我觉得此实施可能存在问题,因为我计划对其进行清理并推向生产(销售人员可以通过电话会议与应用程序上的潜在客户进行通话)。 Is there something I could have done better?有什么我可以做得更好的吗?

The voice(one client-one client) utilizes the Twilio Voice SDK, it there a way I could tweak it for conferencing?语音(一个客户端-一个客户端)使用 Twilio 语音 SDK,有没有办法调整它以进行会议?

You only need to respond with TwiML to a webhook from Twilio. In this case it appears that you are responding with TwiML to your application's front end when a submit button is pressed.您只需要使用 TwiML 响应来自 Twilio 的 webhook。在这种情况下,当按下提交按钮时,您似乎正在使用 TwiML 响应应用程序的前端。

So, you can drop all the TwiML:因此,您可以删除所有 TwiML:

@app.route('/', methods=["GET", "POST"])
def home():
    form = ConferenceList() #form made using flask-wtf
    if form.validate_on_submit():
        contact_1 = form.data['contact_1'] #callee1
        contact_2 = form.data['contact_2'] #callee2

        '''here I feel could be a bottleneck'''
        add_user(contact_1, conference_name='Conf', label='laed#1')
        add_user(contact_2, conference_name='Conf', label="consumer")
        return "whatever"

Since the settings you are trying to apply in the TwiML do not apply to the participants joining the conference, you need to adjust your add_user function to use them.由于您尝试在 TwiML 中应用的设置不适用于参加会议的参与者,因此您需要调整您的add_user function 以使用它们。 In this case, the startConferenceOnEnter and endConferenceOnExit parameters for the moderator should be sent when you create the participant.在这种情况下,主持人的startConferenceOnEnterendConferenceOnExit参数应该在您创建参与者时发送。 It might be easier to write two methods, add_user and add_moderator , to make things clear:编写两个方法add_useradd_moderator可能更容易使事情变得清楚:

def add_user(contact, conference_name, label):
    participant = client.conferences(conference_name).\
            participants.create(
                label=label, #label for participant
                beep='onEnter',
                record=True,
                from_='from_', #same as above
                to=str(contact),
                start_conference_on_enter=False
            )

def add_moderator(contact, conference_name, label):
    participant = client.conferences(conference_name).\
        participants.create(
            label=label,
            beep='onEnter',
            record=True,
            from_='from_',
            to=str(contact),
            start_conference_on_enter=True,
            end_conference_on_exit=True
        )

Then call different functions for the different participants:然后为不同的参与者调用不同的函数:

        add_moderator(contact_1, conference_name='Conf', label='laed#1')
        add_user(contact_2, conference_name='Conf', label="consumer")

When you call add_user or add_moderator it will make an API request and slow down your server response.当您调用add_useradd_moderator时,它会发出 API 请求并减慢您的服务器响应速度。 If you wanted to offload those requests to a worker, that would make your response quicker.如果您想将这些请求卸载给工作人员,那将使您的响应更快。 But for 2 API requests, it is likely not a problem.但是对于 2 API 请求,这可能不是问题。

One other thing you might want to consider is the consumer experience.您可能要考虑的另一件事是消费者体验。 If they answer the phone before your agent does, then they will be greeted with hold music.如果他们在您的代理接听电话之前接听电话,那么他们将收到等待音乐。 You might want to architect it so that the application calls the agent first and only once they have picked up it then dials the consumer.您可能希望对其进行架构,以便应用程序首先调用代理,并且仅在代理接听后才呼叫消费者。 Just worth considering.只是值得考虑。

Edit编辑

After further explanation, you are now telling me that you want to make the call from the browser using the Twilio Voice SDK for JS .经过进一步解释,你现在告诉我你想使用Twilio Voice SDK for JS从浏览器拨打电话。

To make outbound calls with the JS SDK you need to create an access token which includes an outgoing application sid, which refers to a TwiML application.要使用 JS SDK 进行出站调用,您需要创建一个访问令牌,其中包含一个传出应用程序 sid,它指的是一个 TwiML 应用程序。 That TwiML application has a voice URL. When you place the call with the SDK, Twilio makes a webhook request to the voice URL of your TwiML app.该 TwiML 应用程序的语音为 URL。当您使用 SDK 拨打电话时,Twilio 会向您的 TwiML 应用程序的语音 URL 发出 webhook 请求。 Your application can perform actions and return TwiML to tell Twilio what to do with the call.您的应用程序可以执行操作并返回 TwiML 以告知 Twilio 如何处理该呼叫。

When you create the call with the JS SDK you can pass parameters to the call.当您使用 JS SDK 创建调用时,您可以将参数传递给调用。

const device = new Device(token);

const call = await device.connect({
  params: {
    To: ["+15551234567", "+145557654321"]
  }
});

Those parameters are sent with the webhook request to your TwiML App voice URL. You can then use a response very similar to your original code to respond here, because you need to return TwiML to the request from Twilio, and start calls to the other participants in the call.这些参数与 webhook 请求一起发送到您的 TwiML App 语音 URL。然后您可以使用与您的原始代码非常相似的响应在此处进行响应,因为您需要将 TwiML 返回到来自 Twilio 的请求,并开始呼叫其他参与者在通话中。

@app.route('/conference', methods=["POST"])
def conference():
    const numbers = request.form["To"]

    response = VoiceResponse()
    with Dial() as dial:
        dial.conference(
           'Conf',
           start_conference_on_enter=True,
           end_conference_on_exit=True
       )
    response.append(dial)

    for number in numbers:
        add_user(number, conference_name='Conf')

    return Response(str(response), 'text/xml')

This code receives the To parameter, a list of numbers to dial into this conference, builds the TwiML response that will put the browser caller into a conference call, places outbound calls to the numbers to dial them into the conference and then returns the TwiML to Twilio. The dialler in the browser will start the conference and the other participants will arrive in the conference when they answer the phone.此代码接收To参数,即要拨入此会议的号码列表,构建 TwiML 响应,将浏览器呼叫者置于电话会议中,对号码进行出站呼叫以将其拨入会议,然后将 TwiML 返回到Twilio。浏览器中的拨号器将开始会议,其他与会者接听电话时将到达会议。

In this case you don't make the request to your server yourself, you use the JS SDK to trigger the call and let Twilio make the request to your server.在这种情况下,您不会自己向您的服务器发出请求,而是使用 JS SDK 触发调用并让 Twilio 向您的服务器发出请求。 As mentioned in the comments, you may want to offload the API calls to create participants to a background job so that you can respond to the webhook request quicker, but that is beyond the scope of this answer.如评论中所述,您可能希望将创建参与者的 API 调用卸载到后台作业,以便您可以更快地响应 webhook 请求,但这超出了此答案的 scope。

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

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