Finally, I found a solution! I share it below. I hope this to be helpful for somebody... It's not that intuitive, but it works. I followed these steps:
- Create a CONFERENCE and join Customer (set startConferenceOnEnter and EndConferenceOnExit to false).
- Create a CALL and hook it to a URL. In that webhook create a TwiML to join Agent1 to conference, allowing hangupOnStar to later join Agent2 (in another webhook), and setting endConferenceOnExit to false, to avoid hanging up Customer's call.
- Create another webhook to re-join Agent1 after clicking star (*) and let Agent2 join the conference.
I am using Python with Flask framework. Here is the code:
@app.route('/start_conference.html', methods=['GET', 'POST'])
def start_conference():
agent1 = '+XXXXXXXXXXX' # Agent1's phone number
agent2 = '+XXXXXXXXXXX' # Agent2's phone number
confName = 'YourConferenceName'
resp = VoiceResponse()
dial = Dial()
# Create a conference and join Customer
dial.conference(
confName,
start_conference_on_enter=False,
end_conference_on_exit=False,
max_participants = 3 # Limits participants to 3
)
# Call to Agent1 and setup a webhook for this call with a TwiML
to join to the conference as Moderator
client.calls.create(
from_=twilioPhoneNumber,
to=agent1,
url=ROOT_URL+'agent1_to_conference.html' # ROOT_URL is the url where app is being executed
)
resp.append(dial)
return str(resp)
@app.route('/agent1_to_conference.html', methods=['GET', 'POST'])
def agent1_to_conference():
resp = VoiceResponse()
# Join Agent1 to the conference, allowing hangupOnStar
functionality to join Agent2 later
dial = Dial(
action='join_agent2.html',
method='POST',
hangup_on_star=True,
)
dial.conference(
confName,
start_conference_on_enter=True,
end_conference_on_exit=False # False, to avoid hanging up to Customer
)
resp.append(dial)
return str(resp)
@app.route('/join_agent2.html', methods=['GET', 'POST'])
def join_agent2():
resp = VoiceResponse()
dial = Dial()
# Re-join Agent1 (after clicking *)
dial.conference(
confName,
start_conference_on_enter=True,
end_conference_on_exit=True
)
resp.append(dial)
# Join Agent2
client.conferences(confName).participants.create(
from_=twilioPhoneNumber,
to=agent2
)
return str(resp)