viber-replace-bot/handlers.py

113 lines
4.0 KiB
Python
Raw Normal View History

2023-03-04 00:40:08 +03:00
import io
import base64
from flask import request, Response, send_file
from viberbot.api.messages.url_message import URLMessage
from viberbot.api.messages.text_message import TextMessage
from viberbot.api.messages.picture_message import PictureMessage
from viberbot.api.messages.keyboard_message import KeyboardMessage
from viberbot.api.viber_requests import ViberMessageRequest
from viberbot.api.viber_requests import ViberConversationStartedRequest
from load import app, logger, viber, config
from config import parse_bool
from parser.parser import get_about_replacements, docs_parse
ENABLE_PARSER = parse_bool(text=config.parser.enable_parse)
KEYBOARD = {
"Type": "keyboard",
"Buttons": [
{
"Columns": 6,
"Rows": 1,
"BgColor": "#e6f5ff",
"BgLoop": True,
"ActionType": "reply",
"ActionBody": "replaces",
#"ReplyType": "message",
"Text": "Заміни"
},
{
"Columns": 3,
"Rows": 2,
"BgColor": "#e6f5ff",
"BgLoop": True,
"ActionType": "reply",
"ActionBody": "link",
#"ReplyType": "message",
"Text": "Посилання на файл"
}
]
}
if ENABLE_PARSER:
KEYBOARD["Buttons"].append({
"Columns": 3,
"Rows": 2,
"BgColor": "#e6f5ff",
"BgLoop": True,
"ActionType": "reply",
"ActionBody": "update",
#"ReplyType": "message",
"Text": "Обновити дані"
})
else:
KEYBOARD["Buttons"][1]["Columns"] = 6
def get_text_button(index: int):
return KEYBOARD["Buttons"][index]["ActionBody"]
@app.route('/', methods=['POST'])
def incoming():
logger.debug("received request. post data: {0}".format(request.get_data()))
viber_request = viber.parse_request(request.get_data().decode('utf8'))
if isinstance(viber_request, ViberConversationStartedRequest):
viber.send_messages(viber_request.user.id, [
KeyboardMessage(keyboard=KEYBOARD)
])
if isinstance(viber_request, ViberMessageRequest):
message = viber_request.message.text
if message == "id": viber.send_messages(viber_request.sender.id, [TextMessage(text=viber_request.sender.id, keyboard=KEYBOARD)])
if message == get_text_button(0):
data = get_about_replacements()
if 'image' in data:
viber.send_messages(viber_request.sender.id, [
PictureMessage(
media=f"{config.webhook.webhook_url}/image",
keyboard=KEYBOARD
)
])
elif message == get_text_button(1):
viber.send_messages(viber_request.sender.id, [
URLMessage(media=config.parser.link, keyboard=KEYBOARD)
])
elif ENABLE_PARSER and message == get_text_button(2) and viber_request.sender.id:
if viber_request.sender.id not in config.parser.admins.split(","):
viber.send_messages(viber_request.sender.id, [TextMessage(text="Ви не адміністратор!")])
else:
try:
docs_parse()
viber.send_messages(viber_request.sender.id, [
TextMessage(text="Замены обновлены!", keyboard=KEYBOARD)])
except Exception as e:
print(e)
viber.send_messages(viber_request.sender.id, [
TextMessage(text='Ошибка обновления', keyboard=KEYBOARD)])
2023-03-15 19:15:51 +03:00
else:
viber.send_messages(viber_request.sender.id, [
TextMessage(text='Привіт!', keyboard=KEYBOARD)])
2023-03-04 00:40:08 +03:00
return Response(status=200)
@app.route('/image')
def get_image():
data = get_about_replacements()
if 'image' in data:
return send_file(io.BytesIO(base64.b64decode(data['data']['all'])), mimetype='image/jpeg')