Telegram Bot API

sendWidget

分类: 小组件 · Bot API 10.5 · 官方文档

版本历史: Bot API 10.4 新增

发送带有 HTML 小组件的消息 (Bot API 10.4)。

返回值: 调用成功时返回 Message。

参数

参数类型必填说明
chat_idInteger or String是聊天 ID 或 @username
message_thread_idInteger可选论坛话题 ID
htmlString是小组件的 HTML 代码:1-32768 字节,zlib 压缩后最多 4096 字节
widthInteger可选小组件宽度(逻辑像素;省略则占满消息宽度)
heightInteger可选小组件高度(逻辑像素,50-600;省略则自适应内容)
public_dataString可选小组件的公共状态(public_data),0-1024 字节——在显示该小组件的所有位置均可见
chat_dataString可选小组件在此聊天中的状态(chat_data),0-1024 字节
captionString可选说明文字(0-1024)
parse_modeString可选HTML、Markdown 或 MarkdownV2
caption_entitiesArray of MessageEntity可选说明文字的 MessageEntity JSON 数组
show_caption_above_mediaBoolean可选在小组件上方显示说明文字
allow_queriesBoolean可选允许小组件向机器人发送查询(widget_query)
is_sharedBoolean可选创建共享小组件——可通过 sendSharedWidget 再次发送
disable_notificationBoolean可选静默发送
protect_contentBoolean可选保护内容
allow_paid_broadcastBoolean可选允许付费广播
message_effect_idString可选消息特效 ID
reply_parametersReplyParameters可选ReplyParameters JSON 对象
reply_markupInlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply可选JSON 对象:InlineKeyboardMarkup / ReplyKeyboardMarkup / ReplyKeyboardRemove / ForceReply

参数说明为简要概括,完整内容请参阅官方文档。

请求示例

curl

curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendWidget" \
  -H "Content-Type: application/json" \
  -d '{
  "chat_id": 123456789,
  "html": "<h3>Order #1042</h3><p>2 × Margherita</p><button>Track the order</button>"
}'

Python

import requests

TOKEN = "123456789:AAEexampleTokenReplaceWithYours"
resp = requests.post(
    f"https://api.telegram.org/bot{TOKEN}/sendWidget",
    json={
        "chat_id": 123456789,
        "html": "<h3>Order #1042</h3><p>2 × Margherita</p><button>Track the order</button>"
    },
    timeout=30,
)
print(resp.json())

JavaScript

const TOKEN = "123456789:AAEexampleTokenReplaceWithYours";
const res = await fetch(`https://api.telegram.org/bot${TOKEN}/sendWidget`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    "chat_id": 123456789,
    "html": "<h3>Order #1042</h3><p>2 × Margherita</p><button>Track the order</button>"
  }),
});
console.log(await res.json());

分步教程: Telegram 小组件(Widget)入门:在机器人消息中运行交互式 HTML