How To Use Exotel In Python.

In this article, we will learn how to use Exotel in python.

Once you register at Exotel, you get a trial account. Your trial account is loaded with ₹1000 which you can use to test the system.

Features:

Voice, SMS, Authentication, App to app calling

Here, in this article, we will learn about the voice calling system provided by Exotel and how to implement it in our project.

Step 1) First of all create an Exotel account, in the accounting process KYC is also needed. In your account, KYC is not done so you can not access made calls or voice.

Step 2) If the Exotel account process is done then you got a sid, token, account_key in My Account under API Settings tab,
Your app id and callerId you can find in Home icon under App Bazzer tab click than you got it.

Step 3) Create one function for Exotel voice call.

Let’s start with one example for Exotel calling functionality

Here, I’m getting all credentials using the .exotel_setting file. You can also add your all credentials in a separate file. Like,

sid = “exotel_sid”
api_key = “asdasdasddnsbdskdb3243basdas”

import requests
import base64
import json
from django.http import JsonResponse
from requests.models import Response
from .exotel_setting import sid, token, callurl, from_no, to, api_key, agent_no, customer_no ,callerid

def connect_customer_to_agent(request):
    encoding = api_key + ":" + token
    data_bytes = encoding.encode("utf-8")
    encoding = base64.b64encode(data_bytes)
    decodingStr = encoding.decode("utf-8")
    url = "https://api.exotel.com/v1/Accounts/"+sid+"/Calls/connect.json"

    payload={
      'From': agent_no,
      'To' : customer_no,
      'CallerId': callerid
    } 

    headers = {
      'Authorization': "Basic " + decodingStr,
      'accept': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded'
    }

   response = requests.request("POST", url, headers=headers, data=payload)
   return JsonResponse(response.json())

=> If you call the function then you can get a JSON response like,

{"Call": {"Sid": "0da8776557ad45197f86fs7312f15br", "ParentCallSid": null, "DateCreated": "2021-11-27 11:35:29", "DateUpdated": "2021-11-27 11:35:29", "AccountSid": "exotel_sid", "To": "08888888888", "From": "09999999999","PhoneNumberSid": "02245389813", "Status": "in-progress", "StartTime": "2021-11-27 11:35:29", "EndTime": null, "Duration": null, "Price": null, "Direction": "outbound-api", "AnsweredBy": null, "ForwardedFrom": null, "CallerName": null, "Uri": "/v1/Accounts/exotel_sid/Calls/0da8776557ad45197f86fs7312f15br.json", "RecordingUrl": null}}

If you need the RecordingUrl then you can go with this payload request:

payload = {
    'From': from_no,
    'To' : to,
    'CallerId': callerid,
    'StatusCallback': 'http://your-application.com/exotel-callback',
    'StatusCallbackEvents[0]': 'terminal',
    'StatusCallbackContentType': 'application/json'
}

Here, StatusCallback An HTTP POST request will be made to this URL depending on what events are subscribed using ‘StatusCallbackEvents’.
= Refer to the complete list of parameters that will be sent to your endpoint.
= You can get the response of the following API endpoint ‘http://your-application.com/exotel-callback’,

Response:

{
"CallSid": "492205107c5fb48f4ac25ds1f77759339",
"EventType": "terminal",
"DateCreated": "2019-04-08 03:17:59",
"DateUpdated": "2019-04-08 03:18:35",
"Status": "no-answer",
"To": "+91886799XXXX",
"From": "+91941374XXXX",
"PhoneNumberSid": "0113083XXXX",
"StartTime": "2019-04-08 03:17:59",
"EndTime": "2019-04-08 03:18:36",
"Direction": "outbound-api",
"RecordingUrl": "https://s3-ap-southeast-1.amazonaws.com/exotelrecordings//492205107c5fb48f4ac25ds1f77759339.mp3",
"ConversationDuration": 32,
"Legs": [
    {
      "OnCallDuration": 41,
      "Status": "completed"
    },
    {
      "OnCallDuration": 32,
      "Status": "completed"
    }
]
}

To learn more about Exotel, check the official documentation.

I hope you’ll get impressed by the blog of Exotel using python.  If you’ve got any questions regarding this blog, please let me know in the comments.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories