blob: 167fcb71652ef04ed5393f42930edbba3847eb97 [file] [log] [blame]
Suraj Shetty863b93c2019-05-21 07:57:06 +05301import frappe
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05302import requests
Suraj Shettyaf2eac42019-09-17 15:53:23 +05303from frappe import _
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05304
5# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
Suraj Shettyc8c17422019-06-07 10:22:50 +05306# api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call
7# api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call
Suraj Shetty863b93c2019-05-21 07:57:06 +05308
9@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053010def handle_incoming_call(**kwargs):
Suraj Shettyaf2eac42019-09-17 15:53:23 +053011 try:
12 exotel_settings = get_exotel_settings()
13 if not exotel_settings.enabled: return
Suraj Shetty863b93c2019-05-21 07:57:06 +053014
Suraj Shettyaf2eac42019-09-17 15:53:23 +053015 call_payload = kwargs
16 status = call_payload.get('Status')
17 if status == 'free':
18 return
Suraj Shetty07fe2992019-05-22 15:48:57 +053019
Suraj Shettyaf2eac42019-09-17 15:53:23 +053020 call_log = get_call_log(call_payload)
21 if not call_log:
22 create_call_log(call_payload)
23 else:
24 update_call_log(call_payload, call_log=call_log)
25 except Exception as e:
26 frappe.db.rollback()
27 frappe.log_error(title=_('Error in Exotel incoming call'))
28 frappe.db.commit()
Suraj Shetty07fe2992019-05-22 15:48:57 +053029
Suraj Shettybd03a512019-05-27 15:30:41 +053030@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053031def handle_end_call(**kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053032 update_call_log(kwargs, 'Completed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053033
34@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053035def handle_missed_call(**kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053036 update_call_log(kwargs, 'Missed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053037
Suraj Shettyf5dd4942019-07-16 11:07:25 +053038def update_call_log(call_payload, status='Ringing', call_log=None):
39 call_log = call_log or get_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053040 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053041 call_log.status = status
Suraj Shettyf5dd4942019-07-16 11:07:25 +053042 call_log.to = call_payload.get('DialWhomNumber')
Suraj Shettyc8c17422019-06-07 10:22:50 +053043 call_log.duration = call_payload.get('DialCallDuration') or 0
Suraj Shettybe1dddd2019-06-17 08:06:14 +053044 call_log.recording_url = call_payload.get('RecordingUrl')
Suraj Shettybd03a512019-05-27 15:30:41 +053045 call_log.save(ignore_permissions=True)
46 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053047 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053048
Suraj Shetty502565f2019-07-01 14:28:59 +053049def get_call_log(call_payload):
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053050 call_log = frappe.get_all('Call Log', {
Suraj Shettyc8c17422019-06-07 10:22:50 +053051 'id': call_payload.get('CallSid'),
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053052 }, limit=1)
53
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053054 if call_log:
55 return frappe.get_doc('Call Log', call_log[0].name)
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053056
Suraj Shetty502565f2019-07-01 14:28:59 +053057def create_call_log(call_payload):
58 call_log = frappe.new_doc('Call Log')
59 call_log.id = call_payload.get('CallSid')
Suraj Shettyf5dd4942019-07-16 11:07:25 +053060 call_log.to = call_payload.get('DialWhomNumber')
Suraj Shetty502565f2019-07-01 14:28:59 +053061 call_log.medium = call_payload.get('To')
62 call_log.status = 'Ringing'
63 setattr(call_log, 'from', call_payload.get('CallFrom'))
64 call_log.save(ignore_permissions=True)
65 frappe.db.commit()
66 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053067
Suraj Shetty07fe2992019-05-22 15:48:57 +053068@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053069def get_call_status(call_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053070 endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id))
71 response = requests.get(endpoint)
Suraj Shetty07fe2992019-05-22 15:48:57 +053072 status = response.json().get('Call', {}).get('Status')
73 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053074
Suraj Shetty07fe2992019-05-22 15:48:57 +053075@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053076def make_a_call(from_number, to_number, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053077 endpoint = get_exotel_endpoint('Calls/connect.json?details=true')
78 response = requests.post(endpoint, data={
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053079 'From': from_number,
80 'To': to_number,
81 'CallerId': caller_id
82 })
83
84 return response.json()
85
86def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +053087 return frappe.get_single('Exotel Settings')
88
Suraj Shettye9bfecf2019-06-03 12:27:02 +053089def whitelist_numbers(numbers, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053090 endpoint = get_exotel_endpoint('CustomerWhitelist')
91 response = requests.post(endpoint, data={
Suraj Shettye9bfecf2019-06-03 12:27:02 +053092 'VirtualNumber': caller_id,
93 'Number': numbers,
94 })
95
Suraj Shetty06f80342019-06-13 17:13:54 +053096 return response
97
98def get_all_exophones():
99 endpoint = get_exotel_endpoint('IncomingPhoneNumbers')
100 response = requests.post(endpoint)
101 return response
102
103def get_exotel_endpoint(action):
104 settings = get_exotel_settings()
105 return 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}'.format(
106 api_key=settings.api_key,
107 api_token=settings.api_token,
108 sid=settings.account_sid,
109 action=action
Suraj Shettyaf2eac42019-09-17 15:53:23 +0530110 )