|
|
|
@ -1,32 +1,92 @@
|
|
|
|
|
|
|
|
import re
|
|
|
|
from max_bot.max_api import *
|
|
|
|
from max_bot.max_api import *
|
|
|
|
from max_bot.models import Client
|
|
|
|
import qrcode
|
|
|
|
from restoran_max_bot.common import common_get_data, common_get_contact
|
|
|
|
from restoran_max_bot.bot_started import check_registration
|
|
|
|
|
|
|
|
from restoran_max_bot.common import *
|
|
|
|
|
|
|
|
from restoran_max_bot.iiko_api import customer_info, customer_create, customer_wallet
|
|
|
|
|
|
|
|
from restoran_max_bot.rkiper_api import rkiper_customer_info, rkiper_customer_create
|
|
|
|
|
|
|
|
from restoran_max_bot.utils import has_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# проверка есть ли регистрация клиента
|
|
|
|
# получение информации по интеграции с терминалом ikko, rkeeper
|
|
|
|
def get_registration(client: Client, chat_id: str, settings: dict):
|
|
|
|
def get_bonus(client: Client, phone: str, default_bonus: float, contact: Contact):
|
|
|
|
contact = common_get_contact(client=client, uid_client=chat_id)
|
|
|
|
integration = common_get_integration(client=client)
|
|
|
|
if not contact:
|
|
|
|
bonus = 0
|
|
|
|
data = common_get_data(client=client, key='согласие'.lower())
|
|
|
|
date = contact.field1
|
|
|
|
if data:
|
|
|
|
|
|
|
|
for dt in data:
|
|
|
|
if len(date) < 2:
|
|
|
|
if dt.img:
|
|
|
|
date = '0' + date
|
|
|
|
url = f"https://cdn.telefon-ip.ru/{dt.img}?thumb=600"
|
|
|
|
|
|
|
|
maxbot_send_img_message(chat_id=chat_id, max_token=settings['token'], img=url, message=dt.descr)
|
|
|
|
month = contact.field2
|
|
|
|
|
|
|
|
if len(month) < 2:
|
|
|
|
|
|
|
|
month = '0' + month
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if integration:
|
|
|
|
|
|
|
|
if integration.partner.slug == 'iiko':
|
|
|
|
|
|
|
|
config = json.loads(integration.setting)
|
|
|
|
|
|
|
|
customerinfo = customer_info(config['token'], phone, config['id_org'])
|
|
|
|
|
|
|
|
if not customerinfo:
|
|
|
|
|
|
|
|
birthday = f"1990-{month}-{date} 00:00:00.000"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dt = customer_create(api_key=config['token'], phone=phone, name=contact.name,
|
|
|
|
|
|
|
|
organization_id=config['id_org'], birthday=birthday)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dt = customer_info(config['token'], phone, config['id_org'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
customer_wallet(api_key=config['token'], iiko_wallet_id=dt['walletBalances'][0]['id'],
|
|
|
|
|
|
|
|
customer_id=dt['id'], organization_id=config['id_org'],
|
|
|
|
|
|
|
|
bonus=default_bonus)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bonus = default_bonus
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.descr)
|
|
|
|
bonus = customerinfo['walletBalances']
|
|
|
|
if dt.url:
|
|
|
|
bonus = bonus[0]['balance']
|
|
|
|
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.url)
|
|
|
|
|
|
|
|
|
|
|
|
# http://192.168.9.186:8890/docs
|
|
|
|
|
|
|
|
if integration.partner.slug == 'rkeeper':
|
|
|
|
|
|
|
|
config = json.loads(integration.setting)
|
|
|
|
|
|
|
|
rs = rkiper_customer_info(rkiper_server=config['url'], rkiper_token=config['token'], phone=phone)
|
|
|
|
|
|
|
|
if len(rs):
|
|
|
|
|
|
|
|
rs = rs[0]
|
|
|
|
|
|
|
|
if has_key(rs, 'card_use'):
|
|
|
|
|
|
|
|
bonus = int(rs['card_use']['sum1']) / 100
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
birthday = f"1990-{month}-{date}"
|
|
|
|
|
|
|
|
rs = rkiper_customer_create(rkiper_server=config['url'], rkiper_token=config['token'], phone=phone,
|
|
|
|
|
|
|
|
name=contact.name, birthday=birthday)
|
|
|
|
|
|
|
|
bonus = 0
|
|
|
|
|
|
|
|
return bonus
|
|
|
|
|
|
|
|
|
|
|
|
maxbot_get_phone(chat_id=chat_id, max_token=settings['token'], text='Подтвердите согласие')
|
|
|
|
|
|
|
|
return False
|
|
|
|
# получение вложения в сообщениях для их обработки (регистрация по номеру)
|
|
|
|
return True
|
|
|
|
def get_attachments(message: dict):
|
|
|
|
|
|
|
|
chat_id = message['message']['recipient']['chat_id']
|
|
|
|
|
|
|
|
if 'attachments' in message['message']['body']:
|
|
|
|
|
|
|
|
if 'vcf_info' in message['message']['body']['attachments'][0]['payload']:
|
|
|
|
|
|
|
|
# Регулярное выражение: ищем всё после 'TEL;TYPE=cell:' до \r или конца строки
|
|
|
|
|
|
|
|
match = re.search(r'TEL;TYPE=cell:(\d+)',
|
|
|
|
|
|
|
|
message['message']['body']['attachments'][0]['payload']['vcf_info'])
|
|
|
|
|
|
|
|
if match:
|
|
|
|
|
|
|
|
phone_number = match.group(1)
|
|
|
|
|
|
|
|
return {'attachments_type': 'vcf_info', 'data': phone_number}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {'attachments_type': False, 'data': ''}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bot_message(client: Client, message: dict, settings: dict, message_type):
|
|
|
|
def bot_message(client: Client, message: dict, settings: dict, message_type):
|
|
|
|
|
|
|
|
|
|
|
|
chat_id = message['message']['recipient']['chat_id']
|
|
|
|
chat_id = message['message']['recipient']['chat_id']
|
|
|
|
|
|
|
|
attachments = get_attachments(message=message)
|
|
|
|
|
|
|
|
|
|
|
|
if not get_registration(client=client, chat_id=chat_id, settings=settings):
|
|
|
|
# регистрируем по номеру телефона выдаем соответствующий контент
|
|
|
|
|
|
|
|
if attachments['attachments_type'] == 'vcf_info':
|
|
|
|
|
|
|
|
common_set_contact(client=client, uid_client=chat_id, name=message['message']['sender']['name'],
|
|
|
|
|
|
|
|
phone=attachments['data'])
|
|
|
|
|
|
|
|
message['message']['body']['text'] = 'system_registration'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contact = check_registration(client=client, chat_id=chat_id, message=message, settings=settings)
|
|
|
|
|
|
|
|
if not contact:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
data = None
|
|
|
|
data = None
|
|
|
|
@ -34,21 +94,35 @@ def bot_message(client: Client, message: dict, settings: dict, message_type):
|
|
|
|
data = common_get_data(client=client, key=str(message['message']['body']['text']).lower())
|
|
|
|
data = common_get_data(client=client, key=str(message['message']['body']['text']).lower())
|
|
|
|
|
|
|
|
|
|
|
|
if message_type == 'message_callback':
|
|
|
|
if message_type == 'message_callback':
|
|
|
|
data = common_get_data(client=client, key=str(message['callback']['payload']).lower())
|
|
|
|
key = json.loads(message['callback']['payload'])
|
|
|
|
|
|
|
|
data = common_get_data(client=client, key=str(key['data']).lower())
|
|
|
|
if data:
|
|
|
|
if data:
|
|
|
|
# maxbot_delete_all_messages(chat_id=chat_id, max_token=settings['token'])
|
|
|
|
|
|
|
|
for dt in data:
|
|
|
|
for dt in data:
|
|
|
|
|
|
|
|
if dt.title == '##bonus##':
|
|
|
|
|
|
|
|
img = qrcode.make(contact.phone)
|
|
|
|
|
|
|
|
img.save(f"./restoran_max_bot/static/client_qr/{contact.phone}.png")
|
|
|
|
|
|
|
|
url = f"https://maxbot.telefon-ip.ru/static/client_qr/{contact.phone}.png"
|
|
|
|
|
|
|
|
maxbot_send_img_message(chat_id=chat_id, max_token=settings['token'], img=url,
|
|
|
|
|
|
|
|
message='QR-код для начисления, списания бонусов.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bonus = get_bonus(client=client, phone=contact.phone, default_bonus=float(dt.price), contact=contact)
|
|
|
|
|
|
|
|
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'],
|
|
|
|
|
|
|
|
message=f"Ваши бонусы: {bonus} руб")
|
|
|
|
|
|
|
|
|
|
|
|
if dt.img:
|
|
|
|
if dt.img:
|
|
|
|
url = f"https://cdn.telefon-ip.ru/{dt.img}?thumb=600"
|
|
|
|
url = f"https://cdn.telefon-ip.ru/{dt.img}?thumb=600"
|
|
|
|
maxbot_send_img_message(chat_id=chat_id, max_token=settings['token'], img=url, message=dt.descr)
|
|
|
|
maxbot_send_img_message(chat_id=chat_id, max_token=settings['token'], img=url, message=dt.descr)
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
|
|
if '##' not in dt.title and not dt.img:
|
|
|
|
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.descr)
|
|
|
|
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.descr)
|
|
|
|
|
|
|
|
|
|
|
|
if dt.url:
|
|
|
|
if dt.url:
|
|
|
|
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.url)
|
|
|
|
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.url)
|
|
|
|
|
|
|
|
|
|
|
|
# отправка меню
|
|
|
|
# отправка меню
|
|
|
|
maxbot_send_menu_button(chat_id=chat_id, max_token=settings['token'], message='---', client=client)
|
|
|
|
maxbot_send_menu_button(chat_id=chat_id, max_token=settings['token'], message='👇 Выбери раздел', client=client)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# отправка меню
|
|
|
|
# отправка меню
|
|
|
|
maxbot_send_menu_button(chat_id=chat_id, max_token=settings['token'], message='Выберите раздел', client=client)
|
|
|
|
maxbot_send_menu_button(chat_id=chat_id, max_token=settings['token'], message='👇 Выбери раздел', client=client)
|
|
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|