from flask import Flask, Response, request, make_response
import plivo
from plivo import plivoxml
app=Flask(__name__)
music_url = "https://s3.amazonaws.com/plivocloud/music.mp3"
client = plivo.RestClient(auth_id="<auth_id>", auth_token="<auth_token>")
# Add customer to the MPC. You can assign this to the Plivo app of the endpoint mapped to the customer in the browser app
@app.route("/add/customer/", methods=["GET", "POST"])
def multipartycall_add_customer():
mpc_name = "test"
mpc_params = {
"content": mpc_name,
"role": "Customer",
"status_callback_url": "https://<ngrok_identifier>.ngrok.io/add/agent/",
"status_callback_method": "POST",
"wait_music_url": music_url,
"wait_music_method": "GET",
}
mpc_element = plivoxml.MultiPartyCallElement(**mpc_params)
res = plivoxml.ResponseElement()
res.add(mpc_element)
return Response(res.to_string(), mimetype="application/xml")
# Add agent to the MPC to talk to the customer
@app.route("/customer/callback/", methods=["GET", "POST"])
def multipartycall_add_agent():
mpc_EventName = request.form.get("EventName")
mpc_MPCUUID = request.form.get("MPCUUID")
mpc_ParticipantCallFrom = request.form.get("ParticipantCallFrom")
if mpc_EventName == "MPCInitialized":
call_params = {
'role': "Agent",
'uuid': mpc_MPCUUID,
'start_mpc_on_enter': True,
'from_': mpc_ParticipantCallFrom, # Customer number as caller ID
'to_': "sip:websdk171107061912@phone.plivo.com", #Agent's endpoint username or phone number
'call_status_callback_url': "https://<ngrok_identifier>.ngrok.io/agent/callback/",
'call_status_callback_method': 'POST',
"enter_sound": "none"
}
try:
response = client.multi_party_calls.add_participant(**call_params)
except Exception as e:
response = client.multi_party_calls.stop(uuid=mpc_MPCUUID)
return 'ok'
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)