Telegram Bot API

sendWidget

Danh mục: Widget · Bot API 10.5 · Tài liệu chính thức

Lịch sử phiên bản: Được thêm trong Bot API 10.4

Gửi tin nhắn có widget HTML (Bot API 10.4).

Giá trị trả về: Nếu thành công, trả về Message.

Tham số

Tham sốKiểuBắt buộcMô tả
chat_idInteger or StringCóID chat hoặc @username
message_thread_idIntegerTùy chọnID chủ đề diễn đàn
htmlStringCóMã HTML của widget: 1-32768 byte, tối đa 4096 byte sau khi nén zlib
widthIntegerTùy chọnChiều rộng widget tính bằng pixel logic (bỏ qua để rộng bằng tin nhắn)
heightIntegerTùy chọnChiều cao widget tính bằng pixel logic, 50-600 (bỏ qua để vừa nội dung)
public_dataStringTùy chọnTrạng thái công khai của widget (public_data), 0-1024 byte — hiển thị ở mọi nơi widget xuất hiện
chat_dataStringTùy chọnTrạng thái của widget trong chat này (chat_data), 0-1024 byte
captionStringTùy chọnChú thích (0-1024)
parse_modeStringTùy chọnHTML, Markdown, MarkdownV2
caption_entitiesArray of MessageEntityTùy chọnMảng JSON MessageEntity cho chú thích
show_caption_above_mediaBooleanTùy chọnHiển thị chú thích phía trên widget
allow_queriesBooleanTùy chọnCho phép widget gửi truy vấn đến bot (widget_query)
is_sharedBooleanTùy chọnTạo widget dùng chung — có thể gửi lại bằng sendSharedWidget
disable_notificationBooleanTùy chọnGửi không âm thanh
protect_contentBooleanTùy chọnBảo vệ nội dung
allow_paid_broadcastBooleanTùy chọnCho phép gửi hàng loạt trả phí
message_effect_idStringTùy chọnID hiệu ứng tin nhắn
reply_parametersReplyParametersTùy chọnĐối tượng JSON ReplyParameters
reply_markupInlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReplyTùy chọnJSON: InlineKeyboardMarkup / ReplyKeyboardMarkup / ReplyKeyboardRemove / ForceReply

Mô tả tham số chỉ là bản tóm tắt ngắn gọn; xem tài liệu chính thức để đọc toàn văn.

Ví dụ request

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());

Hướng dẫn từng bước: Widget Telegram: HTML tương tác ngay trong tin nhắn của bot