The Call Notification via Webhook feature is currently available as a Private Beta product, and Twilio may change the information in this document at any time. This means that some features aren't yet implemented, and others may change before the product becomes Generally Available. Private Beta products aren't covered by a Service Level Agreement.
The Twilio Programmable Voice Android and iOS SDKs use platform push notifications, Firebase Cloud Messaging (FCM) for Android and Apple VoIP Service for iOS, for delivering incoming call notifications. The application developers need to provide their cloud messaging credentials or certificates to Twilio (stored as Push Credentials) to be used for authentication when Twilio sends the push notification delivery requests to FCM or APN. In order to receive incoming call push notifications, an app user needs to first register a push binding, which indicates the mobile client is active ready for incoming calls. Upon receiving the push notification, the application uses the Android Voice.handleMessage()
method or the iOS TwilioVoiceSDK.handleNotification()
method and passes the push notification payload. A call-invite-received callback event will be raised to the application so the user can choose to accept or reject the call.
There are a few limitations with the current solution:
Voice.unregister()
method.
We are introducing the new Call Notification Webhook feature. Once configured, the developer's webhook endpoint will receive incoming call notifications every time calls are made to the clients under their account. Application developers are responsible for delivering the call notifications to their clients, via the channels that are best suited for their application such as push notification or web socket.
Below are a few requirements on the application developers' side in order to adopt the feature:
Webhook configuration
Follow the (Voice Customer Configuration API)[doc] to create a default configuration with the webhook URL.
Client binding management
In order to deliver call notifications to app users, the application developers should maintain a list of active clients. Each active client entry should contain:
Webhook handler
An endpoint to receive HTTP POST
requests with information about the call notification. The application endpoint should return a response:
200 OK
: client instance with matching identity and Twilio Account SID is found and call notification dispatched to the client.404 Not Found
: no client with the same identity and Twilio Account SID can be found.The body of the POST
request consists of several key parameters of the call in JSON format:
1{2"twi_account_sid": ACxxxx,3"twi_bridge_token": "eyJraxxxx",4"twi_call_sid": CAxxxx,5"twi_from": "client:alice",6"twi_message_id": WBxxxx,7"twi_message_type": "twilio.voice.call",8"twi_to": "client:bob"9}
Parameters:
Parameter | Definition |
---|---|
twi_account_sid | The Twilio Account SID used for generating this call. |
twi_bridge_token | An encrypted token for the mobile client to connect back to the caller. |
twi_call_sid | The Twilio Call SID. |
twi_from | The caller ID. |
twi_to | The callee ID. |
twi_message_id | A unique ID associated with this webhook request. |
twi_message_type | A string value "twilio.voice.call" indicating this notification is for an incoming call. |
twi_params (optional) | Custom parameters will be listed here as URL-encoded parameters when provided in the <Client> TwiML or REST Call API. Check out here for more information about custom parameters. |
If bindings matching both the Account SID and the callee identity are found, deliver the JSON body directly, without modification, to the clients through the most suitable communication channel for the application's use case, such as push notifications or web sockets. The mobile application will then pass the payload to the Android Voice.handleMessage()
method or the iOS TwilioVoiceSDK.handleNotification()
method upon receiving the notification, just like how it works when Twilio sends the incoming call notifications via push notification, and an incoming call invite will be raised to the application for the user to answer.
Here's an example in Python for handling a webhook request and sending the notification payload to the mobile client via push notification:
1from flask import Flask, request, Response2app = Flask(__name__)34@app.route("/calls/notificationWebhook", methods=["POST"])5def call_notification_webhook():6try:7notification_payload = request.json8except:9notification_payload = None10logging.error("error in parsing request form data as JSON: \n{}".format(sys.exc_info()[0]))11return Response(status=400, mimetype="text/plain")1213# Get the callee identity14twilio_to = notification_payload["twi_to"]15twilio_to_components = twilio_to.split(':')16if len(twilio_to_components) == 2:17callee_client_identity = twilio_to_components[1]18else:19logging.error("error parsing the callee identity in twi_to")20callee_client_identity = None2122# Get the Twilio Account SID23twilio_account_sid = notification_payload["twi_account_sid"]2425if not callee_client_identity:26return Response("Invalid callee identity", status=400, mimetype="text/plain")2728global binding_data29# Use the identity and account SID for binding lookup30bindings = binding_data.get_bindings_by_identity(callee_client_identity, twilio_account_sid)3132if not len(bindings):33response = Response('Not found', status=404, mimetype="text/plain")34return response3536for binding in bindings:37# Send a push notification to this client38notification_webhook_processor.notification_dispatch(binding, notification_payload)3940response = Response(status=200, mimetype="text/plain")41return response42