'Can I share an object between telegram commands of a bot?

I want to create an object when the user press /start in a Telegram bot, and then share this object among all the commands of the bot. Is this possible? As far as I understand, there's only one thread of your bot running in your server. However, I see that there is a context in the command functions. Can I pass this object as a kind of context? For example:

'''
This is a class object that I created to store data from the user and configure the texts I'll display depending on
the user language but maybe I fill it also with info about something it will buy in the bot
'''
import configuration 

from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext

# Commands of the bot
def start(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    s = configuration.conf(update) #Create the object I'm saying
    update.message.reply_markdown_v2(s.text[s.lang_active],
        reply_markup=ForceReply(selective=True),
    )

def check(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    s = configuration.conf(update)   # I want to avoid this!
    update.message.reply_markdown_v2(s.text[s.lang_active],
        reply_markup=ForceReply(selective=True),
    )

... REST OF THE BOT


Solution 1:[1]

python-telegram-bot already comes with a built-in mechanism for storing data. You can do something like

try:
    s = context.user_data['config']
except KeyError:
    s = configuration.confi(update)
    context.user_data['config'] = s

This doesn't have to be repeated in every callback - you can e.g.


Disclaimer: I'm currently the maintainer of python-telegram-bot.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CallMeStag