【发布时间】:2020-05-28 06:24:25
【问题描述】:
我正在尝试使用电报中的机器人 API,并为此使用包装器 python-telegram-bot。目前,我的机器人在直接启动与机器人的对话时能够按预期运行。但是,当在群组中使用机器人或只是在与其他人的正常聊天中提及机器人时,机器人似乎根本看不到消息。
使用电报botFather,我启用了join to groups,我尝试启用和禁用inline mode(对此问题没有影响)。
我的代码是这样的:(代码改编自基本wiki示例)
# Imports
from telegram import InlineQueryResultArticle, ParseMode, \
InputTextMessageContent
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler, Handler, ConversationHandler, CallbackQueryHandler
from telegram.utils.helpers import escape_markdown
from telegram import MessageEntity, ChatAction
# functions
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Let\'s start')
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def process(update, context):
print(f'got this ==> {update.message.text}')
query = update.message.text
output = "Some output here"
update.message.reply_text(output)
def run_bot():
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater("TOKEN_HERE", use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message
dp.add_handler(MessageHandler(Filters.text & (~Filters.command), process))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Block until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
然后我就运行run_bot()。
【问题讨论】:
标签: python bots telegram python-telegram-bot