天天看點

telegram bot sendMessage 發送消息

本文内容來自以下官方文檔

Bots: An introduction for developers

Telegram Bot API

Creating a new bot

Telegram 添加 BotFather 進入聊天界面

點選輸入框中 /start

回複内容中點選 /newbot

閱讀提示分别輸入 name 和 username

緊接着回複内容中包含了接下來需要使用的 token,看起來像這樣:123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

以上步驟可同步參考官方文檔:Creating a new bot

sendMessage

curl 'https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getMe'
           

以上指令請參考官方文檔:getMe

curl -d 'chat_id=700049303&text=hello' 'https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage'
           

以上指令請參考官方文檔:sendMessage

此時你可能會疑惑,你的 chat_id 哪裡來的?與 Bot 發起私聊,在群聊裡添加 Bot,之後均可以通過 getUpdates 接口請求到内容,message.chat.id 即是我們想要的 chat_id 啦,私聊為正,群聊為負

curl 'https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getUpdates'
           

以上指令請參考官方文檔:getUpdates

總結

具備以上知識,監控程式中內建 通知到Telegram 應該不成問題

官方文檔:Telegram Bot API 中有幾節比較短,推薦看一下啊

Authorizing your bot

Making requests

Getting updates