Skip to content

Instantly share code, notes, and snippets.

@theMiddleBlue
Created December 22, 2016 08:06
Show Gist options
  • Save theMiddleBlue/6d5e9082e0c3c378bfb037795b2570b8 to your computer and use it in GitHub Desktop.
Save theMiddleBlue/6d5e9082e0c3c378bfb037795b2570b8 to your computer and use it in GitHub Desktop.
PHP Telegram Webhook
<?php
if(!preg_match('/^149\.154\.167\.(19[7-9]|20[0-9]|21[0-9]|22[0-9]|23[0-3])$/', $_SERVER['REMOTE_ADDR'])) {
die('IP Address not allowed.');
}
if($_SERVER['REQUEST_METHOD'] != 'POST') {
die('Request method not allowed.');
}
$token = '<bot token here>';
$rcv = file_get_contents('php://input');
$m = json_decode($rcv, true);
print_r($m);
if(isset($m['message']['text'])) {
$chatid = $m['message']['chat']['id'];
$firstname = $m['message']['chat']['first_name'];
sendmsg($m, 'Hi '.$firstname.'!');
}
function sendmsg($m, $msg) {
global $token;
$chatid = $m['message']['chat']['id'];
$firstname = $m['message']['chat']['first_name'];
unset($a); exec('curl -s -d "chat_id='.$chatid.'&text='.urlencode($msg).'" "https://api.telegram.org/bot'.$token.'/sendMessage"', $a);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment