示例#1
0
 def _remove_last_message_from_subject(self, subject):
     for om in self.messages:
         try:
             if om.subject == subject:
                 BotTelegramCore.delete_message(self.chat_id, om.message_id)
                 self._delete_message(om)
         except (AttributeError, BadRequest):
             self._delete_message(om)
示例#2
0
文件: base.py 项目: dcrbrdev/JackBot
def config_handlers(instance: BotTelegramCore):
    logger.info('Setting base commands...')

    instance.add_handler(
        CommandHandler(
            "start", lambda update, context: update.message.reply_text(START)))

    instance.add_handler(
        CommandHandler(
            "help", lambda update, context: update.message.reply_text(HELP)))
示例#3
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')
示例#4
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)
示例#5
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)
示例#6
0
    def _send_update_message(self, update_message):
        subject = update_message.subject

        official_observer = Observer.get_official_observer()
        last_official_message = ObserverMessage\
            .objects(subject=subject,
                     observer=official_observer)\
            .order_by('-datetime').first()
        try:
            telegram_message = BotTelegramCore.forward_message(
                to_chat_id=self.chat_id,
                from_chat_id=official_observer.chat_id,
                message_id=last_official_message.message_id)
            self._create_message(telegram_message.message_id, subject)
        except BadRequest:
            last_official_message.delete()
            update_message.delete()
示例#7
0
def config_handlers(instance: BotTelegramCore):
    logger.info("Setting ticket commands...")

    instance.add_handler(CommandHandler("add_ticket", add_ticket))
    instance.add_handler(CommandHandler("remove_ticket", remove_ticket))
    instance.add_handler(CommandHandler("list_tickets", list_tickets))
示例#8
0
def config_handlers(instance: BotTelegramCore):
    logger.info("Setting subscription commands...")

    instance.add_handler(CommandHandler("subscribe", subscribe))
    instance.add_handler(CommandHandler("unsubscribe", unsubscribe))
    instance.add_handler(CommandHandler("subscriptions", subscriptions))
示例#9
0
def config_handlers(instance: BotTelegramCore):
    logger.info('Setting exchange commands...')

    instance.add_handler(CommandHandler("dcr", dcr))
示例#10
0
def config_handlers(instance: BotTelegramCore):
    logging.info('Setting callback handler...')

    instance.add_handler(CallbackQueryHandler(handle_callback))
示例#11
0
 def get_official_observer(cls):
     return cls.objects.get(chat_id=BotTelegramCore.instance().chat_id)
示例#12
0
 def send_message(self, message):
     return BotTelegramCore.send_message(f'{message}', chat_id=self.chat_id)