реализация кнопки старт

main
pilot 3 months ago
parent a3018a60ac
commit 06ed1c6c20

@ -2,6 +2,9 @@ import json
import requests
from max_bot.models import Client
from restoran_max_bot.common import common_get_menu
def maxbot_send_text_message(chat_id: str, max_token: str, message: str):
url = f"https://platform-api.max.ru/messages?chat_id={chat_id}"
@ -43,49 +46,45 @@ def maxbot_send_img_message(chat_id: str, max_token: str, message: str, img: str
return rt
def maxbot_send_menu_button(chat_id: str, max_token: str, message: str, buton: list):
def maxbot_send_menu_button(client: Client, chat_id: str, max_token: str, message: str):
url = f"https://platform-api.max.ru/messages?chat_id={chat_id}"
menu = common_get_menu(client=client)
buttons = []
button = []
cnt = 0
for mn in menu:
if cnt == 2:
cnt = 0
buttons.append(button)
button = []
if mn.emojii:
emojii = str(mn.emojii)
else:
emojii = ''
button_data = {"type": "callback", "text": " ".join([emojii, str(mn.title)]), "payload": mn.title}
button.append(button_data)
cnt = cnt + 1
buttons.append(button)
payload = json.dumps({
"text": message,
"attachments": [
{"type": "inline_keyboard",
"payload": {
"buttons": [
[
{
"type": "link",
"text": "Откройте сайт",
"url": "https://example.com"
},
{
"type": "message",
"text": "type message",
"url": "https://example.com"
}
],
[
{
"type": "clipboard",
"text": "type clipboard",
"payload": "test"
},
{
"type": "callback",
"text": " 🍺 callback",
"payload": "TEEEETE payload"
}
]
]
"buttons": buttons
}
}
]
})
headers = {
'Authorization': f'{max_token}',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
rt = {'success': True, 'error': '', 'data': response}
@ -128,3 +127,31 @@ def maxbot_delete_all_messages(chat_id: str, max_token: str):
maxbot_del_messages(max_token=max_token, message_id=dt['body']['mid'])
return True
def maxbot_get_phone(chat_id: str, max_token: str, text: str):
url = f"https://platform-api.max.ru/messages?chat_id={chat_id}"
payload = json.dumps({
"text": text,
"attachments": [
{"type": "inline_keyboard",
"payload": {
"buttons": [
[
{"type": "request_contact", "text": "Отправить номер"}
]
]
}
}
]
})
headers = {
'Authorization': f'{max_token}',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
rt = {'success': True, 'error': '', 'data': response}
return rt

@ -70,10 +70,10 @@ def api_start_max_v1(request, **kwargs):
# получение сообщения из бота
if data['update_type'] == 'message_created':
bot_message(client=client, message=data, settings=settings_max)
bot_message(client=client, message=data, settings=settings_max, message_type='message_created')
if data['update_type'] == 'message_callback':
bot_message(client=client, message=data, settings=settings_max)
bot_message(client=client, message=data, settings=settings_max, message_type='message_callback')
# start - запуск бота
if data['update_type'] == 'bot_started':

@ -1,15 +1,19 @@
from max_bot.max_api import maxbot_send_text_message, maxbot_send_img_message, maxbot_send_menu_button, \
maxbot_delete_all_messages
from max_bot.max_api import *
from max_bot.models import Client
from restoran_max_bot.common import common_get_data
def bot_message(client: Client, message: dict, settings: dict):
def bot_message(client: Client, message: dict, settings: dict, message_type):
chat_id = message['message']['recipient']['chat_id']
chat_type = message['message']['recipient']['chat_type']
text = str(message['message']['body']['text']).lower()
data = common_get_data(client=client, key=text)
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'])
for dt in data:
if dt.img:
url = f"https://cdn.telefon-ip.ru/{dt.img}?thumb=600"
@ -17,6 +21,9 @@ def bot_message(client: Client, message: dict, settings: dict):
else:
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=dt.descr)
# отправка меню
rs = maxbot_send_menu_button(chat_id=chat_id, max_token=settings['token'], message='test', buton=[])
# print(rs['data'].text)
maxbot_send_menu_button(chat_id=chat_id, max_token=settings['token'], message='---', client=client)
else:
# отправка меню
maxbot_send_menu_button(chat_id=chat_id, max_token=settings['token'], message='Выберите раздел', client=client)
return True

@ -1,10 +1,21 @@
from max_bot.max_api import maxbot_send_text_message
from max_bot.max_api import *
from max_bot.models import Client
from restoran_max_bot.common import common_get_data
def bot_started(client: Client, message: dict, settings: dict):
chat_id = message['message']['recipient']['chat_id']
chat_type = message['message']['recipient']['chat_type']
text = str(message['message']['body']['text']).lower()
maxbot_send_text_message(chat_id=chat_id, max_token=settings['token'], message=text)
chat_id = message['chat_id']
name = message['user']['name']
user_id = message['user']['user_id']
data = common_get_data(client=client, key='start')
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)
# отправка меню
maxbot_get_phone(chat_id=chat_id, max_token=settings['token'], text='Согласен на обработку')
return True

@ -9,3 +9,8 @@ def common_get_data(client: Client, key: str) -> Product:
else:
data = None
return data
def common_get_menu(client: Client) -> ProductCategory:
menu = ProductCategory.objects.filter(client=client, status=1, src=1, slug='menu').order_by('ord')
return menu

Loading…
Cancel
Save