blob: 09c399e6aafc018a3d3d6a107a590faf1e4f4dac [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 Shettyf5dd4942019-07-16 11:07:25 +053021 else:
22 update_call_log(call_payload, call_log=call_log)
Suraj Shetty07fe2992019-05-22 15:48:57 +053023
Suraj Shettybd03a512019-05-27 15:30:41 +053024@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053025def handle_end_call(**kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053026 update_call_log(kwargs, 'Completed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053027
28@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053029def handle_missed_call(**kwargs):
Suraj Shetty340ccb62019-06-17 10:16:38 +053030 update_call_log(kwargs, 'Missed')
Suraj Shettye9bfecf2019-06-03 12:27:02 +053031
Suraj Shettyf5dd4942019-07-16 11:07:25 +053032def update_call_log(call_payload, status='Ringing', call_log=None):
33 call_log = call_log or get_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053034 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053035 call_log.status = status
Suraj Shettyf5dd4942019-07-16 11:07:25 +053036 call_log.to = call_payload.get('DialWhomNumber')
Suraj Shettyc8c17422019-06-07 10:22:50 +053037 call_log.duration = call_payload.get('DialCallDuration') or 0
Suraj Shettybe1dddd2019-06-17 08:06:14 +053038 call_log.recording_url = call_payload.get('RecordingUrl')
Suraj Shettybd03a512019-05-27 15:30:41 +053039 call_log.save(ignore_permissions=True)
40 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053041 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053042
Suraj Shetty502565f2019-07-01 14:28:59 +053043def get_call_log(call_payload):
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053044 call_log = frappe.get_all('Call Log', {
Suraj Shettyc8c17422019-06-07 10:22:50 +053045 'id': call_payload.get('CallSid'),
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053046 }, limit=1)
47
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053048 if call_log:
49 return frappe.get_doc('Call Log', call_log[0].name)
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053050
Suraj Shetty502565f2019-07-01 14:28:59 +053051def create_call_log(call_payload):
52 call_log = frappe.new_doc('Call Log')
53 call_log.id = call_payload.get('CallSid')
Suraj Shettyf5dd4942019-07-16 11:07:25 +053054 call_log.to = call_payload.get('DialWhomNumber')
Suraj Shetty502565f2019-07-01 14:28:59 +053055 call_log.medium = call_payload.get('To')
56 call_log.status = 'Ringing'
57 setattr(call_log, 'from', call_payload.get('CallFrom'))
58 call_log.save(ignore_permissions=True)
59 frappe.db.commit()
60 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053061
Suraj Shetty07fe2992019-05-22 15:48:57 +053062@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053063def get_call_status(call_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053064 endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id))
65 response = requests.get(endpoint)
Suraj Shetty07fe2992019-05-22 15:48:57 +053066 status = response.json().get('Call', {}).get('Status')
67 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053068
Suraj Shetty07fe2992019-05-22 15:48:57 +053069@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053070def make_a_call(from_number, to_number, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053071 endpoint = get_exotel_endpoint('Calls/connect.json?details=true')
72 response = requests.post(endpoint, data={
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053073 'From': from_number,
74 'To': to_number,
75 'CallerId': caller_id
76 })
77
78 return response.json()
79
80def get_exotel_settings():
Suraj Shettye9bfecf2019-06-03 12:27:02 +053081 return frappe.get_single('Exotel Settings')
82
Suraj Shettye9bfecf2019-06-03 12:27:02 +053083def whitelist_numbers(numbers, caller_id):
Suraj Shetty06f80342019-06-13 17:13:54 +053084 endpoint = get_exotel_endpoint('CustomerWhitelist')
85 response = requests.post(endpoint, data={
Suraj Shettye9bfecf2019-06-03 12:27:02 +053086 'VirtualNumber': caller_id,
87 'Number': numbers,
88 })
89
Suraj Shetty06f80342019-06-13 17:13:54 +053090 return response
91
92def get_all_exophones():
93 endpoint = get_exotel_endpoint('IncomingPhoneNumbers')
94 response = requests.post(endpoint)
95 return response
96
97def get_exotel_endpoint(action):
98 settings = get_exotel_settings()
99 return 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}'.format(
100 api_key=settings.api_key,
101 api_token=settings.api_token,
102 sid=settings.account_sid,
103 action=action
104 )