diff --git a/restoran_max_bot/max_bot/models.py b/restoran_max_bot/max_bot/models.py index b796267..74784ab 100644 --- a/restoran_max_bot/max_bot/models.py +++ b/restoran_max_bot/max_bot/models.py @@ -149,6 +149,7 @@ class Users(models.Model): # Список контактов чате class Contact(models.Model): + objects = None up = models.IntegerField() group = models.IntegerField() client = models.ForeignKey(to='Client', on_delete=models.PROTECT, verbose_name=' id client') @@ -564,6 +565,7 @@ class Product(models.Model): title = models.CharField(max_length=200, null=True, help_text='Наименование продукции') descr = models.CharField(max_length=200, null=True, help_text='Описание продукции') img = models.CharField(max_length=200, null=True, help_text='Картинка продукции') + url = models.CharField(max_length=200, null=True, help_text='Картинка продукции') price = models.FloatField(help_text='Стоимотсь продукции') sku = models.CharField(max_length=100, null=True, help_text='идентификационный код товарной позиции у партнера') status = models.IntegerField() diff --git a/restoran_max_bot/restoran_max_bot/bot_message.py b/restoran_max_bot/restoran_max_bot/bot_message.py index 3b895f9..56bf68e 100644 --- a/restoran_max_bot/restoran_max_bot/bot_message.py +++ b/restoran_max_bot/restoran_max_bot/bot_message.py @@ -1,25 +1,50 @@ from max_bot.max_api import * from max_bot.models import Client -from restoran_max_bot.common import common_get_data +from restoran_max_bot.common import common_get_data, common_get_contact + + +# проверка есть ли регистрация клиента +def get_registration(client: Client, chat_id: str, settings: dict): + contact = common_get_contact(client=client, uid_client=chat_id) + if not contact: + data = common_get_data(client=client, key='согласие'.lower()) + if data: + for dt in data: + if dt.img: + 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) + else: + maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.descr) + if dt.url: + maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.url) + + maxbot_get_phone(chat_id=chat_id, max_token=settings['token'], text='Подтвердите согласие') + return False + return True def bot_message(client: Client, message: dict, settings: dict, message_type): chat_id = message['message']['recipient']['chat_id'] - data = None + if not get_registration(client=client, chat_id=chat_id, settings=settings): + return False + + data = None if message_type == 'message_created': data = common_get_data(client=client, key=str(message['message']['body']['text']).lower()) if message_type == 'message_callback': data = common_get_data(client=client, key=str(message['callback']['payload']).lower()) if data: - maxbot_delete_all_messages(chat_id=chat_id, max_token=settings['token']) + # maxbot_delete_all_messages(chat_id=chat_id, max_token=settings['token']) for dt in data: if dt.img: 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) else: maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.descr) + if 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) else: diff --git a/restoran_max_bot/restoran_max_bot/common.py b/restoran_max_bot/restoran_max_bot/common.py index a1c3037..ced7496 100644 --- a/restoran_max_bot/restoran_max_bot/common.py +++ b/restoran_max_bot/restoran_max_bot/common.py @@ -1,5 +1,7 @@ # функция поиска контента по ключевому слову -from max_bot.models import Client, ProductCategory, Product + + +from max_bot.models import Client, ProductCategory, Product, Contact def common_get_data(client: Client, key: str) -> Product: @@ -14,3 +16,25 @@ def common_get_data(client: Client, key: str) -> Product: def common_get_menu(client: Client) -> ProductCategory: menu = ProductCategory.objects.filter(client=client, status=1, src=1, slug='menu').order_by('ord') return menu + + +def common_get_group_contact(client: Client, name: str) -> Contact: + group = Contact.objects.filter(client=client, up=0, name=name).first() + if not group: + group = Contact.objects.create( + client=client, + status=1, + name=name, + up=0, + group=1 + ) + if group.status != 1: + group.status = 1 + group.save() + return group + + +def common_get_contact(client: Client, uid_client: str) -> Contact: + up = common_get_group_contact(client=client, name='max-bot').pk + contact = Contact.objects.filter(client=client, status=1, maxx=uid_client, up=up).first() + return contact diff --git a/restoran_max_bot/restoran_max_bot/iiko_api.py b/restoran_max_bot/restoran_max_bot/iiko_api.py new file mode 100644 index 0000000..30e325b --- /dev/null +++ b/restoran_max_bot/restoran_max_bot/iiko_api.py @@ -0,0 +1,151 @@ +import requests +import json + + +# https://api-ru.iiko.services/docs +# получение токена +def access_token(api_key): + url = "https://api-ru.iiko.services/api/1/access_token" + payload = json.dumps({ + "apiLogin": api_key + }) + headers = { + 'Content-Type': 'application/json' + } + + response = requests.request("POST", url, headers=headers, data=payload) + data = response.json() + if response.status_code == 200: + return data['token'] + else: + return False + + +def iiko_organizations(api_key, organization_id): + url = "https://api-ru.iiko.services/api/1/organizations" + payload = json.dumps({ + }) + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + access_token(api_key) + } + + response = requests.request("POST", url, headers=headers, data=payload) + if response.status_code == 200: + data = response.json() + else: + data = False + return data + + +def customer_create(api_key, phone, name, organization_id): + url = "https://api-ru.iiko.services/api/1/loyalty/iiko/customer/create_or_update" + + payload = json.dumps({ + "organizationId": organization_id, + "phone": phone, + "name": name, + "sex": 0, + "userData": "telegram bot" + }) + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + access_token(api_key) + } + response = requests.request("POST", url, headers=headers, data=payload) + if response.status_code == 200: + data = response.json() + else: + data = False + + return data + + +def customer_info(api_key, phone, organization_id): + url = "https://api-ru.iiko.services/api/1/loyalty/iiko/customer/info" + payload = json.dumps({ + "phone": phone, + "type": "phone", + "organizationId": organization_id + }) + + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + access_token(api_key) + } + response = requests.request("POST", url, headers=headers, data=payload) + if response.status_code == 200: + data = response.json() + else: + data = False + + return data + + +# получение терминала +def iiko_wallet(api_key, organization_id): + url = "https://api-ru.iiko.services/api/1/terminal_groups" + payload = json.dumps({ + "organizationIds": [organization_id] + }) + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + access_token(api_key) + } + + response = requests.request("POST", url, headers=headers, data=payload) + if response.status_code == 200: + data = response.json() + else: + data = False + return data + + +# пополнение баланса клиента +def customer_wallet(api_key, iiko_wallet_id, customer_id, organization_id, bonus, comment="telegram balance"): + url = "https://api-ru.iiko.services/api/1/loyalty/iiko/customer/wallet/topup" + payload = json.dumps({ + "customerId": customer_id, + "walletId": iiko_wallet_id, + "sum": bonus, + "comment": comment, + "organizationId": organization_id + }) + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + access_token(api_key) + } + + response = requests.request("POST", url, headers=headers, data=payload) + if response.status_code == 200: + rs = response.json() + else: + print('Erorr customer_wallet:', response.status_code, response.text) + rs = False + + return rs + + +# списание баланса у клиента +def chargeoff_wallet(api_key, iiko_wallet_id, customer_id, organization_id, bonus, comment="telegram balance delete"): + url = "https://api-ru.iiko.services/api/1/loyalty/iiko/customer/wallet/chargeoff" + payload = json.dumps({ + "customerId": customer_id, + "walletId": iiko_wallet_id, + "sum": bonus, + "comment": comment, + "organizationId": organization_id + }) + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + access_token(api_key) + } + + response = requests.request("POST", url, headers=headers, data=payload) + if response.status_code == 200: + rs = response.json() + else: + print('Erorr customer_wallet:', response.status_code, response.text) + rs = False + + return rs diff --git a/restoran_max_bot/restoran_max_bot/rkiper_api.py b/restoran_max_bot/restoran_max_bot/rkiper_api.py new file mode 100644 index 0000000..617065e --- /dev/null +++ b/restoran_max_bot/restoran_max_bot/rkiper_api.py @@ -0,0 +1,26 @@ +import requests +import json +from restoran_max_bot.utils import normalize_mobile_phone + + +def rkiper_customer_info(rkiper_server, rkiper_token, phone): + url = f"http://{rkiper_server}/accounts/searchv2" + phone = normalize_mobile_phone(phone) + phone = '*' + phone[1:] + payload = json.dumps({ + "case_sensitive": False, + "fields": [ + "email", + "tel1" + ], + "limit": 100, + "mask": f"{phone}", + "match_all": False + }) + headers = { + 'Content-Type': 'application/json', + 'X-API-Key': rkiper_token + } + + response = requests.request("POST", url, headers=headers, data=payload).json() + return response diff --git a/restoran_max_bot/restoran_max_bot/utils.py b/restoran_max_bot/restoran_max_bot/utils.py index bd93b7d..7c21615 100644 --- a/restoran_max_bot/restoran_max_bot/utils.py +++ b/restoran_max_bot/restoran_max_bot/utils.py @@ -89,3 +89,5 @@ def is_json(myjson): except ValueError as e: return False return True + +