-
Notifications
You must be signed in to change notification settings - Fork 0
/
poem.py
60 lines (51 loc) · 2.44 KB
/
poem.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
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ParseMode
from telegram.ext import ContextTypes
import const
from db import DataBase
from recitation import Recitation
from config import settings
from util import Util
class Poem:
@staticmethod
async def show_poem_by_id(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
poem_id = int(query.data.split(':')[1])
poem_text = DataBase().get_poem_text(poem_id)
new_poem_text = Util.break_long_verses(poem_text)
poem_info = DataBase().get_poem_info(poem_id)
messages = Util.break_long_poems(new_poem_text, poem_info)
for message in messages:
await context.bot.send_message(query.from_user.id, message, parse_mode=ParseMode.HTML)
if recitation_info := Recitation.get_recitation_info(poem_id):
await context.bot.send_audio(query.from_user.id, recitation_info[0], performer=recitation_info[2],
title=recitation_info[1])
@staticmethod
async def category_poems(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
category_id = int(query.data.split(':')[1])
offset = int(query.data.split(':')[2])
poems: list = DataBase().get_category_poems(category_id, offset)
buttons = list()
for i in range(0, len(poems), 2):
row = list()
for poem in poems[i: i + 2]:
button = InlineKeyboardButton(poem[2], callback_data=f'poem:{poem[0]}')
row.append(button)
buttons.append(row)
last_row = []
if offset != 0:
last_row.append(
InlineKeyboardButton("قبلی", callback_data=f'category:{category_id}:{offset - settings.POEM_PER_PAGE}'))
if len(poems) == settings.POEM_PER_PAGE:
last_row.append(
InlineKeyboardButton("بعدی", callback_data=f'category:{category_id}:{offset + settings.POEM_PER_PAGE}'))
if last_row:
buttons.append(last_row)
menu = InlineKeyboardMarkup(buttons)
if update.effective_message.text == const.POEMS.strip():
await update.effective_message.edit_reply_markup(menu)
else:
await context.bot.send_message(query.from_user.id, const.POEMS, reply_markup=menu)