def show_many_users(queue: Queue) -> str: """ Return string with first and last 2 users """ users_list = '' for user_id, first_name, last_name, _ in queue.show_first_user(2): users_list += f"[{user_id}] {first_name} {last_name}\n" users_list += '. . . . . . . .\n' for user_id, first_name, last_name, _ in reversed(queue.show_last_user(2)): users_list += f"[{user_id}] {first_name} {last_name}\n" return users_list
def view_queue(queue: Queue, message: Message) -> None: """ Function to see who is in line """ if not queue.queue_length(): bot.send_message(message.chat.id, "В очереди никого нет") else: if queue.queue_length() > 4: bot.send_message(message.chat.id, show_many_users(queue)) else: bot.send_message(message.chat.id, show_few_users(queue)) bot.register_next_step_handler(message, main)
def clear_queue(queue: Queue, message: Message) -> None: """ Remove all people from the queue """ if queue.queue_length(): queue.clear_queue() bot.send_message(message.chat.id, "Очередь очищена!", reply_markup=keyboard_admin) else: bot.send_message(message.chat.id, "В очереди никого нет") bot.register_next_step_handler(message, main)
async def create_handler(message: Message): title = message.text[8:] if title.replace(" ", "") == "": await message.reply("Empty title. Use /create [text]\n", reply=False) return session = Session() time = datetime.now() chat = get_chat(session, message.chat.id) delta = timedelta(minutes=chat.default_time) time += delta seconds = timedelta(time.second) time -= seconds queue = Queue(creator_id=message.from_user.id, message_id=message.message_id, pin_date=time, title=title, chat_id=message.chat.id) session.add(queue) session.commit() session.close() await message.reply( f"{title}\n\nPublication time: {time.strftime('%H:%M, %d.%m.%Y')}", reply=False)
def show_few_users(queue: Queue) -> str: """ Return string with all users """ users_list = '' for user_id, first_name, last_name, _ in queue.show_all_user(): users_list += f"[{user_id}] {first_name} {last_name}\n" return users_list
def get_in_line(queue: Queue, message: Message) -> None: """ Get to the end of the queue """ if message.from_user.id not in \ [telegram_id[3] for telegram_id in queue.show_all_user()]: bot.send_message(message.chat.id, 'Ваше имя и фамилия') bot.register_next_step_handler(message, get_full_name) else: bot.send_message(message.chat.id, "Ты уже в очереди!", reply_markup=keyboard_main) bot.register_next_step_handler(message, main_menu)
def delete_first(queue: Queue, message: Message) -> None: """ Delete a first person in the queue """ if queue.queue_length(): deleted_user = queue.delete_first_user() first_name, last_name = deleted_user[1], deleted_user[2] bot.send_message(message.chat.id, f"\"{first_name} {last_name}\" был удален", reply_markup=keyboard_admin) if queue.queue_length() > 0: bot.send_message(queue.show_first_user()[3], 'Сейчас твоя очередь!') if queue.queue_length() > 1: bot.send_message(queue.show_all_user()[1][3], "Приготовься, ты следуюющий)") else: bot.send_message(message.chat.id, "Некого удалять", reply_markup=keyboard_admin) bot.register_next_step_handler(message, main)
import os from typing import List from telebot.types import Message from database import Queue from keyboard import * from scraping_habr import get_articles, add_new_articles bot = telebot.TeleBot(os.getenv("BOT_TOKEN")) queue = Queue() NAME_DB = 'habr.db' ARTICLE_TABLE = 'habr_db' NEWS_TABLE = 'news' ARTICLE_URL = "https://habr.com/ru/all/" NEWS_URL = 'https://habr.com/ru/news/' def main(message: Message) -> None: """ The main function of queue """ text = message.text.lower() if text == "удалить первого" and is_admin(message): delete_first(queue, message) if text == "очистить очередь" and is_admin(message): clear_queue(queue, message) if text == "стать в очередь": get_in_line(queue, message) if text == "посмотреть": view_queue(queue, message)