blob: c70b0948aefb0fa015e7cadd9fb909654bb45ee0 [file] [log] [blame]
Suraj Shetty863b93c2019-05-21 07:57:06 +05301import frappe
2from erpnext.crm.doctype.utils import get_document_with_phone_number
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05303import requests
4
5# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
Suraj Shetty863b93c2019-05-21 07:57:06 +05306
7@frappe.whitelist(allow_guest=True)
8def handle_incoming_call(*args, **kwargs):
Suraj Shetty07fe2992019-05-22 15:48:57 +05309 exotel_settings = get_exotel_settings()
10 if not exotel_settings.enabled: return
Suraj Shetty863b93c2019-05-21 07:57:06 +053011
Suraj Shetty07fe2992019-05-22 15:48:57 +053012 employee_email = kwargs.get('AgentEmail')
13 status = kwargs.get('Status')
14
Suraj Shetty39a4d592019-05-27 10:38:43 +053015 if status == 'free':
16 # call disconnected for agent
17 # "and get_call_status(kwargs.get('CallSid')) in ['in-progress']" - additional check to ensure if the call was redirected
18 frappe.publish_realtime('call_disconnected', user=employee_email)
Suraj Shetty07fe2992019-05-22 15:48:57 +053019 return
20
21 call_log = get_call_log(kwargs)
22
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053023 data = frappe._dict({
24 'call_from': kwargs.get('CallFrom'),
25 'agent_email': kwargs.get('AgentEmail'),
26 'call_type': kwargs.get('Direction'),
Suraj Shetty07fe2992019-05-22 15:48:57 +053027 'call_log': call_log,
28 'call_status_method': 'erpnext.erpnext_integrations.exotel_integration.get_call_status'
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053029 })
Suraj Shettybd03a512019-05-27 15:30:41 +053030
31 frappe.publish_realtime('show_call_popup', data, user=data.agent_email)
32
33@frappe.whitelist(allow_guest=True)
34def handle_end_call(*args, **kwargs):
Suraj Shettye9bfecf2019-06-03 12:27:02 +053035 close_call_log(kwargs)
36
37@frappe.whitelist(allow_guest=True)
38def handle_missed_call(*args, **kwargs):
39 close_call_log(kwargs)
40
41def close_call_log(call_payload):
42 call_log = get_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053043 if call_log:
44 call_log.status = 'Closed'
45 call_log.save(ignore_permissions=True)
46 frappe.db.commit()
Suraj Shetty863b93c2019-05-21 07:57:06 +053047
Suraj Shetty863b93c2019-05-21 07:57:06 +053048
Suraj Shettybd03a512019-05-27 15:30:41 +053049def get_call_log(call_payload, create_new_if_not_found=True):
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053050 communication = frappe.get_all('Communication', {
51 'communication_medium': 'Phone',
52 'call_id': call_payload.get('CallSid'),
53 }, limit=1)
54
55 if communication:
Suraj Shetty07fe2992019-05-22 15:48:57 +053056 communication = frappe.get_doc('Communication', communication[0].name)
Suraj Shettybd03a512019-05-27 15:30:41 +053057 return communication
58 elif create_new_if_not_found:
Suraj Shetty07fe2992019-05-22 15:48:57 +053059 communication = frappe.new_doc('Communication')
60 communication.subject = frappe._('Call from {}').format(call_payload.get("CallFrom"))
61 communication.communication_medium = 'Phone'
62 communication.phone_no = call_payload.get("CallFrom")
63 communication.comment_type = 'Info'
64 communication.communication_type = 'Communication'
65 communication.sent_or_received = 'Received'
66 communication.communication_date = call_payload.get('StartTime')
67 communication.call_id = call_payload.get('CallSid')
Suraj Shettybd03a512019-05-27 15:30:41 +053068 communication.status = 'Open'
69 communication.content = frappe._('Call from {}').format(call_payload.get("CallFrom"))
70 communication.save(ignore_permissions=True)
71 frappe.db.commit()
72 return communication
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053073
Suraj Shetty07fe2992019-05-22 15:48:57 +053074@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053075def get_call_status(call_id):
Suraj Shetty07fe2992019-05-22 15:48:57 +053076 print(call_id)
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053077 settings = get_exotel_settings()
Suraj Shetty07fe2992019-05-22 15:48:57 +053078 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 +053079 api_key=settings.api_key,
80 api_token=settings.api_token,
81 call_id=call_id
82 ))
Suraj Shetty07fe2992019-05-22 15:48:57 +053083 status = response.json().get('Call', {}).get('Status')
84 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053085
Suraj Shetty07fe2992019-05-22 15:48:57 +053086@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053087def make_a_call(from_number, to_number, caller_id):
88 settings = get_exotel_settings()
Suraj Shetty07fe2992019-05-22 15:48:57 +053089 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 +053090 api_key=settings.api_key,
91 api_token=settings.api_token,
Suraj Shettye9bfecf2019-06-03 12:27:02 +053092 sid=settings.account_sid
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053093 ), data={
94 'From': from_number,
95 'To': to_number,
96 'CallerId': caller_id
97 })
98
99 return response.json()
100
101def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530102 return frappe.get_single('Exotel Settings')
103
104@frappe.whitelist(allow_guest=True)
105def get_phone_numbers():
106 numbers = 'some number'
107 whitelist_numbers(numbers, 'for number')
108 return numbers
109
110def whitelist_numbers(numbers, caller_id):
111 settings = get_exotel_settings()
112 query = 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/CustomerWhitelist'.format(
113 api_key=settings.api_key,
114 api_token=settings.api_token,
115 sid=settings.account_sid
116 )
117 response = requests.post(query, data={
118 'VirtualNumber': caller_id,
119 'Number': numbers,
120 })
121
122 return response