跳至主要內容

呼叫 Bot API:請求驗證、ID 規則與限流

MPChat Bot 請求如何鑑權與構造:基礎 URL、token 安全五條規則、HTTP 方法、Content-Type、ID 型別規則,以及每秒 30 次限流。

使用 Authorization: Bearer <token> 標頭鑑權,token 只儲存在伺服端。


總覽

Bot API 位址為 https://call.mp.net/bot/{methodName}。例如,呼叫 getMe 驗證 Bot 身分。

curl --silent --show-error "https://call.mp.net/bot/getMe" \
-H "Authorization: Bearer ${MPCHAT_BOT_TOKEN}"

既有串接仍可用 https://call.mp.net/bot/bot{MPCHAT_BOT_TOKEN}/{methodName} 鑑權。兩處同時傳 token 時,API 以 Bearer token 為準。

Token 安全規則

  • 不進前端——WebView、瀏覽器、行動端程式都不得包含。

  • 應用程式日誌、代理伺服器存取日誌、追蹤和錯誤回報都要遮蔽憑證,涵蓋 Authorization 標頭及含 token 的舊式 URL。

  • 不進版本庫——.env 加入 .gitignore;透過 CI 密鑰注入。

  • 不進聊天——工單、IM、AI 對話禁止貼上真實 token。

  • 洩露即輪換——立即在 BotFather revoke 並更換。

HTTP 方法與 Content-Type

  • 多數寫操作為 POST。getMe、getUpdates、getWebhookInfo、getMyCommands 同時支援 GET。

  • application/json——絕大多數寫方法。

  • multipart/form-data——媒體本地上傳(sendPhoto、sendVideo、sendDocument、setChatPhoto)。

ID 型別規則

chat_id、user_id、message_id 接受 JavaScript 安全整數(0–9007199254740991)或同範圍十進位字串。建議用 BIGINT 或 VARCHAR 儲存,注意 JSON 精度。

限流

預設每 token 每秒 30 次;超限回傳 429。生產程式必須實作指數退避(見生產檢查清單)。

請求前檢查清單

  • token 從環境變數讀取。

  • ID 型別符合文件約定。

  • POST body 為合法 JSON。

  • 媒體方法使用公網 URL 或 multipart(不複用 file_id)。

相關文章

測試與生產 Bot 使用不同 token。token 洩露應優先 revoke。

是否回答了您的問題?