grammY
grammY 整合 (Telegram Bot API)
Section titled “grammY 整合 (Telegram Bot API)”為何選擇 grammY
Section titled “為何選擇 grammY”- 以 TypeScript 為優先的 Bot API 客戶端,內建長輪詢 + Webhook 輔助程式、中介軟體、錯誤處理、速率限制器。
- 比手動處理 fetch + FormData 更簡潔的媒體輔助程式;支援所有 Bot API 方法。
- 可擴充:透過自訂 fetch 支援 Proxy、會話中介軟體 (選用)、型別安全的 context。
我們發布的內容
Section titled “我們發布的內容”- 單一客戶端路徑: 已移除基於 fetch 的實作;grammY 現在是唯一的 Telegram 客戶端 (發送 + 閘道),並預設啟用 grammY 節流器。
- 閘道:
monitorTelegramProvider建構 grammYBot,連接提及/允許清單閘門、透過getFile/download下載媒體,並使用sendMessage/sendPhoto/sendVideo/sendAudio/sendDocument傳送回覆。透過webhookCallback支援長輪詢或 Webhook。 - Proxy: 選用的
channels.telegram.proxy透過 grammY 的client.baseFetch使用undici.ProxyAgent。 - Webhook 支援:
webhook-set.ts封裝setWebhook/deleteWebhook;webhook.ts託管具有健康檢查與優雅關機功能的回調。當設定channels.telegram.webhookUrl+channels.telegram.webhookSecret時,閘道會啟用 Webhook 模式 (否則會進行長輪詢)。 - 會話: 私人聊天會合併至 Agent 主會話 (
agent:<agentId>:<mainKey>);群組使用agent:<agentId>:telegram:group:<chatId>;回覆會路由回同一個頻道。 - 設定選項:
channels.telegram.botToken、channels.telegram.dmPolicy、channels.telegram.groups(允許清單 + 提及的預設值)、channels.telegram.allowFrom、channels.telegram.groupAllowFrom、channels.telegram.groupPolicy、channels.telegram.mediaMaxMb、channels.telegram.linkPreview、channels.telegram.proxy、channels.telegram.webhookSecret、channels.telegram.webhookUrl。 - 草稿串流: 選用的
channels.telegram.streamMode在私人主題聊天中使用sendMessageDraft(Bot API 9.3+)。這與頻道區塊串流是分開的。 - 測試: grammy mocks 涵蓋了 DM + 群組提及閘門和 outbound send;仍歡迎更多 media/webhook fixtures。
待解決的問題
- 若遇到 Bot API 429s,可選用 grammY 外掛程式(throttler)。
- 新增更多結構化的媒體測試(貼圖、語音訊息)。
- 讓 webhook 監聽連接埠可設定(目前固定為 8787,除非透過 gateway 連接)。