处理电报机器人时出错
Error handling Telegram bot
我正在努力避免电报错误。未修改消息时出现此错误:
telegram.error.BadRequest: Message is not modified
我想制作一个函数来在发生此错误时打印一条消息,而不是打印原始错误消息电报。我尝试过类似的方法但不起作用:
def error_callback(bot, update, error):
try:
raise error
except BadRequest:
# handle malformed requests - read more below!
print('Same message')
首先,如果没有明显的错误,如果用户点击按钮太快,可能会发生此错误。在这种情况下,它很容易被忽略。
假设您正在使用 python-telegram-bot library 查看您的代码,您可以遵循 2 种方法:
1.全局忽略错误:
def error(bot, update, error):
if not (error.message == "Message is not modified"):
logger.warning('Update "%s" caused error "%s"' % (update, error))
但您仍会在控制台上收到:
2017-11-03 17:16:41,405 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update
你唯一能做的就是禁用该字符串任何类型的任何错误这样做:
updater.dispatcher.logger.addFilter((lambda s: not s.msg.endswith('A TelegramError was raised while processing the Update')))
在你的主要()。 credits
2。忽略您正在调用的方法中的错误:
您可以忽略您正在调用的方法中的错误:
try:
# the method causing the error
except TelegramError as e:
if str(e) != "Message is not modified": print(e)
第二种方法将在控制台上完全忽略错误而不修改错误回调函数,但您必须在导致该异常的每个方法中使用它。
打印 'text' 而不是忽略:
我建议你忽略这个错误,但是如果你想像你说的那样打印一个字符串:你可以很容易地修改这两种打印字符串的方法。
第一种方法的示例:
def error(bot, update, error):
if error.message == "Message is not modified":
# print your string
return
logger.warning('Update "%s" caused error "%s"' % (update, error))
第二种方法的例子:
try:
# the method causing the error
except TelegramError as e:
if str(e) == "Message is not modified": print(your_string)
检查文档:
请注意,目前只能在没有 reply_markup 或使用嵌入式键盘的情况下编辑消息。
https://core.telegram.org/bots/api#updating-messages
您似乎尝试使用 reply_markup 编辑消息,但键盘不是 inline.
无论如何,如果问题仍然存在 - 请显示来源。
我正在努力避免电报错误。未修改消息时出现此错误:
telegram.error.BadRequest: Message is not modified
我想制作一个函数来在发生此错误时打印一条消息,而不是打印原始错误消息电报。我尝试过类似的方法但不起作用:
def error_callback(bot, update, error):
try:
raise error
except BadRequest:
# handle malformed requests - read more below!
print('Same message')
首先,如果没有明显的错误,如果用户点击按钮太快,可能会发生此错误。在这种情况下,它很容易被忽略。
假设您正在使用 python-telegram-bot library 查看您的代码,您可以遵循 2 种方法:
1.全局忽略错误:
def error(bot, update, error):
if not (error.message == "Message is not modified"):
logger.warning('Update "%s" caused error "%s"' % (update, error))
但您仍会在控制台上收到:
2017-11-03 17:16:41,405 - telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update
你唯一能做的就是禁用该字符串任何类型的任何错误这样做:
updater.dispatcher.logger.addFilter((lambda s: not s.msg.endswith('A TelegramError was raised while processing the Update')))
在你的主要()。 credits
2。忽略您正在调用的方法中的错误:
您可以忽略您正在调用的方法中的错误:
try:
# the method causing the error
except TelegramError as e:
if str(e) != "Message is not modified": print(e)
第二种方法将在控制台上完全忽略错误而不修改错误回调函数,但您必须在导致该异常的每个方法中使用它。
打印 'text' 而不是忽略:
我建议你忽略这个错误,但是如果你想像你说的那样打印一个字符串:你可以很容易地修改这两种打印字符串的方法。
第一种方法的示例:
def error(bot, update, error):
if error.message == "Message is not modified":
# print your string
return
logger.warning('Update "%s" caused error "%s"' % (update, error))
第二种方法的例子:
try:
# the method causing the error
except TelegramError as e:
if str(e) == "Message is not modified": print(your_string)
检查文档: 请注意,目前只能在没有 reply_markup 或使用嵌入式键盘的情况下编辑消息。 https://core.telegram.org/bots/api#updating-messages
您似乎尝试使用 reply_markup 编辑消息,但键盘不是 inline.
无论如何,如果问题仍然存在 - 请显示来源。