def subscribe(update: Update, context: CallbackContext): user = update.effective_user chat = update.effective_chat message = update.effective_message if not BotTelegramCore.instance().is_admin(user.id, chat.id): message.reply_text(ADMIN_RESTRICTED) return try: observer = UserObserver.objects.get(chat_id=f"{chat.id}") except DoesNotExist: observer = UserObserver(f"{chat.username}", f"{chat.id}").save() subscribed_subjects = Subject.objects(observers=observer) avaliable_subjects = [ subject for subject in Subject.objects() if subject not in subscribed_subjects ] buttons_list = [ InlineKeyboardButton(subject.header, callback_data=json.dumps({ "type": "subscribe", "id": f"{subject.id}" })) for subject in avaliable_subjects ] menu = InlineKeyboardMarkup(build_menu(buttons_list, n_cols=1)) message.reply_text("<b>Avaliable subjects</b>", parse_mode='HTML', reply_markup=menu)
def subscriptions(update: Update, context: CallbackContext): user = update.effective_user chat = update.effective_chat message = update.effective_message if not BotTelegramCore.instance().is_admin(user.id, chat.id): message.reply_text(ADMIN_RESTRICTED) return try: observer = UserObserver.objects.get(chat_id=f"{chat.id}") except DoesNotExist: message.reply_text(f"Observer with chat_id {chat.id} doesn't exist!\n" f"Call /subscribe before") return subscribed_subjects = Subject.objects(observers=observer) text = "<b>Subscribed subjects</b>\n\n" for index, subject in enumerate(subscribed_subjects): text += '-' text += subject.__str__() text += '\n' if index != len(subscribed_subjects) else '' message.reply_text(text, parse_mode='HTML')
def unsubscribe(update: Update, context: CallbackContext): user = update.effective_user chat = update.effective_chat message = update.effective_message if not BotTelegramCore.instance().is_admin(user.id, chat.id): message.reply_text(ADMIN_RESTRICTED) return try: observer = UserObserver.objects.get(chat_id=f"{chat.id}") except DoesNotExist: message.reply_text(f"Observer with chat_id {chat.id} doesn't exist!\n" f"Call /subscribe before") return subscribed_subjects = Subject.objects(observers=observer) buttons_list = [ InlineKeyboardButton(subject.header, callback_data=json.dumps({ "type": "unsubscribe", "id": f"{subject.id}" })) for subject in subscribed_subjects ] menu = InlineKeyboardMarkup(build_menu(buttons_list, n_cols=1)) message.reply_text("<b>Subscribed subjects</b>", parse_mode='HTML', reply_markup=menu)
def create_all(cls): subjects = Subject.objects() for subject in subjects: cls(subject)