You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Telegram引用框内发送图片?Telegram Bot开发求助

Telegram Bot 实现引用块内嵌带链接二维码的方案

你需要放弃sendPhoto方法,改用sendMessage结合格式化文本+附件引用的方式,才能实现把图片放进引用块的效果。Telegram的sendPhoto是独立发送图片的接口,无法将图片嵌入到文本格式(比如引用块)中,而sendMessage支持通过附件引用的方式,在格式化文本里内嵌图片。

具体实现步骤

  • 选择解析模式:使用MarkdownV2或HTML(推荐MarkdownV2,语法更简洁)
  • 构造带引用块的消息文本:在引用块内通过attachment://前缀引用附件图片
  • 调用sendMessage时,将二维码图片作为附件传入,同时指定解析模式

代码示例(以Python的python-telegram-bot v20+为例)

from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
from telegram.constants import ParseMode

async def send_qr_in_quote(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # 构造引用块文本,引用附件里的二维码图片
    message_text = "> 请扫描下方二维码访问链接:\n> ![二维码](attachment://qr_code.png)"
    
    # 读取本地二维码图片文件
    with open("qr_code.png", "rb") as qr_file:
        # 调用sendMessage,传入附件和解析模式
        await context.bot.send_message(
            chat_id=update.effective_chat.id,
            text=message_text,
            parse_mode=ParseMode.MARKDOWN_V2,
            # 传入附件,键名要和文本里的attachment://后的名称一致
            document=qr_file,
            filename="qr_code.png"
        )

if __name__ == "__main__":
    application = ApplicationBuilder().token("你的Bot Token").build()
    application.add_handler(CommandHandler("sendqr", send_qr_in_quote))
    application.run_polling()

注意事项

  • 如果使用MarkdownV2,文本里的特殊字符(比如_、*等)需要转义,否则会解析失败
  • 附件的文件名必须和文本里attachment://后面的名称完全匹配
  • Telegram不支持给内嵌图片直接添加可点击链接,若需要引导跳转,可以在引用块里额外添加一行带链接的文本,比如:
    > 请扫描下方二维码或直接点击链接访问:
    > ![二维码](attachment://qr_code.png)
    > [访问链接](https://你的目标链接.com)
    

内容的提问来源于stack exchange,提问作者Matthew Parker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 15:06:05