sendMessage
Gửi tin nhắn văn bản.
Giá trị trả về: Nếu thành công, trả về Message.
Tham số
| Tham số | Kiểu | Bắt buộc | Mô tả |
|---|---|---|---|
business_connection_id | String | Tùy chọn | ID kết nối doanh nghiệp |
chat_id | Integer or String | Có | ID chat hoặc @username |
message_thread_id | Integer | Tùy chọn | ID chủ đề diễn đàn |
direct_messages_topic_id | Integer | Tùy chọn | ID chủ đề tin nhắn trực tiếp (cho kênh, Bot API 10.0) |
ephemeral_message_parameters | EphemeralMessageParameters | Tùy chọn | Đối tượng JSON EphemeralMessageParameters — tin nhắn tạm thời chỉ hiển thị với một người dùng (Bot API 10.3) |
text | String | Có | Nội dung tin nhắn (1-4096 ký tự) |
parse_mode | String | Tùy chọn | HTML, Markdown, MarkdownV2 |
entities | Array of MessageEntity | Tùy chọn | Mảng JSON MessageEntity |
link_preview_options | LinkPreviewOptions | Tùy chọn | Đối tượng JSON LinkPreviewOptions |
disable_notification | Boolean | Tùy chọn | Gửi không âm thanh |
protect_content | Boolean | Tùy chọn | Bảo vệ nội dung |
allow_paid_broadcast | Boolean | Tùy chọn | Cho phép gửi hàng loạt trả phí (0.1 Stars/tin nhắn) |
message_effect_id | String | Tùy chọn | ID hiệu ứng tin nhắn |
suggested_post_parameters | SuggestedPostParameters | Tùy chọn | Đối tượng JSON SuggestedPostParameters (cho kênh) |
reply_parameters | ReplyParameters | Tùy chọn | Đối tượng JSON ReplyParameters |
reply_markup | InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply | Tùy chọn | JSON: InlineKeyboardMarkup / ReplyKeyboardMarkup / ReplyKeyboardRemove / ForceReply |
Ví dụ request
curl
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" \
-H "Content-Type: application/json" \
-d '{"chat_id":123456789,"text":"Hello from the Bot API!"}'
Python
import requests
TOKEN = "123456789:AAEexampleTokenReplaceWithYours"
resp = requests.post(
f"https://api.telegram.org/bot{TOKEN}/sendMessage",
json={
"chat_id": 123456789,
"text": "Hello from the Bot API!"
},
timeout=30,
)
print(resp.json())
JavaScript
const TOKEN = "123456789:AAEexampleTokenReplaceWithYours";
const res = await fetch(`https://api.telegram.org/bot${TOKEN}/sendMessage`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"chat_id": 123456789,
"text": "Hello from the Bot API!"
}),
});
console.log(await res.json());
Hướng dẫn từng bước: API gửi tin nhắn Telegram: hướng dẫn sendMessage với curl, Python và JavaScript
Phương thức liên quan
forwardMessageChuyển tiếp một tin nhắn.forwardMessagesChuyển tiếp nhiều tin nhắn.copyMessageSao chép tin nhắn mà không kèm liên kết chuyển tiếp.copyMessagesSao chép nhiều tin nhắn.deleteMessageXóa một tin nhắn.deleteMessagesXóa nhiều tin nhắn.sendChatActionGửi hành động trong chat (đang nhập, v.v.).sendMessageDraftGửi bản nháp tin nhắn - bản xem trước tạm thời (Bot API 9.5+).sendContactGửi thông tin liên hệ.sendLocationGửi vị trí.sendVenueGửi địa điểm.setMessageReactionĐặt cảm xúc cho tin nhắn.