コード例 #1
0
ファイル: main.py プロジェクト: eternnoir/pyTelegramBotAPI
        username = '******'
    bot.send_message(message.chat.id, username)


# You can make it case-insensitive
@bot.message_handler(text=TextFilter(equals=l_("My first name"),
                                     ignore_case=True))
def return_user_id(message: types.Message):
    bot.send_message(message.chat.id, message.from_user.first_name)


all_menu_texts = []
for language in i18n.available_translations:
    for menu_text in ("My user id", "My user name", "My first name"):
        all_menu_texts.append(_(menu_text, language))


# When user confused language. (handles all menu buttons texts)
@bot.message_handler(text=TextFilter(contains=all_menu_texts,
                                     ignore_case=True))
def missed_message(message: types.Message):
    bot.send_message(message.chat.id,
                     _("Seems you confused language"),
                     reply_markup=keyboards.menu_keyboard(_))


if __name__ == '__main__':
    bot.setup_middleware(i18n)
    bot.add_custom_filter(TextMatchFilter())
    asyncio.run(bot.infinity_polling())
コード例 #2
0

class Middleware(BaseMiddleware):
    def __init__(self):
        self.update_types = ['message']

    def pre_process(self, message, data):
        data['foo'] = 'Hello'  # just for example
        # we edited the data. now, this data is passed to handler.
        # return SkipHandler() -> this will skip handler
        # return CancelUpdate() -> this will cancel update
    def post_process(self, message, data, exception=None):
        print(data['foo'])
        if exception:  # check for exception
            print(exception)


@bot.message_handler(commands=['start'])
def start(
    message, data: dict
):  # you don't have to put data parameter in handler if you don't need it.
    bot.send_message(message.chat.id, data['foo'])
    data[
        'foo'] = 'Processed'  # we changed value of data.. this data is now passed to post_process.


# Setup middleware
bot.setup_middleware(Middleware())

bot.infinity_polling()
コード例 #3
0
        self.last_time = {}
        self.limit = limit
        self.update_types = ['message']
        # Always specify update types, otherwise middlewares won't work

    def pre_process(self, message, data):
        if not message.from_user.id in self.last_time:
            # User is not in a dict, so lets add and cancel this function
            self.last_time[message.from_user.id] = message.date
            return
        if message.date - self.last_time[message.from_user.id] < self.limit:
            # User is flooding
            bot.send_message(message.chat.id,
                             'You are making request too often')
            return CancelUpdate()
        self.last_time[message.from_user.id] = message.date

    def post_process(self, message, data, exception):
        pass


bot.setup_middleware(SimpleMiddleware(2))


@bot.message_handler(commands=['start'])
def start(message):  # you don't have to put data in handler.
    bot.send_message(message.chat.id, 'Hello!')


bot.infinity_polling()