閒聊

telegram 收發訊息

這只是一個簡單的筆記而已,要更強大功能的bot其實github很多可以找

送訊息

<?php

define('BOT_TOKEN', '128981753:AAFvb-IY-iw-FvYrFyFVdsKVXXvmk_GaVPk');

$chat_id = 你要聊天對象的id;
$text="安安";
$url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/sendMessage?text='.$text.'&chat_id='.$chat_id;
$result = file_get_contents($url);
$result = json_decode($result, true);

var_dump($result['result']);
echo "done";
?>

你要聊天對象的id是對方的uid喔 不是username 像我自己是153852109

收訊息

<?php

define('BOT_TOKEN', '128981753:AAFvb-IY-iw-FvYrFyFVdsKVXXvmk_GaVPk');
/*
# Note: you want to change the offset based on the last update_id you received
$url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/getUpdates?offset=0';
$result = file_get_contents($url);
$result = json_decode($result, true);

foreach ($result['result'] as $message) {
    var_dump($message);
}
?>
Be the First to comment.

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。

(若看不到驗證碼,請重新整理網頁。)