【问题标题】:Telegram bot replying in private but when used in a normal chat电报机器人私下回复,但在正常聊天中使用时
【发布时间】: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


    【解决方案1】:

    与 BotFather 交谈并禁用您的机器人的隐私模式

    为什么?嗯,official page 可以回答:

    在隐私模式下运行的机器人不会收到人们发送给群组的所有消息。相反,它只会收到:

    • 以斜杠“/”开头的消息(参见上面的命令)
    • 回复机器人自己的消息
    • 服务消息(在群组中添加或删除的人等)
    • 来自其会员频道的消息

    另外我建议你查看FAQ of this topic

    请记住过滤您要回复的消息,否则您可能最终会向群组发送垃圾邮件。

    【讨论】:

    • 其实我不希望机器人收听所有消息并过滤消息,我希望机器人只在提到时才回答,这应该是可行的,无需更改隐私设置。但是,这里不会发生这种情况!
    • 您能举例说明您希望收到什么吗?看看它是否符合常见问题解答第 3 点所说的内容。
    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2017-05-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2019-11-23
    相关资源
    最近更新 更多