如何通过Create Participant API和Outbound Call API实现多客户同时振铃
多号码并行呼叫并接入会议的解决方案
需求背景
需要同时呼叫多个坐席/客户,将首个接听者接入会议。当前使用Create Participant API和Outbound Call API均为逐个拨打,已尝试的TwiML多<Number>标签、循环调用Participant API方案存在系统开销大、无法大规模并行振铃的问题。
问题1:是否有推荐方案通过Create Participant API为多个号码发起并行呼叫?
有,核心是并行异步调用Create Participant API,而非串行循环。利用多线程或异步任务框架同时触发多个API请求,每个请求对应一个目标号码,并指定同一个会议SID,即可实现真正的并行振铃。
示例(Python + asyncio):
import asyncio import aiohttp TWILIO_ACCOUNT_SID = "你的账户SID" TWILIO_AUTH_TOKEN = "你的认证Token" CONFERENCE_SID = "目标会议SID" TARGET_NUMBERS = ["+1234567890", "+0987654321"] async def create_participant(session, number): url = f"https://api.twilio.com/2010-04-01/Accounts/{TWILIO_ACCOUNT_SID}/Conferences/{CONFERENCE_SID}/Participants.json" data = { "From": "你的Twilio号码", "To": number, "StatusCallback": "你的状态回调URL" } async with session.post(url, auth=(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN), data=data) as resp: return await resp.json() async def main(): async with aiohttp.ClientSession() as session: tasks = [create_participant(session, num) for num in TARGET_NUMBERS] await asyncio.gather(*tasks) if __name__ == "__main__": asyncio.run(main())
问题2:能否利用Outbound Call API同时发起多个呼叫并接入同一会议?
可以,类似SIP分叉的效果。并行发起多个Outbound Call请求,每个请求返回的TwiML都指向同一个会议ID,所有呼叫会同时振铃,首个接听者直接进入会议。
示例(Node.js):
const twilio = require('twilio'); const client = twilio('你的账户SID', '你的认证Token'); const CONFERENCE_ID = '统一会议ID'; const TARGET_NUMBERS = ['+1234567890', '+0987654321']; const FROM_NUMBER = '你的Twilio号码'; // 生成TwiML,指定接入目标会议 const getConferenceTwiML = () => { return `<Response><Dial><Conference>${CONFERENCE_ID}</Conference></Dial></Response>`; }; // 并行发起多个呼叫 Promise.all( TARGET_NUMBERS.map(number => client.calls.create({ from: FROM_NUMBER, to: number, twiml: getConferenceTwiML() }) ) ).then(calls => { console.log('所有呼叫已发起:', calls.map(c => c.sid)); }).catch(err => { console.error('呼叫发起失败:', err); });
问题3:首个参与者接入时取消其他振铃呼叫的最佳实践?
核心是通过Webhook监听会议参与者接入事件,触发后主动挂断剩余振铃呼叫:
- 记录所有发起的呼叫SID(可存储在Redis/数据库中)
- 配置会议的
StatusCallback,监听participant.connected事件 - 收到事件后,遍历未完成的呼叫SID,调用Twilio Calls API更新呼叫状态为挂断
示例(Python Flask 处理Webhook + 挂断呼叫):
from flask import Flask, request import requests app = Flask(__name__) TWILIO_ACCOUNT_SID = "你的账户SID" TWILIO_AUTH_TOKEN = "你的认证Token" # 假设已存储所有发起的呼叫SID,这里用模拟数据 ACTIVE_CALL_SIDS = ["CAxxxxxx1", "CAxxxxxx2", "CAxxxxxx3"] @app.route('/conference-webhook', methods=['POST']) def conference_webhook(): event_type = request.form.get('EventType') if event_type == 'participant.connected': # 遍历所有活跃呼叫,挂断振铃中的 for call_sid in ACTIVE_CALL_SIDS: # 先查询呼叫当前状态 status_url = f"https://api.twilio.com/2010-04-01/Accounts/{TWILIO_ACCOUNT_SID}/Calls/{call_sid}.json" resp = requests.get(status_url, auth=(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)) call_status = resp.json().get('status') if call_status == 'ringing': # 发起挂断请求 hangup_url = f"https://api.twilio.com/2010-04-01/Accounts/{TWILIO_ACCOUNT_SID}/Calls/{call_sid}.json" requests.post(hangup_url, auth=(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN), data={'Twiml': '<Response><Hangup/></Response>'}) # 清空活跃呼叫记录 ACTIVE_CALL_SIDS.clear() return '', 200 if __name__ == '__main__': app.run(port=5000)
内容的提问来源于stack exchange,提问作者Wasim Akram
相关产品推荐
相关产品推荐

