blob: a42718602add1d0970e4a6e92833672552545a7d [file] [log] [blame]
Suraj Shetty863b93c2019-05-21 07:57:06 +05301import frappe
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05302import requests
3
4# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
Suraj Shettyc8c17422019-06-07 10:22:50 +05305# api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call
6# api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call
Suraj Shetty863b93c2019-05-21 07:57:06 +05307
8@frappe.whitelist(allow_guest=True)
9def handle_incoming_call(*args, **kwargs):
Suraj Shetty07fe2992019-05-22 15:48:57 +053010 exotel_settings = get_exotel_settings()
11 if not exotel_settings.enabled: return
Suraj Shetty863b93c2019-05-21 07:57:06 +053012
Suraj Shetty07fe2992019-05-22 15:48:57 +053013 status = kwargs.get('Status')
Suraj Shetty39a4d592019-05-27 10:38:43 +053014 if status == 'free':
Suraj Shetty07fe2992019-05-22 15:48:57 +053015 return
16
Suraj Shetty340ccb62019-06-17 10:16:38 +053017 create_call_log(kwargs)
Suraj Shetty07fe2992019-05-22 15:48:57 +053018
Suraj Shettybd03a512019-05-27 15:30:41 +053019@frappe.whitelist(allow_guest=True)
20def handle_end_call(*args, **kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053021 update_call_log(kwargs, 'Completed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053022
23@frappe.whitelist(allow_guest=True)
24def handle_missed_call(*args, **kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053025 update_call_log(kwargs, 'Missed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053026
Suraj Shetty87645e92019-06-06 11:24:31 +053027def update_call_log(call_payload, status):
28 call_log = get_call_log(call_payload, False)
Suraj Shettybd03a512019-05-27 15:30:41 +053029 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053030 call_log.status = status
31 call_log.duration = call_payload.get('DialCallDuration') or 0
Suraj Shettybe1dddd2019-06-17 08:06:14 +053032 call_log.recording_url = call_payload.get('RecordingUrl')
Suraj Shettybd03a512019-05-27 15:30:41 +053033 call_log.save(ignore_permissions=True)
34 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053035 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053036
Suraj Shettybd03a512019-05-27 15:30:41 +053037def get_call_log(call_payload, create_new_if_not_found=True):
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053038 call_log = frappe.get_all('Call Log', {
Suraj Shettyc8c17422019-06-07 10:22:50 +053039 'id': call_payload.get('CallSid'),
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053040 }, limit=1)
41
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053042 if call_log:
43 return frappe.get_doc('Call Log', call_log[0].name)
Suraj Shettybd03a512019-05-27 15:30:41 +053044 elif create_new_if_not_found:
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053045 call_log = frappe.new_doc('Call Log')
Suraj Shettyc8c17422019-06-07 10:22:50 +053046 call_log.id = call_payload.get('CallSid')
Suraj Shetty3fdefff2019-06-17 08:27:00 +053047 call_log.to = call_payload.get('CallTo')
48 call_log.medium = call_payload.get('To')
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053049 call_log.status = 'Ringing'
Suraj Shettyc8c17422019-06-07 10:22:50 +053050 setattr(call_log, 'from', call_payload.get('CallFrom'))
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053051 call_log.save(ignore_permissions=True)
Suraj Shettybd03a512019-05-27 15:30:41 +053052 frappe.db.commit()
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053053 return call_log
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053054
Suraj Shetty340ccb62019-06-17 10:16:38 +053055create_call_log = get_call_log
56
Suraj Shetty07fe2992019-05-22 15:48:57 +053057@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053058def get_call_status(call_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053059 endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id))
60 response = requests.get(endpoint)
Suraj Shetty07fe2992019-05-22 15:48:57 +053061 status = response.json().get('Call', {}).get('Status')
62 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053063
Suraj Shetty07fe2992019-05-22 15:48:57 +053064@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053065def make_a_call(from_number, to_number, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053066 endpoint = get_exotel_endpoint('Calls/connect.json?details=true')
67 response = requests.post(endpoint, data={
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053068 'From': from_number,
69 'To': to_number,
70 'CallerId': caller_id
71 })
72
73 return response.json()
74
75def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +053076 return frappe.get_single('Exotel Settings')
77
Suraj Shettye9bfecf2019-06-03 12:27:02 +053078def whitelist_numbers(numbers, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053079 endpoint = get_exotel_endpoint('CustomerWhitelist')
80 response = requests.post(endpoint, data={
Suraj Shettye9bfecf2019-06-03 12:27:02 +053081 'VirtualNumber': caller_id,
82 'Number': numbers,
83 })
84
Suraj Shetty06f80342019-06-13 17:13:54 +053085 return response
86
87def get_all_exophones():
88 endpoint = get_exotel_endpoint('IncomingPhoneNumbers')
89 response = requests.post(endpoint)
90 return response
91
92def get_exotel_endpoint(action):
93 settings = get_exotel_settings()
94 return 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}'.format(
95 api_key=settings.api_key,
96 api_token=settings.api_token,
97 sid=settings.account_sid,
98 action=action
99 )