blob: 32602d63113e5c26720f68876ba4bd8fa6b02034 [file] [log] [blame]
Suraj Shetty863b93c2019-05-21 07:57:06 +05301import frappe
Suraj Shetty2f847792019-06-12 10:31:07 +05302from erpnext.crm.doctype.utils import get_employee_emails_for_popup
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05303import requests
4
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)
10def handle_incoming_call(*args, **kwargs):
Suraj Shetty07fe2992019-05-22 15:48:57 +053011 exotel_settings = get_exotel_settings()
12 if not exotel_settings.enabled: return
Suraj Shetty863b93c2019-05-21 07:57:06 +053013
Suraj Shetty07fe2992019-05-22 15:48:57 +053014 status = kwargs.get('Status')
Suraj Shetty39a4d592019-05-27 10:38:43 +053015 if status == 'free':
Suraj Shetty07fe2992019-05-22 15:48:57 +053016 return
17
18 call_log = get_call_log(kwargs)
19
Suraj Shettyc8c17422019-06-07 10:22:50 +053020 employee_emails = get_employee_emails_for_popup(kwargs.get('To'))
Suraj Shetty87645e92019-06-06 11:24:31 +053021 for email in employee_emails:
22 frappe.publish_realtime('show_call_popup', call_log, user=email)
Suraj Shettybd03a512019-05-27 15:30:41 +053023
24@frappe.whitelist(allow_guest=True)
25def handle_end_call(*args, **kwargs):
Suraj Shettyc8c17422019-06-07 10:22:50 +053026 call_log = update_call_log(kwargs, 'Completed')
27 frappe.publish_realtime('call_disconnected', call_log)
Suraj Shettye9bfecf2019-06-03 12:27:02 +053028
29@frappe.whitelist(allow_guest=True)
30def handle_missed_call(*args, **kwargs):
Suraj Shettyc8c17422019-06-07 10:22:50 +053031 call_log = update_call_log(kwargs, 'Missed')
32 frappe.publish_realtime('call_disconnected', call_log)
Suraj Shettye9bfecf2019-06-03 12:27:02 +053033
Suraj Shetty87645e92019-06-06 11:24:31 +053034def update_call_log(call_payload, status):
35 call_log = get_call_log(call_payload, False)
Suraj Shettybd03a512019-05-27 15:30:41 +053036 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053037 call_log.status = status
38 call_log.duration = call_payload.get('DialCallDuration') or 0
Suraj Shettybe1dddd2019-06-17 08:06:14 +053039 call_log.recording_url = call_payload.get('RecordingUrl')
Suraj Shettybd03a512019-05-27 15:30:41 +053040 call_log.save(ignore_permissions=True)
41 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053042 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053043
Suraj Shetty863b93c2019-05-21 07:57:06 +053044
Suraj Shettybd03a512019-05-27 15:30:41 +053045def get_call_log(call_payload, create_new_if_not_found=True):
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053046 call_log = frappe.get_all('Call Log', {
Suraj Shettyc8c17422019-06-07 10:22:50 +053047 'id': call_payload.get('CallSid'),
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053048 }, limit=1)
49
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053050 if call_log:
51 return frappe.get_doc('Call Log', call_log[0].name)
Suraj Shettybd03a512019-05-27 15:30:41 +053052 elif create_new_if_not_found:
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053053 call_log = frappe.new_doc('Call Log')
Suraj Shettyc8c17422019-06-07 10:22:50 +053054 call_log.id = call_payload.get('CallSid')
Suraj Shetty3fdefff2019-06-17 08:27:00 +053055 call_log.to = call_payload.get('CallTo')
56 call_log.medium = call_payload.get('To')
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053057 call_log.status = 'Ringing'
Suraj Shettyc8c17422019-06-07 10:22:50 +053058 setattr(call_log, 'from', call_payload.get('CallFrom'))
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053059 call_log.save(ignore_permissions=True)
Suraj Shettybd03a512019-05-27 15:30:41 +053060 frappe.db.commit()
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053061 return call_log
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053062
Suraj Shetty07fe2992019-05-22 15:48:57 +053063@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053064def get_call_status(call_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053065 endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id))
66 response = requests.get(endpoint)
Suraj Shetty07fe2992019-05-22 15:48:57 +053067 status = response.json().get('Call', {}).get('Status')
68 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053069
Suraj Shetty07fe2992019-05-22 15:48:57 +053070@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053071def make_a_call(from_number, to_number, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053072 endpoint = get_exotel_endpoint('Calls/connect.json?details=true')
73 response = requests.post(endpoint, data={
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053074 'From': from_number,
75 'To': to_number,
76 'CallerId': caller_id
77 })
78
79 return response.json()
80
81def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +053082 return frappe.get_single('Exotel Settings')
83
84@frappe.whitelist(allow_guest=True)
85def get_phone_numbers():
86 numbers = 'some number'
87 whitelist_numbers(numbers, 'for number')
88 return numbers
89
90def whitelist_numbers(numbers, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053091 endpoint = get_exotel_endpoint('CustomerWhitelist')
92 response = requests.post(endpoint, data={
Suraj Shettye9bfecf2019-06-03 12:27:02 +053093 'VirtualNumber': caller_id,
94 'Number': numbers,
95 })
96
Suraj Shetty06f80342019-06-13 17:13:54 +053097 return response
98
99def get_all_exophones():
100 endpoint = get_exotel_endpoint('IncomingPhoneNumbers')
101 response = requests.post(endpoint)
102 return response
103
104def get_exotel_endpoint(action):
105 settings = get_exotel_settings()
106 return 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}'.format(
107 api_key=settings.api_key,
108 api_token=settings.api_token,
109 sid=settings.account_sid,
110 action=action
111 )