python - 如何使用 python-telegram-bot 转发消息

标签 python bots telegram

我正在使用 python-telegram-api 编写一个 Telegram Bot 组管理员我希望我的机器人能够分组回复消息 like this但我能得到的唯一类似的东西是 this 。有没有办法像第一张图那样回复消息?

from telegram.ext import (Updater, CommandHandler, MessageHandler, 
Filters, ConversationHandler)
import telegram
import logging

telegram_token = "BOT TOKEN"

updater = Updater(token=telegram_token, use_context=True);
dispatcher = updater.dispatcher;
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - % 
(message)s', level=logging.INFO);

def mesgs_hand(update, context): 
    if(update.message.text == "Hi"):
        context.bot.forward_message(chat_id=update.effective_chat.id, 
        from_chat_id=update.effective_chat.id, 
        message_id=update.message.message_id, text="Hey there!"); 
        #this method forwards the message but without adding 'Hey there!' 

    elif(update.message.text == "Hello"):
        context.bot.send_message(chat_id=update.message.chat_id, text="Hello!"); 
        #this method just replies to the message without forwarding 

messages_handler = MessageHandler(Filters.text, mesgs_hand)
dispatcher.add_handler(messages_handler)


def bot():
    updater.start_polling();

if __name__ == "__main__":
    bot();

最佳答案

您可以在 send_message 函数中使用 reply_to_message_id 参数:

def mesgs_hand(update, context): 
    if(update.message.text == "Hi"):
        context.bot.send_message(
           chat_id=update.message.chat_id,
           reply_to_message_id=update.message.message_id,
           text="Hey there!")

关于python - 如何使用 python-telegram-bot 转发消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59127269/

相关文章:

python - 安装redis时缺少redis库

php - 如何使用 php 执行 shell cmd(python)并打印所有不断更新的输出?

python - 使用 Python 的旧推文 Tweepy

security - 一个只说 "Mozilla/4.0"的用户代理是一个机器人,对吧?

python - Telethon 无法通过两步验证登录帐户

python - 使用 anaconda navigator 在 Windows 上导入 TensorFlow 时出错

web-crawler - 什么是 "Bytespider"用户代理?

javascript - 如何在不和谐机器人上发出命令来随机生成报价?

python - 在 Telegram Bot python中发送长消息

cordova - 启动 Telegram API 实例