-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitrix.py
406 lines (331 loc) · 15.7 KB
/
bitrix.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import re
import crest, whatsapp
def connector_activate(config_data, placement_options):
connector_value = placement_options.get('CONNECTOR')
line_value = placement_options.get('LINE')
connectors = config_data['bitrix']['connectors']
# Поиск коннектора
for connector_data in connectors:
if connector_data['connector_id'] == connector_value:
# Проверяем наличие списка lines и наличие line_value в нем
if 'lines' in connector_data and line_value in connector_data['lines']:
return {'error': f"Connector {connector_value} is already connected to line {line_value}."}
# Если line_value отсутствует, активируем коннектор для новой линии
connection_data = {
'CONNECTOR': connector_value,
'LINE': line_value,
'ACTIVE': 1
}
response = crest.call_api('POST', 'imconnector.activate', connection_data, config_data)
if 'result' in response:
# Добавляем line_value в список lines
if 'lines' not in connector_data:
connector_data['lines'] = []
connector_data['lines'].append(line_value)
config_value = config_data['bitrix']['config_key']
# Сохраняем обновленные данные конфигурации
crest.write_to_config(config_value, {'connectors': connectors}, 'bitrix')
return {'Success': f"{connector_value} connected to line {line_value}."}
return response
else:
# Если коннектор не найден, возвращаем ошибку
return {'error': f"Connector {connector_value} not found in the list."}
def send_message(config_data, message_params):
# Инициализация списка файлов
files = []
# Проверка наличия ключа 'file_url' в полученных данных
if 'file_url' in message_params:
# Добавление URL файла в список файлов
files.append({'url': message_params['file_url']})
message_data = {
'CONNECTOR': message_params['b24_connector'],
'LINE': message_params['b24_line'],
'MESSAGES': [
{
'user': {
'id': message_params['wa_id'],
'last_name': message_params['name'],
'phone': message_params['wa_id'],
'skip_phone_validate': 'Y'
},
'message': {
'text': message_params['body'],
'files': files
},
'chat': {
'id': message_params['chat_id'],
# 'url': f"https://web.whatsapp.com/send/?phone={message_params['wa_id']}"
}
}
]
}
return crest.call_api('POST', 'imconnector.send.messages', message_data, config_data)
def send_status_delivery(config_data, status_data):
status_data = {
'CONNECTOR': status_data['connector_id'],
'LINE': status_data['line_id'],
'MESSAGES': [
{
'im': {
'chat_id': status_data['chat_id'],
'message_id': status_data['message_id']
},
'message': {
'id': []
},
'chat': {
'id': ''
}
}
]
}
return crest.call_api('POST', 'imconnector.send.status.delivery', status_data, config_data)
def process_chat_message(config_data, message_data):
try:
connector_id = message_data.get('data[CONNECTOR]')
line_id = message_data.get('data[LINE]')
# Определяем мессенджер для маршрутизации
messenger = find_messenger(config_data['messengers'], connector_id, line_id)
if messenger:
if messenger == 'whatsapp':
chat_id = message_data.get('data[MESSAGES][0][im][chat_id]')
file_type = message_data.get('data[MESSAGES][0][message][files][0][type]')
file_link = message_data.get('data[MESSAGES][0][message][files][0][link]')
user_id = message_data.get('data[MESSAGES][0][message][user_id]')
chat_message = {
'biz_opaque_callback_data': f'user_{user_id}'
}
if not file_type:
chat_message['type'] = 'text'
message_text = message_data.get('data[MESSAGES][0][message][text]')
text = re.sub(r'\[(?!(br|\n))[^\]]+\]', '', message_text)
text = text.replace('[br]', '\n')
chat_message['text'] = {'body': text}
# elif file_type in ['image', 'video', 'audio']:
elif file_type in ['image']:
chat_message['type'] = file_type
chat_message[file_type] = {'link': file_link}
else:
chat_message['type'] = 'document'
chat_message['document'] = {}
chat_message['document']['link'] = file_link
chat_message['document']['filename'] = message_data.get('data[MESSAGES][0][message][files][0][name]')
user_list = crest.call_api('GET', 'im.chat.user.list', {'CHAT_ID': chat_id}, config_data)
if user_list:
get_users_info = crest.call_api('POST', 'im.user.list.get', {'ID': user_list['result']}, config_data)
personal_mobile = get_personal_mobile(get_users_info['result'])
connector_data = {
'connector_id': connector_id,
'line_id': line_id
}
response = whatsapp.send_message(config_data, personal_mobile, chat_message, connector_data)
if not 'error' in response:
status_data = {
'message_id': message_data.get('data[MESSAGES][0][im][message_id]'),
'chat_id': chat_id,
'connector_id': connector_id,
'line_id': line_id
}
send_status_delivery(config_data, status_data)
return response
else:
return f'Мессенджер не найден для коннектора {connector_id} и линии {line_id}'
except Exception as e:
return {'error': str(e)}
def find_messenger(messengers, connector_id, line_id):
# Перебор всех мессенджеров в конфигурации
for messenger_name, messenger_data in messengers.items():
# Проверка каждой записи в списке мессенджера на совпадение с connector_id и line_id
for entry in messenger_data:
if entry.get('connector_id') == connector_id and str(entry.get('line_id')) == str(line_id):
return messenger_name
return False
def get_personal_mobile(users):
personal_mobiles = []
for user_id, user_info in users.items():
if user_info.get('external_auth_id') == 'imconnector':
phones = user_info.get('phones', {})
personal_mobile = phones.get('personal_mobile')
if personal_mobile:
personal_mobiles.append(personal_mobile)
return personal_mobiles
def uploadfile(config_data, file_content_base64, filename):
bitrix_data = config_data['bitrix']
file_data = {
'id': bitrix_data['storage_id'],
'fileContent': file_content_base64,
'data': {'NAME': filename}
}
return crest.call_api('POST', 'disk.storage.uploadfile', file_data, config_data)
def get_storage(config_data):
config_value = config_data['bitrix']['config_key']
get_storage_data = crest.call_api('POST', 'disk.storage.getforapp', {}, config_data)
if 'result' in get_storage_data:
storage_id = get_storage_data['result']['ID']
if crest.write_to_config(config_value, {'storage_id': storage_id}, 'bitrix'):
return f'ID {storage_id} хранилища успешно записан в конфигурацию'
else:
return f'Ошибка получения данных хранилища {get_storage_data}'
def line_disconnection(config_data, event_data):
bitrix_data = config_data['bitrix']
event_value = event_data.get('event')
line_value = event_data.get('data[line]')
connector_value = event_data.get('data[connector]')
connectors = bitrix_data['connectors']
if event_value == 'ONIMCONNECTORSTATUSDELETE':
# Удаляем line_value только из конкретного коннектора
for connector in connectors:
if connector['connector_id'] == connector_value and 'lines' in connector:
if line_value in connector['lines']:
connector['lines'].remove(line_value)
response = f"Line {line_value} disconnected from connector {connector_value}."
elif event_value == 'ONIMCONNECTORLINEDELETE':
# Удаляем line_value из всех коннекторов
for connector in connectors:
if 'lines' in connector and line_value in connector['lines']:
connector['lines'].remove(line_value)
response = f"Line {line_value} disconnected from all connectors."
# Сохранение обновленных данных конфигурации
config_value = bitrix_data['config_key']
if crest.write_to_config(config_value, {'connectors': connectors}, 'bitrix'):
return response
def messageservice_processing(config_data, message_data):
messageservice_code = message_data.get('code')
if messageservice_code:
messenger_type, whatsapp_data = get_messenger_type_by_id(config_data, messageservice_code)
if not messenger_type:
print('Messenger not found')
return
message_to = message_data.get('message_to')
message_body = message_data.get('message_body')
user_id = message_data.get('auth[user_id]')
if messenger_type == 'whatsapp' and whatsapp_data:
message = {
'type': 'text',
'text': {
'body': message_body
},
'biz_opaque_callback_data': f'user_{user_id}'
}
whatsapp.send_message(config_data, [message_to], message, whatsapp_data=whatsapp_data)
else:
print(f'Messenger type {messenger_type} is not supported or whatsapp_data is missing')
def get_messenger_type_by_id(config_data, messenger_id):
messengers = config_data.get('messengers', {})
for messenger_type, messenger_list in messengers.items():
for messenger in messenger_list:
if messenger.get('messenger_id') == messenger_id and messenger_type == "whatsapp":
whatsapp_data = {
"access_token": messenger.get("access_token"),
"phone_id": messenger.get("phone_id")
}
return messenger_type, whatsapp_data
elif messenger.get('messenger_id') == messenger_id:
# Для неватсап мессенджеров возвращаем только тип
return messenger_type, None
return None, None
def send_notification(config_data, data):
notification_data = {
**data,
'ATTACH': []
}
crest.call_api('POST', 'im.notify.system.add', notification_data, config_data)
# Интеграция с Asterisk
import configparser
import json
import time
config = configparser.ConfigParser()
config.read('config.ini')
CONFIG_FILE = config.get('bitrix', 'config_value')
CONFIG_DATA = crest.get_params(CONFIG_FILE)
BITRIX_USERS_FILE = 'bitrix_users.json'
DEFAULT_USER_ID = config.get('bitrix', 'default_user_id')
CRM_CREATE = config.get('bitrix', 'crm_create')
SHOW_CARD = config.get('bitrix', 'show_card')
def update_bitrix_users_file():
start = 0
bitrix_users = {}
while True:
response = crest.call_api('POST', 'user.get', {'ACTIVE': 'true', 'start': start}, CONFIG_DATA)
if 'result' in response:
users = response['result']
for user in users:
if user.get('UF_PHONE_INNER'):
bitrix_users[user.get('ID')] = user.get('UF_PHONE_INNER')
start += len(users)
if 'next' not in response:
break
else:
print('Ошибка при получении списка пользователей', response)
break
with open(BITRIX_USERS_FILE, 'w') as file:
json.dump(bitrix_users, file)
def get_user_info(user_id=None, user_phone=None):
for _ in range(2):
try:
with open(BITRIX_USERS_FILE, 'r') as file:
bitrix_users = json.load(file)
except Exception:
bitrix_users = {}
update_bitrix_users_file()
continue
if user_phone:
for key, value in bitrix_users.items():
if value == user_phone:
return key, False
if user_id:
if user_id in bitrix_users:
return bitrix_users[user_id], False
break
default_value = DEFAULT_USER_ID if DEFAULT_USER_ID else next(iter(bitrix_users.keys()), None)
return default_value, True
# Регистрация звонка в Битрикс24
def register_call(bitrix_user_id, phone_number, call_type):
register_param = {
'USER_ID': bitrix_user_id,
'PHONE_NUMBER': phone_number,
'TYPE': call_type,
'SHOW': SHOW_CARD,
'CRM_CREATE': CRM_CREATE
}
call_data = crest.call_api('POST', 'telephony.externalcall.register', register_param, CONFIG_DATA)
if 'result' in call_data:
return call_data['result']['CALL_ID']
else:
print('ОШИБКА!!!!! register_call', phone_number, call_data)
# Завершение звонка
def finish_call(call_data, config_data=None):
finish_param = {
'CALL_ID': call_data.get('bitrix_call_id'),
'USER_ID': call_data.get('bitrix_user_id'),
'DURATION': round(time.time() - call_data.get('start_time', time.time())),
'STATUS_CODE': call_data.get('call_status')
}
if None in finish_param.values(): # Проверяем, есть ли None среди значений
print('Один из параметров равен None')
return False
config_to_use = config_data if config_data is not None else CONFIG_DATA
response = crest.call_api('POST', 'telephony.externalcall.finish', finish_param, config_to_use)
if 'result' in response:
return True
else:
print('ОШИБКА finish_call', response)
return False
# Отправка файла записи
def attachRecord(call_data, encoded_file):
file_data = {
'CALL_ID': call_data["bitrix_call_id"],
'FILENAME': call_data["file_name"],
'FILE_CONTENT': encoded_file
}
response = crest.call_api('POST', 'telephony.externalCall.attachRecord', file_data, CONFIG_DATA)
# Показать/скрыть карточку
def card_action(call_id, user_id, action, config_data=None):
if SHOW_CARD != '1' and action == 'show':
return
call_data = {
'CALL_ID': call_id,
'USER_ID': user_id
}
config_to_use = config_data if config_data is not None else CONFIG_DATA
response = crest.call_api('POST', f'telephony.externalcall.{action}', call_data, config_to_use)