blob: 4e88c5b03a61557529a59bfb7dda998ae3e3b8f3 [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')
15
Suraj Shetty39a4d592019-05-27 10:38:43 +053016 if status == 'free':
17 # call disconnected for agent
18 # "and get_call_status(kwargs.get('CallSid')) in ['in-progress']" - additional check to ensure if the call was redirected
Suraj Shetty07fe2992019-05-22 15:48:57 +053019 return
20
21 call_log = get_call_log(kwargs)
22
Suraj Shettyc8c17422019-06-07 10:22:50 +053023 employee_emails = get_employee_emails_for_popup(kwargs.get('To'))
Suraj Shetty87645e92019-06-06 11:24:31 +053024 for email in employee_emails:
25 frappe.publish_realtime('show_call_popup', call_log, user=email)
Suraj Shettybd03a512019-05-27 15:30:41 +053026
27@frappe.whitelist(allow_guest=True)
28def handle_end_call(*args, **kwargs):
Suraj Shettyc8c17422019-06-07 10:22:50 +053029 call_log = update_call_log(kwargs, 'Completed')
30 frappe.publish_realtime('call_disconnected', call_log)
Suraj Shettye9bfecf2019-06-03 12:27:02 +053031
32@frappe.whitelist(allow_guest=True)
33def handle_missed_call(*args, **kwargs):
Suraj Shettyc8c17422019-06-07 10:22:50 +053034 call_log = update_call_log(kwargs, 'Missed')
35 frappe.publish_realtime('call_disconnected', call_log)
Suraj Shettye9bfecf2019-06-03 12:27:02 +053036
Suraj Shetty87645e92019-06-06 11:24:31 +053037def update_call_log(call_payload, status):
38 call_log = get_call_log(call_payload, False)
Suraj Shettybd03a512019-05-27 15:30:41 +053039 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053040 call_log.status = status
41 call_log.duration = call_payload.get('DialCallDuration') or 0
Suraj Shettybd03a512019-05-27 15:30:41 +053042 call_log.save(ignore_permissions=True)
43 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053044 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053045
Suraj Shetty863b93c2019-05-21 07:57:06 +053046
Suraj Shettybd03a512019-05-27 15:30:41 +053047def get_call_log(call_payload, create_new_if_not_found=True):
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053048 call_log = frappe.get_all('Call Log', {
Suraj Shettyc8c17422019-06-07 10:22:50 +053049 'id': call_payload.get('CallSid'),
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053050 }, limit=1)
51
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053052 if call_log:
53 return frappe.get_doc('Call Log', call_log[0].name)
Suraj Shettybd03a512019-05-27 15:30:41 +053054 elif create_new_if_not_found:
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053055 call_log = frappe.new_doc('Call Log')
Suraj Shettyc8c17422019-06-07 10:22:50 +053056 call_log.id = call_payload.get('CallSid')
57 call_log.to = call_payload.get('To')
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053058 call_log.status = 'Ringing'
Suraj Shettyc8c17422019-06-07 10:22:50 +053059 setattr(call_log, 'from', call_payload.get('CallFrom'))
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053060 call_log.save(ignore_permissions=True)
Suraj Shettybd03a512019-05-27 15:30:41 +053061 frappe.db.commit()
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053062 return call_log
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 get_call_status(call_id):
Suraj Shetty07fe2992019-05-22 15:48:57 +053066 print(call_id)
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053067 settings = get_exotel_settings()
Suraj Shetty07fe2992019-05-22 15:48:57 +053068 response = requests.get('https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/erpnext/Calls/{call_id}.json'.format(
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053069 api_key=settings.api_key,
70 api_token=settings.api_token,
71 call_id=call_id
72 ))
Suraj Shetty07fe2992019-05-22 15:48:57 +053073 status = response.json().get('Call', {}).get('Status')
74 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053075
Suraj Shetty07fe2992019-05-22 15:48:57 +053076@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053077def make_a_call(from_number, to_number, caller_id):
78 settings = get_exotel_settings()
Suraj Shetty07fe2992019-05-22 15:48:57 +053079 response = requests.post('https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/Calls/connect.json?details=true'.format(
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053080 api_key=settings.api_key,
81 api_token=settings.api_token,
Suraj Shettye9bfecf2019-06-03 12:27:02 +053082 sid=settings.account_sid
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053083 ), data={
84 'From': from_number,
85 'To': to_number,
86 'CallerId': caller_id
87 })
88
89 return response.json()
90
91def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +053092 return frappe.get_single('Exotel Settings')
93
94@frappe.whitelist(allow_guest=True)
95def get_phone_numbers():
96 numbers = 'some number'
97 whitelist_numbers(numbers, 'for number')
98 return numbers
99
100def whitelist_numbers(numbers, caller_id):
101 settings = get_exotel_settings()
102 query = 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/CustomerWhitelist'.format(
103 api_key=settings.api_key,
104 api_token=settings.api_token,
105 sid=settings.account_sid
106 )
107 response = requests.post(query, data={
108 'VirtualNumber': caller_id,
109 'Number': numbers,
110 })
111
112 return response