Exemple #1
0
def main():
    bot_token = os.getenv("BOT_TOKEN")
    admins = os.getenv("ADMINS").split(",")

    bot = Updater(bot_token)
    # automated message forwarding
    bot.dispatcher.add_handler(
        handlers.auto_forward_messages(
            contrib_filters.text,
            contrib_filters.update.message,
            ~contrib_filters.command,
            filters.contains_job_hashtag,
            filters.contains_django_mention,
        ))
    # manual admin commands
    bot.dispatcher.add_handler(
        handlers.reply_warning_to_messages(
            contrib_filters.reply,
            contrib_filters.command,
            contrib_filters.user(username=admins),
        ))
    bot.dispatcher.add_handler(
        handlers.manual_forward_messages(
            contrib_filters.reply,
            contrib_filters.command,
            contrib_filters.user(username=admins),
            filters.forwarded_message_contains_job_hashtag,
            filters.forwarded_message_contains_django_mention,
        ))
    bot.dispatcher.add_handler(
        handlers.put_in_readonly_for_message(
            contrib_filters.reply,
            contrib_filters.command,
            contrib_filters.user(username=admins),
        ))
    # error handling
    bot.dispatcher.add_error_handler(handlers.log_errors)

    if in_heroku():
        app_name = os.getenv("HEROKU_APP_NAME")
        init_sentry()
        bot.start_webhook(
            listen="0.0.0.0",
            port=int(os.getenv("PORT")),
            url_path=bot_token,
            webhook_url=f"https://{app_name}.herokuapp.com/" + bot_token,
        )
        bot.idle()
    else:
        bot.start_polling()
    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
Exemple #3
0
def sendTelegramMessage():
    updater = Updater(config.TOKEN)
    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # on different commands - answer in Telegram
    dispatcher.add_handler(
        CommandHandler("start", start, Filters.user(username="******")))
    dispatcher.add_handler(
        CommandHandler("stop", stop, Filters.user(username="******")))
    dispatcher.add_handler(
        CommandHandler("update", update, Filters.user(username="******")))

    # Start the Bot
    updater.start_polling()

    # Block until you press Ctrl-C or the process receives SIGINT, SIGTERM or
    # SIGABRT. This should be used most of the time, since start_polling() is
    # non-blocking and will stop the bot gracefully.
    updater.idle()
Exemple #4
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)
Exemple #5
0
def register_command(dispatcher: Dispatcher, command_name: str,
                     command_class: Type[Command], **kwargs) -> None:
    """Registers a new command.

    Args:
        dispatcher : telegram.ext.Dispatcher
            Telegram dispatcher.
        command_name : str
            Command name.
        command_class : type
            Class where the command is implemented.
        defaults : Dict[str, Any]
            Defaults arguments to be passed to the instances of that command.
        authorized_users : List[int]
            List of users authorized to call this command; if none provided, all
            users are authorized
    """
    from telecom.cmd_help import Help

    def factory(*args, **kwargs):
        cmd = command_class()
        cmd(*args, **kwargs)

    logging.debug("Registering command %s", command_name)

    Command.COMMANDS[command_name] = command_class
    Help.HELP_DICT[command_name] = command_class.__HELP__

    authorized_users = kwargs.get("authorized_users", [])
    command_filters = Filters.command
    if authorized_users:
        command_filters &= Filters.user(authorized_users)

    dispatcher.add_handler(
        CommandHandler(command_name,
                       functools.partial(factory,
                                         **(kwargs.get("defaults", {}))),
                       pass_args=True,
                       filters=command_filters))