blob: 57cba78bdbbe3669fc38ef524359b5b7aa134d64 [file] [log] [blame]
Suraj Shetty863b93c2019-05-21 07:57:06 +05301import frappe
Suraj Shetty87645e92019-06-06 11:24:31 +05302from erpnext.crm.doctype.utils import get_document_with_phone_number, 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 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 status = kwargs.get('Status')
13
Suraj Shetty39a4d592019-05-27 10:38:43 +053014 if status == 'free':
15 # call disconnected for agent
16 # "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 +053017 return
18
19 call_log = get_call_log(kwargs)
20
Suraj Shetty87645e92019-06-06 11:24:31 +053021 employee_emails = get_employee_emails_for_popup()
22 for email in employee_emails:
23 frappe.publish_realtime('show_call_popup', call_log, user=email)
Suraj Shettybd03a512019-05-27 15:30:41 +053024
25@frappe.whitelist(allow_guest=True)
26def handle_end_call(*args, **kwargs):
Suraj Shetty87645e92019-06-06 11:24:31 +053027 update_call_log(kwargs, 'Completed')
Suraj Shetty97780612019-06-06 14:48:37 +053028 frappe.publish_realtime('call_disconnected', kwargs.get('CallSid'))
Suraj Shettye9bfecf2019-06-03 12:27:02 +053029
30@frappe.whitelist(allow_guest=True)
31def handle_missed_call(*args, **kwargs):
Suraj Shetty87645e92019-06-06 11:24:31 +053032 update_call_log(kwargs, 'Missed')
Suraj Shetty97780612019-06-06 14:48:37 +053033 frappe.publish_realtime('call_disconnected', kwargs.get('CallSid'))
Suraj Shettye9bfecf2019-06-03 12:27:02 +053034
Suraj Shetty87645e92019-06-06 11:24:31 +053035def update_call_log(call_payload, status):
36 call_log = get_call_log(call_payload, False)
Suraj Shettybd03a512019-05-27 15:30:41 +053037 if call_log:
Suraj Shetty87645e92019-06-06 11:24:31 +053038 call_log.call_status = status
39 call_log.call_duration = call_payload.get('DialCallDuration') or 0
Suraj Shettybd03a512019-05-27 15:30:41 +053040 call_log.save(ignore_permissions=True)
41 frappe.db.commit()
Suraj Shetty863b93c2019-05-21 07:57:06 +053042
Suraj Shetty863b93c2019-05-21 07:57:06 +053043
Suraj Shettybd03a512019-05-27 15:30:41 +053044def get_call_log(call_payload, create_new_if_not_found=True):
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053045 call_log = frappe.get_all('Call Log', {
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053046 'call_id': call_payload.get('CallSid'),
47 }, limit=1)
48
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053049 if call_log:
50 return frappe.get_doc('Call Log', call_log[0].name)
Suraj Shettybd03a512019-05-27 15:30:41 +053051 elif create_new_if_not_found:
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053052 call_log = frappe.new_doc('Call Log')
53 call_log.call_id = call_payload.get('CallSid')
54 call_log.call_from = call_payload.get('CallFrom')
55 call_log.status = 'Ringing'
56 call_log.save(ignore_permissions=True)
Suraj Shettybd03a512019-05-27 15:30:41 +053057 frappe.db.commit()
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053058 return call_log
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053059
Suraj Shetty07fe2992019-05-22 15:48:57 +053060@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053061def get_call_status(call_id):
Suraj Shetty07fe2992019-05-22 15:48:57 +053062 print(call_id)
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053063 settings = get_exotel_settings()
Suraj Shetty07fe2992019-05-22 15:48:57 +053064 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 +053065 api_key=settings.api_key,
66 api_token=settings.api_token,
67 call_id=call_id
68 ))
Suraj Shetty07fe2992019-05-22 15:48:57 +053069 status = response.json().get('Call', {}).get('Status')
70 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053071
Suraj Shetty07fe2992019-05-22 15:48:57 +053072@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053073def make_a_call(from_number, to_number, caller_id):
74 settings = get_exotel_settings()
Suraj Shetty07fe2992019-05-22 15:48:57 +053075 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 +053076 api_key=settings.api_key,
77 api_token=settings.api_token,
Suraj Shettye9bfecf2019-06-03 12:27:02 +053078 sid=settings.account_sid
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053079 ), data={
80 'From': from_number,
81 'To': to_number,
82 'CallerId': caller_id
83 })
84
85 return response.json()
86
87def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +053088 return frappe.get_single('Exotel Settings')
89
90@frappe.whitelist(allow_guest=True)
91def get_phone_numbers():
92 numbers = 'some number'
93 whitelist_numbers(numbers, 'for number')
94 return numbers
95
96def whitelist_numbers(numbers, caller_id):
97 settings = get_exotel_settings()
98 query = 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/CustomerWhitelist'.format(
99 api_key=settings.api_key,
100 api_token=settings.api_token,
101 sid=settings.account_sid
102 )
103 response = requests.post(query, data={
104 'VirtualNumber': caller_id,
105 'Number': numbers,
106 })
107
108 return response