一些些的code、一點點的Life,這就是生活。
這只是一個簡單的筆記而已,要更強大功能的bot其實github很多可以找
網路有很多telegram bot 傳送訊息的範例,但是有的很肥大,雖然功能很多。 如果你只是想要基本的傳送訊息的功能而已的話,可以看下面範例,程式碼不多,簡單易懂。
送訊息
<?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); } ?>
一個不務正業,甚麼科技產品都玩的網路宅宅。 If you want to find me,that's easy! I will always on the Internet. so just find me, follow me. I will show you everything fun.