sendWidget
发送带有 HTML 小组件的消息 (Bot API 10.4)。
返回值: 调用成功时返回 Message。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
chat_id | Integer or String | 是 | 聊天 ID 或 @username |
message_thread_id | Integer | 可选 | 论坛话题 ID |
html | String | 是 | 小组件的 HTML 代码:1-32768 字节,zlib 压缩后最多 4096 字节 |
width | Integer | 可选 | 小组件宽度(逻辑像素;省略则占满消息宽度) |
height | Integer | 可选 | 小组件高度(逻辑像素,50-600;省略则自适应内容) |
public_data | String | 可选 | 小组件的公共状态(public_data),0-1024 字节——在显示该小组件的所有位置均可见 |
chat_data | String | 可选 | 小组件在此聊天中的状态(chat_data),0-1024 字节 |
caption | String | 可选 | 说明文字(0-1024) |
parse_mode | String | 可选 | HTML、Markdown 或 MarkdownV2 |
caption_entities | Array of MessageEntity | 可选 | 说明文字的 MessageEntity JSON 数组 |
show_caption_above_media | Boolean | 可选 | 在小组件上方显示说明文字 |
allow_queries | Boolean | 可选 | 允许小组件向机器人发送查询(widget_query) |
is_shared | Boolean | 可选 | 创建共享小组件——可通过 sendSharedWidget 再次发送 |
disable_notification | Boolean | 可选 | 静默发送 |
protect_content | Boolean | 可选 | 保护内容 |
allow_paid_broadcast | Boolean | 可选 | 允许付费广播 |
message_effect_id | String | 可选 | 消息特效 ID |
reply_parameters | ReplyParameters | 可选 | ReplyParameters JSON 对象 |
reply_markup | InlineKeyboardMarkup 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
相关方法
sendSharedWidget再次发送机器人创建的共享小组件 (Bot API 10.4)。answerWidgetQuery回应来自小组件的查询 (Bot API 10.4)。editMessageWidgetData编辑消息中小组件的数据 (Bot API 10.4)。editSharedWidgetData编辑共享小组件的公共数据 (Bot API 10.4)。