-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaster.py
65 lines (51 loc) · 2.22 KB
/
aster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
import threading
import time
import random
import string
from configparser import ConfigParser
config = ConfigParser()
config.read('config.ini')
pbxurl = config.get('asterisk', 'server_address')
pbxport = config.get('asterisk', 'ari_port')
server_protocol = config.get('asterisk', 'server_protocol')
ari_username = config.get('asterisk', 'ari_username')
ari_password = config.get('asterisk', 'ari_password')
def find_number(number, code=None):
endpoints = requests.get(f'{server_protocol}://{pbxurl}:{pbxport}/ari/endpoints',
auth=(ari_username, ari_password)).json()
number_data = {'message': None, 'code': None, 'found': False}
for endpoint in endpoints:
if endpoint['resource'] == str(number):
if endpoint['state'] == 'online':
number_data['found'] = True
number_data['message'] = f'Extension {number} found. Wait for a call with a code. Send {number}#<code> to confirm'
technology = endpoint['technology']
if not code:
code = generate_verification_code()
number_data['code'] = code
threading.Thread(target=verify_call, args=(number, code, technology)).start()
else:
number_data['found'] = True
number_data['message'] = f'Extension {number} found, but not online. Register it and try again.'
if not number_data['found']:
number_data['message'] = f'Extension {number} not found.'
return number_data
def verify_call(number, code, technology):
payload = {
'endpoint': f'{technology}/{number}',
'extension': 's',
'context': 'verify_number',
'priority': 1,
'timeout': 30,
'callerId': 'telegramisk',
'variables': {
'verification_code': f'{code}'
}
}
time.sleep(5)
requests.post(f'{server_protocol}://{pbxurl}:{pbxport}/ari/channels',
auth=(ari_username, ari_password), json=payload)
def generate_verification_code():
code = ''.join(random.choices(string.digits, k=4))
return code