Skip to content

Instantly share code, notes, and snippets.

@mrcat323
Created June 10, 2017 06:50
Show Gist options
  • Save mrcat323/49392455cb8a622d185ac10c49e42f5a to your computer and use it in GitHub Desktop.
Save mrcat323/49392455cb8a622d185ac10c49e42f5a to your computer and use it in GitHub Desktop.
Telekhram bot
<?php
$token = '';
$last_offset_filename = 'last_offset.txt';
$last_offset = file_exists($last_offset_filename) ? file_get_contents($last_offset_filename) : '0';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
]);
function getUpdates() {
global $token, $ch, $last_offset;
curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot$token/getUpdates?timeout=15&offset=$last_offset&allowed_updates[]=message");
return json_decode(curl_exec($ch), true);
}
function sendMessage($chat_id, $text, $parse_mode = null) {
global $token, $ch;
$query = ['chat_id' => $chat_id, 'text' => $text];
if ($parse_mode) {
$query['parse_mode'] = $parse_mode;
}
curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot$token/sendMessage?" . http_build_query($query));
curl_exec($ch);
}
while (true) {
$updates = getUpdates();
foreach ($updates['result'] as $update) {
$msg = $update['message'];
$text = $msg['text'];
$chat_id = $msg['chat']['id'];
if ($text === 'hi') {
sendMessage($chat_id, '<i>Hello!!!</i>', 'HTML');
}elseif ($text === '#quote') {
sendMessage($chat_id, '<i>Control is an illusion...', 'HTML');
}elseif ($text === '#season_3.0' || $text === '#season_3' || $text === '#season3') {
sendMessage($chat_id, '<p>Recording had been started in February 2017, and the 3rd season will release in October 2017</p>', 'HTML');
}
else {
sendMessage($chat_id, '<pre>Unknown command</pre>', 'HTML');
}
}
if (isset($updates['result']) && sizeof($updates['result']) > 0) {
$last_offset = $updates['result'][sizeof($updates['result']) - 1]['update_id'] + 1;
file_put_contents($last_offset_filename, $last_offset);
}
sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment