Skip to content

Instantly share code, notes, and snippets.

@chuv1
Created May 2, 2017 07:01
Show Gist options
  • Save chuv1/bdf58cf8b55d4844d6683cccc37dbd29 to your computer and use it in GitHub Desktop.
Save chuv1/bdf58cf8b55d4844d6683cccc37dbd29 to your computer and use it in GitHub Desktop.
Watchdog. Will notify you if your bot is down.
<?php
$message = '';
$botoneHook = file_get_contents('https://api.telegram.org/bot___FIRST_BOT_API_KEY___/getWebhookInfo');
$bottwoHook = file_get_contents('https://api.telegram.org/bot___SECOND_BOT_API_KEY___/getWebhookInfo');
$botOne = !empty($botoneHook) && !empty(json_decode($botoneHook, true)) && !empty(json_decode($botoneHook, true)['result']['pending_update_count']) && json_decode($botoneHook, true)['result']['pending_update_count'] > 5?
json_decode($botoneHook, true)['result']:'ok';
$botTwo = !empty($bottwoHook) && !empty(json_decode($bottwoHook, true)) && !empty(json_decode($bottwoHook, true)['result']['pending_update_count']) && json_decode($bottwoHook, true)['result']['pending_update_count'] > 5?
json_decode($bottwoHook, true)['result']:'ok';
if($botTwo == 'ok' && $botOne == 'ok') exit();
if($botOne != 'ok' && !empty($botOne) && is_array($botOne)){
$botOne = sprintf('🆘 BotOneBot errors count - %s, last one occurred %s - (%s)',
$botOne['pending_update_count'],
date("H:i:s", $botOne['last_error_date']),
$botOne['last_error_message']
);
}
if($botTwo != 'ok' && !empty($botTwo) && is_array($botTwo)){
$botTwo = sprintf('🆘 BotTwoBot errors count - %s, last one occurred %s - (%s)',
$botTwo['pending_update_count'],
date("H:i:s", $botTwo['last_error_date']),
$botTwo['last_error_message']
);
}
$message .= trim(($botTwo != 'ok'?$botTwo:'').PHP_EOL.($botOne != 'ok'?$botOne:''));
$fields_string = '';
$url = 'https://api.telegram.org/bot___SENDER_BOT_API_KEY___/sendMessage'; // Sender bot api key
$fields = array(
'chat_id' => urlencode('___YOUR_TG_ID___'),
'text' => urlencode(''.$message)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment