telegram 的官方說明
https://core.telegram.org/bots
1. 申請一隻機器人
只要輸入一個命令,回答兩個問題 就可申請
先將 BotFather 加為好友,再透過「對話」方式就可以申請
https://t.me/BotFather
加為好友後,會顯示相關的說明,如:
I can help you create and manage Telegram bots. If you're new to the Bot API, please see the manual.
You can control me by sending these commands:
/newbot - create a new bot
/mybots - edit your bots [beta]
Edit Bots
/setname - change a bot's name
/setdescription - change bot description
/setabouttext - change bot about info
/setuserpic - change bot profile photo
/setcommands - change the list of commands
/deletebot - delete a bot
Bot Settings
/token - generate authorization token
/revoke - revoke bot access token
/setinline - toggle inline mode
/setinlinegeo - toggle inline location requests
/setinlinefeedback - change inline feedback settings
/setjoingroups - can your bot be added to groups?
/setprivacy - toggle privacy mode in groups
Games
/mygames - edit your games [beta]
/newgame - create a new game
/listgames - get a list of your games
/editgame - edit a game
/deletegame - delete an existing game
對話框輸入 /newbot 即可申請
robot name 可以使用中文
2.發送訊息到 Telegram
https://core.telegram.org/bots/samples
這裡有官方推薦 各種語言的 API 範例
https://core.telegram.org/bots/api
Telegram Bot API 文件
我不寫對話機器人
我只是要在伺服器上發訊息到 Telegram (機器人發給特定 user)
GET 方式
最簡單的是以一行 GET 網址即可發送訊息:
https://api.telegram.org/bot1056400000:AAFAXIm8KXeBV_e7JsJLGyN3OQWpR4iCCCC/sendMessage?chat_id=743694444&text=HelloWorld主要的三個參數:
bot後面 Token
chat_id 指的是用戶編號 (你要發訊息給哪個用戶) => 查詢 id 的方式,文末說明
text 訊息內容
發送成功後,telegram 會回應:
{ "ok": true, "result": { "message_id": 4, "from": { "id": 1056400000, "is_bot": true, "first_name": "Box機器人", "username": "bbbbbb2019_bot" }, "chat": { "id": 743694444, "first_name": "KKK", "last_name": "Lee", "type": "private" }, "date": 1572361981, "text": "HelloWorld" } }
POST 方式
參考 https://core.telegram.org/bots/samples/hellobot
<?php $TOKEN = "1056400000:AAFAXIm8KXeBV_e7JsJLGyN3OQWpR4iCCCC"; $API_URL = "https://api.telegram.org/bot$TOKEN/"; #網址最後的斜線不可刪除 $parameters["method"] = 'sendMessage'; $parameters["chat_id"] = '743694444'; $parameters["text"] = 'helloWorld'; $handle = curl_init($API_URL); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($handle, CURLOPT_TIMEOUT, 60); curl_setopt($handle, CURLOPT_POST, true); curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters)); curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); $response = curl_exec($handle); echo $response;
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import ssl from urllib import request from urllib import parse from urllib.request import urlopen ssl._create_default_https_context = ssl._create_unverified_context values = {"method": "sendMessage", "chat_id":"743694444", "text":"helloWorld"} data = parse.urlencode(values).encode('utf-8') url = 'https://api.telegram.org/bot1056400000:AAFAXIm8KXeBV_e7JsJLGyN3OQWpR4iCCCC/' request = request.Request(url, data) response = urlopen(request) print(response.read().decode())
查詢自己 chat_id(用戶編號) 的方式
搜尋這個機器人 @chat_id_echo_bot 或是 @getmyid_bot
再輸入 /start 就會顯示 id
---
官方提供的各種語言範例:
https://core.telegram.org/bots/samples
設定機器人的圖示icon
1.準備 至少 150x150 的 jpg/png
2.
於 @BotFather 對話中輸入
/setuserpic
即可上傳圖片、設定
設定機器人的功能表 (menu)
1.
於 @BotFather 對話中輸入
/setcommands
2.
輸入 menu 資料,例如:
hello - 吃飯吃飯
book - 書書書
cat - 貓貓
更新 telegram 訊息中的縮圖暫存、文字預覽
當文章內的標題、圖片、文字有變動時,希望 telegram 更新暫存(preview) 的網址內容1.加入這個機器人
@webpagebot
2.對話框貼上網址即可參考:
https://stackoverflow.com/questions/34707915/how-do-you-clear-the-open-graph-cache-of-an-url-on-telegram
Telegram 中文化: 不需設定、點一下 TG 畫面全中文