blob: c04cedce31f7d8c05a6299606047a36d0d2e61ae [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)
Suraj Shetty502565f2019-07-01 14:28:59 +05309def handle_incoming_call(**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 Shetty502565f2019-07-01 14:28:59 +053013 call_payload = kwargs
14 status = call_payload.get('Status')
Suraj Shetty39a4d592019-05-27 10:38:43 +053015 if status == 'free':
Suraj Shetty07fe2992019-05-22 15:48:57 +053016 return
17
Suraj Shetty502565f2019-07-01 14:28:59 +053018 call_log = get_call_log(call_payload)
19 if not call_log:
20 create_call_log(call_payload)
Suraj Shetty07fe2992019-05-22 15:48:57 +053021
Suraj Shettybd03a512019-05-27 15:30:41 +053022@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053023def handle_end_call(**kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053024 update_call_log(kwargs, 'Completed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053025
26@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053027def handle_missed_call(**kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053028 update_call_log(kwargs, 'Missed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053029
Suraj Shetty87645e92019-06-06 11:24:31 +053030def update_call_log(call_payload, status):
Suraj Shetty502565f2019-07-01 14:28:59 +053031 call_log = get_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053032 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053033 call_log.status = status
34 call_log.duration = call_payload.get('DialCallDuration') or 0
Suraj Shettybe1dddd2019-06-17 08:06:14 +053035 call_log.recording_url = call_payload.get('RecordingUrl')
Suraj Shettybd03a512019-05-27 15:30:41 +053036 call_log.save(ignore_permissions=True)
37 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053038 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053039
Suraj Shetty502565f2019-07-01 14:28:59 +053040def get_call_log(call_payload):
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053041 call_log = frappe.get_all('Call Log', {
Suraj Shettyc8c17422019-06-07 10:22:50 +053042 'id': call_payload.get('CallSid'),
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053043 }, limit=1)
44
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053045 if call_log:
46 return frappe.get_doc('Call Log', call_log[0].name)
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053047
Suraj Shetty502565f2019-07-01 14:28:59 +053048def create_call_log(call_payload):
49 call_log = frappe.new_doc('Call Log')
50 call_log.id = call_payload.get('CallSid')
51 call_log.to = call_payload.get('CallTo')
52 call_log.medium = call_payload.get('To')
53 call_log.status = 'Ringing'
54 setattr(call_log, 'from', call_payload.get('CallFrom'))
55 call_log.save(ignore_permissions=True)
56 frappe.db.commit()
57 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053058
Suraj Shetty07fe2992019-05-22 15:48:57 +053059@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053060def get_call_status(call_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053061 endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id))
62 response = requests.get(endpoint)
Suraj Shetty07fe2992019-05-22 15:48:57 +053063 status = response.json().get('Call', {}).get('Status')
64 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053065
Suraj Shetty07fe2992019-05-22 15:48:57 +053066@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053067def make_a_call(from_number, to_number, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053068 endpoint = get_exotel_endpoint('Calls/connect.json?details=true')
69 response = requests.post(endpoint, data={
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053070 'From': from_number,
71 'To': to_number,
72 'CallerId': caller_id
73 })
74
75 return response.json()
76
77def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +053078 return frappe.get_single('Exotel Settings')
79
Suraj Shettye9bfecf2019-06-03 12:27:02 +053080def whitelist_numbers(numbers, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053081 endpoint = get_exotel_endpoint('CustomerWhitelist')
82 response = requests.post(endpoint, data={
Suraj Shettye9bfecf2019-06-03 12:27:02 +053083 'VirtualNumber': caller_id,
84 'Number': numbers,
85 })
86
Suraj Shetty06f80342019-06-13 17:13:54 +053087 return response
88
89def get_all_exophones():
90 endpoint = get_exotel_endpoint('IncomingPhoneNumbers')
91 response = requests.post(endpoint)
92 return response
93
94def get_exotel_endpoint(action):
95 settings = get_exotel_settings()
96 return 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}'.format(
97 api_key=settings.api_key,
98 api_token=settings.api_token,
99 sid=settings.account_sid,
100 action=action
101 )