예제 #1
0
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)
예제 #2
0
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')
예제 #3
0
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)
예제 #4
0
파일: session.py 프로젝트: dcrbrdev/JackBot
 def create_all(cls):
     subjects = Subject.objects()
     for subject in subjects:
         cls(subject)