示例#1
0
def main():
    """Run bot."""

    parser = arg_parser()
    storage = vars(parser.parse_args())

    updater = Updater(storage['token'])
    dispatcher = updater.dispatcher

    if storage['folder'] is None:
        imap_trace(select_email_folder, storage)()
    logging.info('Cmd line args storage: {0}'.format(storage))

    ChatFilter = Filters.chat(storage['chat'])

    dispatcher.add_handler(CommandHandler("start", start, ChatFilter))
    dispatcher.add_handler(CommandHandler("help", start, ChatFilter))
    dispatcher.add_handler(
        CommandHandler("set",
                       partial(set_timer, imap_trace(scan_email, storage)),
                       ChatFilter))
    dispatcher.add_handler(CommandHandler("unset", unset, ChatFilter))

    updater.start_polling()
    updater.idle()
示例#2
0
    def __init__(self, callback):

        self.callback = callback

        keyboard = [
            [
                InlineKeyboardButton("Open", callback_data='open'),
                #    InlineKeyboardButton("Option 2", callback_data='2')
            ],
            #[
            #    InlineKeyboardButton("Option 3", callback_data='3')
            #]
        ]
        self.reply_markup = InlineKeyboardMarkup(keyboard)

        self.updater = Updater(token=cfg.TELEGRAM_API_KEY, use_context=True)
        self.dispatcher = self.updater.dispatcher

        chat_filter = Filters.chat(chat_id=cfg.ALLOWED_CHAT)
        user_filter = Filters.user(cfg.ALLOWED_USERS)

        self.start_handler = CommandHandler("start", self.start_cmd)
        self.open_handler = CommandHandler("open",
                                           self.open_cmd,
                                           filters=(chat_filter
                                                    and user_filter))

        self.dispatcher.add_handler(self.start_handler)
        self.dispatcher.add_handler(self.open_handler)

        self.dispatcher.add_handler(CallbackQueryHandler(self.button_callback))

        self.main_thread = threading.Thread(target=self.thread_body)
        self.main_thread.start()
        return
示例#3
0
def add_roll(upd: Updater, handlers_group: int):
    logger.info("registering roll handlers")
    dp = upd.dispatcher
    dp.add_handler(MessageHandler(Filters.dice, roll), handlers_group)
    dp.add_handler(
        MessageHandler(Filters.regex(MEME_REGEX), roll, run_async=True), handlers_group
    )
    dp.add_handler(
        CommandHandler(
            "gdpr_me",
            satisfy_GDPR,
            filters=Filters.chat(username=get_group_chat_id().strip("@")),
            run_async=True,
        ),
        handlers_group,
    )
    dp.add_handler(
        CommandHandler(
            "hussars",
            show_hussars,
            filters=~Filters.chat(username=get_group_chat_id().strip("@"))
            | admin_filter,
            run_async=True,
        ),
        handlers_group,
    )
    dp.add_handler(
        CommandHandler(
            "htop",
            show_active_hussars,
            filters=admin_filter
            & Filters.chat(username=get_group_chat_id().strip("@")),
            run_async=True,
        ),
        handlers_group,
    )
    dp.add_handler(
        CommandHandler(
            "wipe_hussars",
            wipe_hussars,
            filters=admin_filter
            & Filters.chat(username=get_group_chat_id().strip("@")),
            run_async=True,
        ),
        handlers_group,
    )
 def __init__(self, dispatcher, chat_id):
     self.chat_id = chat_id
     if self.chat_id != None:
         dispatcher.add_handler(
             MessageHandler(
                 Filters.chat(self.chat_id)
                 & Filters.status_update.new_chat_members, self.new_member))
     else:
         dispatcher.add_handler(
             CommandHandler('get_chat_id', self.get_chat_id))
     dispatcher.add_error_handler(self.error)
示例#5
0
def main():
    updater = Updater(TOKEN)
    updater.dispatcher.add_handler(CommandHandler("start", start))
    updater.dispatcher.add_handler(CommandHandler("meme", meme))
    updater.dispatcher.add_handler(CommandHandler("help", help))
    updater.dispatcher.add_handler(
        MessageHandler(Filters.chat(chat_id=chat_id_of_channel),
                       channel_message))
    updater.dispatcher.add_handler(MessageHandler(Filters.text, reply_msg))
    updater.start_polling()
    updater.idle()
示例#6
0
class StartParsingHandler(Handler):
    filter = Filters.chat(
        chat_id=cfg.moderation_chat) & Filters.command & Filters.user(
            username='******')

    def handle(self, bot: Bot, update: Update):
        parsing.start()
        bot.send_message(chat_id=cfg.moderation_chat,
                         text='Запущен парсинг групп в контакте')

    def get_handler(self):
        return CommandHandler('parse', self.handle, filters=self.filter)
示例#7
0
def add_length(upd: Updater, handlers_group: int):
    logger.info("registering length handlers")
    dp = upd.dispatcher
    dp.add_handler(
        CommandHandler(
            "length",
            _length,
            filters=Filters.chat(username=get_group_chat_id().strip("@")),
            run_async=True,
        ),
        handlers_group,
    )

    dp.add_handler(
        CommandHandler(
            "longest",
            _longest,
            filters=Filters.chat(username=get_group_chat_id().strip("@")),
            run_async=True,
        ),
        handlers_group,
    )
示例#8
0
class VoteHandler(Handler):
    filter = Filters.chat(chat_id=cfg.moderation_chat)

    def handle(self, bot: Bot, update: Update, user_data, chat_data):
        data = json.loads(update.callback_query.data)
        moderator = ...
        if Moderator.select().where(Moderator.user_id == update.callback_query.
                                    from_user.id).exists():
            moderator = Moderator.select().where(
                Moderator.user_id == update.callback_query.from_user.id).peek(
                    1)
        else:
            moderator = Moderator(
                user_id=update.callback_query.from_user.id,
                username=update.callback_query.from_user.username
                or update.callback_query.from_user.first_name)
            moderator.save()
        if Vote.select().where(
                Vote.moderator == moderator,
                Vote.publication_id == data.get('publication_id'),
                Vote.points > 0).exists():
            bot.answer_callback_query(
                callback_query_id=update.callback_query.id,
                text='Вы уже голосовали',
                show_alert=False)
        else:
            vote = Vote(
                publication_id=data.get('publication_id'),
                moderator=moderator,
                date=datetime.datetime.now(),
                points=data.get('points'),
            )
            vote.save()
            bot.answer_callback_query(
                callback_query_id=update.callback_query.id,
                text='Ваш голос принят',
                show_alert=False)

    def get_handler(self):
        return CallbackQueryHandler(callback=self.handle,
                                    pass_chat_data=True,
                                    pass_groupdict=True,
                                    pass_user_data=True)
示例#9
0
class QueueLength(Handler):
    filter = Filters.chat(chat_id=cfg.moderation_chat) & Filters.command

    def handle(self, bot: Bot, update: Update):
        queue_length = publication_service.get_queue_length()
        if queue_length:
            in_hours = datetime.timedelta(
                hours=(queue_length * cfg.publication_interval / 60))
            bot.send_message(
                chat_id=update.effective_chat.id,
                reply_to_message_id=update.effective_message.message_id,
                text=
                f'Колличество постов в очереди {queue_length}\nРазмер очереди в часаx {in_hours}'
            )
        else:
            bot.send_message(
                chat_id=update.effective_chat.id,
                reply_to_message_id=update.effective_message.message_id,
                text=f'Очередь пуста, милорд :(')

    def get_handler(self):
        return CommandHandler('queue', self.handle, filters=self.filter)
示例#10
0
def add_covid_mode(upd: Updater, handlers_group: int):
    logger.info("registering covid handlers")
    dp = upd.dispatcher
    dp.add_handler(
        CommandHandler(
            "check",
            test,
            filters=Filters.chat(username=get_group_chat_id().strip("@"))
            & admin_filter,
            run_async=True,
        ),
        handlers_group,
    )

    dp.add_handler(
        CommandHandler(
            "infect",
            infect_admin,
            filters=Filters.chat(username=get_group_chat_id().strip("@"))
            & admin_filter,
            run_async=True,
        ),
        handlers_group,
    )

    dp.add_handler(CommandHandler("cough", cough, run_async=True), handlers_group)

    dp.add_handler(
        CommandHandler(
            "quarantine",
            quarantine,
            filters=Filters.chat(username=get_group_chat_id().strip("@"))
            & admin_filter,
            run_async=True,
        ),
        handlers_group,
    )

    dp.add_handler(
        CommandHandler(
            "stats",
            stats,
            filters=Filters.chat(username=get_group_chat_id().strip("@"))
            & admin_filter,
            run_async=True,
        ),
        handlers_group,
    )

    dp.add_handler(
        CommandHandler(
            "temp",
            temp,
            filters=only_admin_on_others
            & Filters.chat(username=get_group_chat_id().strip("@")),
            run_async=True,
        ),
        handlers_group,
    )

    # We must do this, since bot api doesnt present a way to get all members
    # of chat at once
    dp.add_handler(
        MessageHandler(
            Filters.chat(username=get_group_chat_id().strip("@")),
            callback=catch_message,
        ),
        handlers_group,
    )
示例#11
0
*Usage:*
  ➢ `/setreminder 30s reminder`*:* Here the time format is same as the time format in muting but with extra seconds(s)
  ➢ `/clearreminder 1234567890123456789`
"""

RemindersHandler = CommandHandler(['reminders', 'myreminders'],
                                  reminders,
                                  filters=Filters.chat_type.private,
                                  run_async=True)
SetReminderHandler = DisableAbleCommandHandler('setreminder',
                                               set_reminder,
                                               run_async=True)
ClearReminderHandler = DisableAbleCommandHandler('clearreminder',
                                                 clear_reminder,
                                                 run_async=True)
ClearAllRemindersHandler = CommandHandler('clearallreminders',
                                          clear_all_reminders,
                                          filters=Filters.chat(OWNER_ID),
                                          run_async=True)
ClearALLMyRemindersHandler = CommandHandler(
    ['clearmyreminders', 'clearallmyreminders'],
    clear_all_my_reminders,
    filters=Filters.chat_type.private,
    run_async=True)

dispatcher.add_handler(RemindersHandler)
dispatcher.add_handler(SetReminderHandler)
dispatcher.add_handler(ClearReminderHandler)
dispatcher.add_handler(ClearAllRemindersHandler)
dispatcher.add_handler(ClearALLMyRemindersHandler)
示例#12
0
 def filters(cls):
     (Filters.chat(int(os.getenv("GROUP_ID")))
      | Filters.chat(int(os.getenv("USER_ID"))))
示例#13
0
    print('2')
    buf.seek(0)
    buf.name = 'img.png'
    print('3')
    return buf


### CREATING COMMAND FUNCTIONS
def preview(bot, update):
    bot.send_message(chat_id=USER_ID, text=generate_msg_last_record())


def send(bot, update):
    bot.send_message(chat_id=CHANNEL_ID, text=generate_msg_last_record())


def preview_graph(bot, update):
    bot.send_photo(chat_id=USER_ID, photo=generate_graph())


from telegram.ext import CommandHandler
dispatcher = updater.dispatcher
user_filter = Filters.chat(USER_ID)
dispatcher.add_handler(CommandHandler('preview', preview, filters=user_filter))
dispatcher.add_handler(CommandHandler('send', send, filters=user_filter))
dispatcher.add_handler(
    CommandHandler('previewgraph', preview_graph, filters=user_filter))

updater.start_polling()
print('started')
示例#14
0
def main():
    updater = Updater(str(TOKEN), use_context=True)
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.chat(int(CHAT_ID)), switch))
    updater.start_polling()
    updater.idle()