-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_users.py
218 lines (182 loc) · 7.07 KB
/
generate_users.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os
import subprocess
import xml.etree.ElementTree as ET
import csv
cc_filepath="volume-configs/callcenter.conf.xml"
input_users_dirpath="input-users"
output_users_dirpath="output-users"
conf_template = """
<configuration name="callcenter.conf" description="CallCenter">
<settings>
<param name="truncate-agents-on-load" value="true"/>
<param name="truncate-tiers-on-load" value="true"/>
</settings>
<queues>
{queues}
</queues>
<agents>
</agents>
<tiers>
</tiers>
</configuration>
""".strip()
queue_template = """
<queue name="{extension}@default">
<param name="strategy" value="ring-all"/>
<param name="moh-sound" value="$${{hold_music}}"/>
<param name="time-base-score" value="system"/>
<param name="max-wait-time" value="120"/>
<param name="max-wait-time-with-no-agent" value="0"/>
<param name="max-wait-time-with-no-agent-time-reached" value="0"/>
<param name="tier-rules-apply" value="false"/>
<param name="tier-rule-wait-second" value="300"/>
<param name="tier-rule-wait-multiply-level" value="false"/>
<param name="tier-rule-no-agent-no-wait" value="false"/>
<param name="discard-abandoned-after" value="60"/>
<param name="abandoned-resume-allowed" value="false"/>
</queue>
"""
users_teplate = """
<include>
{users}
</include>
""".strip()
intercom_template = """
<user id="{extension}">
<params>
<param name="password" value="{password}"/>
</params>
<variables>
<variable name="toll_allow" value="domestic,international,local"/>
<variable name="accountcode" value="{extension}"/>
<variable name="user_context" value="intercoms-main"/>
<variable name="effective_caller_id_name" value="{extension}"/>
<variable name="effective_caller_id_number" value="{extension}"/>
<variable name="outbound_caller_id_name" value="$${{outbound_caller_name}}"/>
<variable name="outbound_caller_id_number" value="$${{outbound_caller_id}}"/>
<variable name="sip-force-contact" value="NDLB-connectile-dysfunction"/>
<variable name="X-Doma-AI-Type" value="intercom"/>
</variables>
</user>
"""
apartment_template = """
<user id="{extension}">
<params>
<param name="password" value="{password}"/>
<param name="vm-password" value="{extension}"/>
</params>
<variables>
<variable name="absolute_codec_string" value="OPUS,G722,PCMU,PCMA,VP8"/>
<variable name="toll_allow" value="domestic,international,local"/>
<variable name="accountcode" value="{extension}"/>
<variable name="user_context" value="rooms-main"/>
<variable name="effective_caller_id_name" value="room house 1 apart 1"/>
<variable name="effective_caller_id_number" value="room-1-1"/>
<variable name="outbound_caller_id_name" value="$${{outbound_caller_name}}"/>
<variable name="outbound_caller_id_number" value="$${{outbound_caller_id}}"/>
<variable name="sip-force-contact" value="NDLB-connectile-dysfunction"/>
<variable name="sip-force-expires" value="7200"/>
<variable name="X-Doma-AI-Type" value="apartment"/>
</variables>
</user>
"""
def fs_cli(cmd):
password = os.getenv('SOCKET_PASSWORD').strip()
try:
subprocess.run([
'docker', 'compose', 'exec', '-Td', 'sip-server',
'/usr/local/freeswitch/bin/fs_cli', '-rRS', '--password', password, '-x',
cmd
])
except Exception as e:
print('Exception while calling fs_cli:', e)
def reload_queue(login):
fs_cli(f"callcenter_config queue unload {login}@default")
fs_cli(f"callcenter_config queue load {login}@default")
def reload_xml():
print('Reloading XML...')
fs_cli(f"reloadxml")
def transliterate(s):
replacements = {
'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D', 'Е': 'E', 'Ё': 'Yo',
'Ж': 'Zh', 'З': 'Z', 'И': 'I', 'Й': 'Y', 'К': 'K', 'Л': 'L', 'М': 'M',
'Н': 'N', 'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T', 'У': 'U',
'Ф': 'F', 'Х': 'Kh', 'Ц': 'Ts', 'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Shch',
'Ъ': '', 'Ы': 'Y', 'Ь': '', 'Э': 'E', 'Ю': 'Yu', 'Я': 'Ya',
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'yo',
'ж': 'zh', 'з': 'z', 'и': 'i', 'й': 'y', 'к': 'k', 'л': 'l', 'м': 'm',
'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u',
'ф': 'f', 'х': 'kh', 'ц': 'ts', 'ч': 'ch', 'ш': 'sh', 'щ': 'shch',
'ъ': '', 'ы': 'y', 'ь': '', 'э': 'e', 'ю': 'yu', 'я': 'ya',
' .,;:-!?*&@#$%^()[]{}|\\\'"<>': '_',
}
for replace_from, replace_to in replacements.items():
for c in replace_from:
s = s.replace(c, replace_to)
# Original script had a bug, which adds a _ to the end of the string
return s + '_'
def import_dotenv():
if not os.path.exists('.env'):
return
print('Importing .env...')
with open('.env') as e:
for line in e:
key, value = line.split('=')
os.environ[key.strip()] = value.strip()
def read_existing_users():
if not os.path.exists(cc_filepath):
return []
print('Reading existing users...')
with open(cc_filepath, 'r') as cc:
content = cc.read()
root = ET.fromstring(content)
queues = root.findall('.//queues/queue')
names = [q.attrib['name'].split('@')[0] for q in queues]
return names
def read_input_users():
if not os.path.isdir(input_users_dirpath):
return []
print('Reading input users...')
result = {}
for group in os.listdir(input_users_dirpath):
fname = os.path.join(input_users_dirpath, group)
output_fname = transliterate(os.path.splitext(group)[0])
with open(fname) as f:
reader = csv.DictReader(f, delimiter=';')
result[output_fname] = list(reader)
return result
def generate_configs(users):
print('Generating configs...')
queues = []
for fname, group in users.items():
group_users = []
for user in group:
if user['kvartira'] == 'x':
user_xml = intercom_template.format(**user)
else:
user_xml = apartment_template.format(**user)
queues.append(queue_template.format(**user))
group_users.append(user_xml)
with open(os.path.join(output_users_dirpath, fname + '.xml'), 'w') as f:
f.write(users_teplate.format(users='\n'.join(group_users)))
with open(cc_filepath, 'w') as f:
f.write(conf_template.format(queues='\n'.join(queues)))
def main():
import_dotenv()
users = read_existing_users()
input_users = read_input_users()
generate_configs(input_users)
reload_xml()
new_users = {
row['extension']
for group in input_users.values()
for row in group
if row['kvartira'] != 'x'
} - set(users)
if not new_users:
print('No new users')
for new_user in new_users:
print(new_user)
reload_queue(new_user)
if __name__ == '__main__':
main()