我可以在机器人的电报命令之间共享一个对象吗?
Can I share an object between telegram commands of a bot?
我想在 Telegram bot 中当用户按下 /start 时创建一个对象,然后在 bot 的所有命令中共享这个对象。这可能吗?据我了解,您的服务器中只有一个机器人线程 运行。但是,我看到命令函数中有一个上下文。我可以将此对象作为一种上下文传递吗?例如:
'''
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
python-telegram-bot
已经带有 built-in mechanism for storing data。你可以这样做
try:
s = context.user_data['config']
except KeyError:
s = configuration.confi(update)
context.user_data['config'] = s
不必在每次回调中都重复此操作 - 您可以例如
- 如果需要,请使用
TypeHandler
in a low group 创建配置。那么在更高组的所有处理程序中,您无需担心
- 使用 custom implementation of
CallbackContext
添加 属性 context.user_config
免责声明:我目前是 python-telegram-bot
.
的维护者
我想在 Telegram bot 中当用户按下 /start 时创建一个对象,然后在 bot 的所有命令中共享这个对象。这可能吗?据我了解,您的服务器中只有一个机器人线程 运行。但是,我看到命令函数中有一个上下文。我可以将此对象作为一种上下文传递吗?例如:
'''
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
python-telegram-bot
已经带有 built-in mechanism for storing data。你可以这样做
try:
s = context.user_data['config']
except KeyError:
s = configuration.confi(update)
context.user_data['config'] = s
不必在每次回调中都重复此操作 - 您可以例如
- 如果需要,请使用
TypeHandler
in a low group 创建配置。那么在更高组的所有处理程序中,您无需担心 - 使用 custom implementation of
CallbackContext
添加 属性context.user_config
免责声明:我目前是 python-telegram-bot
.