Telegram機械人 接收跟發送訊息記錄

 https://core.telegram.org/bots/api


Telegram機械人  發送跟接收訊息
==========================
步驟一
申請帳號並設定

1.在Telegram客戶端搜尋欄輸入BotFather並打開聊天介面
2.對話 輸入 /newbot
3.輸入機器人名稱
4.輸入機器人使用者名稱
5.取得TOKEN複製起來. 呼叫API都要用到

==========================
步驟二 設定權限

1.去BotFather 對話

2.在group可以回應,要透過 /mybots -> 選擇 bot -> Bot Settings -> Group Privacy -> 按下 Turn Off

3.開啟 bot inline query 功能,要透過 /mybots -> 選擇 bot -> Bot Settings -> Inline Mode -> 按下 Turn on

4.要設定 bot 使用提示功能,要透過 /mybots -> 選擇 bot -> Bot Settings -> Inline Mode -> 按下 Edit inline placeholder,輸入使用提示即可

5.其他bot 簡介納耶也是在Bot Settings 裡面設置

*Channel跟Group都可以發言 ,好像要有管理者權限

==========================
步驟三
取得聊天室或頻道ID
要去APP上看聊天室的id

==========================
步驟四  發送訊息

直接呼叫GET 要測試可以直接在瀏覽器上打
https://api.telegram.org/bot步驟一取得的toke/sendMessage?chat_id=聊天室id&text=要發送的文字

php程式碼
define('BOT_TOKEN', 'XXXXX');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$text;
file_get_contents($sendto);



==========================
步驟五  接收訊息

1.要申請 Telegram Bot Webhook .不用審核申請就會過

2.你要有台伺服器可以接收資料

3.呼叫https://api.telegram.org/bot步驟一取得的toke/setWebhook?url=https://用來接收資料的網址

4.接收

php程式碼
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$got_message = $update["message"]["text"];


$fp = fopen('data.txt', 'a+');//opens file in write-only mode  
fwrite($fp,$content);  
fclose($fp);  

留言

此網誌的熱門文章