Commit 21ee07f8 authored by Gampe Sebastian's avatar Gampe Sebastian
Browse files

add telegram bot ( members count )

parent 2e1d2a32
<?php
define('DATA_PATH','../data/');
define('BOT_TOKEN','<BOT_TOKEN>');
define('BOT_API','https://api.telegram.org/bot'.BOT_TOKEN.'/');
define('CHAT',Array(
Array('chat_id'=>'<CHAT_ID>','chat_name'=>'discussion','data_url'=>DATA_PATH.'stats/telegram.csv','bot_offset'=>2)
)
);
define('UPDATE_INTERVAL',3600);
?>
<?php
include_once('conf.php');
function tg_bot($param){
$url=BOT_API.$param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return json_decode( $data, true );
}
function read_data($fn){
$fp=fopen($fn,'r');
$data=fread($fp,filesize($fp));
fclose($fp);
return $data;
}
function write_data($fn,$data,$mode){
$fp=fopen($fn,$mode);
fwrite($fp,$data);
fclose($fp);
}
function get_members_count($chat_id){
return intVal( tg_bot('getChatMembersCount?chat_id='.$chat_id )['result']);
}
function get_members_counts(){
$timestamp=date('Y-m-d',time());
foreach(CHAT as $chat){
$membersCount=0;
if( filemtime($chat['data_url']) == false ){
write_data($chat['data_url'],'timestamp,'.$chat['chat_name'],'a+');
$membersCount=get_members_count($chat['chat_id']);
} elseif( filemtime($chat['data_url']) + UPDATE_INTERVAL < time() ) {
$membersCount=get_members_count($chat['chat_id']);
}
if( $membersCount > 0 ){
write_data($chat['data_url'],$timestamp.','.( $membersCount-$chat['bot_offset'] ),'a+');
}
}
}
get_members_counts();
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment