From 56cbc2ff937cc15971feff3e06a7fe9d03544d2c Mon Sep 17 00:00:00 2001 From: bedilbek Date: Sat, 11 Apr 2020 13:03:52 +0500 Subject: [PATCH] Add examples to better understand middleware handler --- examples/middleware/i18n.py | 53 +++++++++++++++++++++++++++++ examples/middleware/session.py | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 examples/middleware/i18n.py create mode 100644 examples/middleware/session.py diff --git a/examples/middleware/i18n.py b/examples/middleware/i18n.py new file mode 100644 index 0000000..3cea875 --- /dev/null +++ b/examples/middleware/i18n.py @@ -0,0 +1,53 @@ +#!/usr/bin/python + +# This example shows how to implement i18n (internationalization) l10n (localization) to create +# multi-language bots with middleware handler. +# +# Note: For the sake of simplicity of this example no extra library is used. However, it is recommended to use +# better i18n systems (gettext and etc) for handling multilingual translations. +# This is not a working, production-ready sample and it is highly recommended not to use it in production. +# +# In this example let's imagine we want to introduce localization or internationalization into our project and +# we need some global function to activate the language once and to use that language in all other message +# handler functions for not repeatedly activating it. +# The middleware (i18n and l10n) is explained: + +import telebot +from telebot import apihelper + +apihelper.ENABLE_MIDDLEWARE = True + +TRANSLATIONS = { + 'hello': { + 'en': 'hello', + 'ru': 'привет', + 'uz': 'salom' + } +} + +_lang = 'en' + + +def activate(lang): + global _lang + _lang = lang + + +def _(string): + return TRANSLATIONS[string][_lang] + + +bot = telebot.TeleBot('TOKEN') + + +@bot.middleware_handler(update_types=['message']) +def activate_language(bot_instance, message): + activate(message.from_user.language_code) + + +@bot.message_handler(commands=['start']) +def start(message): + bot.send_message(message.chat.id, _('hello')) + + +bot.polling() diff --git a/examples/middleware/session.py b/examples/middleware/session.py new file mode 100644 index 0000000..a1a30e5 --- /dev/null +++ b/examples/middleware/session.py @@ -0,0 +1,61 @@ +#!/usr/bin/python + +# This example shows how to implement session creation and retrieval based on user id with middleware handler. +# +# Note: For the sake of simplicity of this example no extra library is used. However, it is recommended to use +# in-memory or on-disk storage implementations (redis, mysql, postgres and etc) for storing and retrieving structures. +# This is not a working, production-ready sample and it is highly recommended not to use it in production. +# +# In this example let's imagine we want to create a session for each user who communicates with the bot to store +# different kind of temporary data while session is active. As an example we want to track the state of the user +# with the help of this session. So, we need a way to store this session data somewhere globally to enable other +# message handler functions to be able to use it. +# The middleware session is explained: + +import telebot +from telebot import apihelper + +apihelper.ENABLE_MIDDLEWARE = True + +INFO_STATE = 'ON_INFO_MENU' +MAIN_STATE = 'ON_MAIN_MENU' + +SESSIONS = { + -10000: { + 'state': INFO_STATE + }, + -11111: { + 'state': MAIN_STATE + } +} + + +def get_or_create_session(user_id): + try: + return SESSIONS[user_id] + except KeyError: + SESSIONS[user_id] = {'state': MAIN_STATE} + return SESSIONS[user_id] + + +bot = telebot.TeleBot('TOKEN') + + +@bot.middleware_handler(update_types=['message']) +def set_session(bot_instance, message): + bot_instance.session = get_or_create_session(message.from_user.id) + + +@bot.message_handler(commands=['start']) +def start(message): + bot.session['state'] = MAIN_STATE + bot.send_message(message.chat.id, bot.session['state']) + + +@bot.message_handler(commands=['info']) +def start(message): + bot.session['state'] = INFO_STATE + bot.send_message(message.chat.id, bot.session['state']) + + +bot.polling()