From f1739d55c3b0759bcb90f2fa8853de968b9109f0 Mon Sep 17 00:00:00 2001 From: Frank Bischof Date: Thu, 9 Mar 2023 14:58:38 +0100 Subject: [PATCH] Innitial commit --- README.md | 28 +++++++++++++++ telegram_webhook.php | 81 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 README.md create mode 100644 telegram_webhook.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..011f269 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# telegram-php-webhook +PHP Webhook for telegram bots + +## Install +Create a bot using the botfather (https://core.telegram.org/bots/tutorial) +Set the bot webhook using setWebhook. + +Ea: +``` +curl -F "url=https:///" https://api.telegram.org/bot/setWebhook +``` + +Afterwards check if the webhook info is set properly on https://api.telegram.org/bot/getWebhookInfo + +## Setup +Only thing to set is the BOT ID in the code. +``` +$TELEGRAM_BOT = '15560000000:AAFoWcuFvqXXXXXXXXXXXXXXXXXXXXX'; +``` + +## Usage +You can use the following commands as an example: +/ping +/time +/chatid +/picture + +You can easily add more in the case statement in the code. \ No newline at end of file diff --git a/telegram_webhook.php b/telegram_webhook.php new file mode 100644 index 0000000..794ee81 --- /dev/null +++ b/telegram_webhook.php @@ -0,0 +1,81 @@ +/getWebhookInfo +# URL to set the webhook +curl -F "url=https:///" https://api.telegram.org/bot<$TELEGRAM_BOT>/setWebhook + +# COMMANDS for botfather to take in +help - Show help +ping - Check keepalive +time - Shows the current time. +picture - Reply with a picture +*/ + +// Telegram function for posting text. +function telegram($msg) { + global $TELEGRAM_BOT,$TELEGRAM_CHATID; + $url='https://api.telegram.org/bot'.$TELEGRAM_BOT.'/sendMessage';$data=array('chat_id'=>$TELEGRAM_CHATID,'text'=>$msg); + $options=array('http'=>array('method'=>'POST','header'=>"Content-Type:application/x-www-form-urlencoded\r\n",'content'=>http_build_query($data),),); + $context=stream_context_create($options); + $result=file_get_contents($url,false,$context); + return $result; +} + +// Telegram function for posting images. +function telegram_pic($msg, $caption) { + global $TELEGRAM_BOT,$TELEGRAM_CHATID; + $url='https://api.telegram.org/bot'.$TELEGRAM_BOT.'/sendPhoto';$data=array('chat_id'=>$TELEGRAM_CHATID,'photo'=>$msg,'caption'=>$caption); + $options=array('http'=>array('method'=>'POST','header'=>"Content-Type:application/x-www-form-urlencoded\r\n",'content'=>http_build_query($data),),); + $context=stream_context_create($options); + $result=file_get_contents($url,false,$context); + return $result; +} + +// Set your Bot ID and Chat ID. +$TELEGRAM_BOT='123456789:ABCDEFXXXXXXXXXXXXXXXXXXXXX'; + +$TELEGRAM_API = "https://api.telegram.org/bot$TELEGRAM_BOT"; +$TELEGRAM_RCV = json_decode(file_get_contents("php://input"), TRUE); + +// ChatID needed to reply in the same chat +$TELEGRAM_CHATID = $TELEGRAM_RCV["message"]["chat"]["id"]; + +// Fetch the first word, ea for commands +$MESSAGE = explode(" ", strtolower($TELEGRAM_RCV["message"]["text"])); + +$MESSAGE_FULL = $TELEGRAM_RCV["message"]["text"]; +$MESSAGE_FULL = substr(strstr("$MESSAGE_FULL"," "), 1); + +// Trim off botname +if(strpos($MESSAGE[0], '@') !== false){ + $COMMAND = explode('@', $MESSAGE[0]); + $COMMAND = $COMMAND[0]; +} else { + $COMMAND = $MESSAGE[0]; +} + +// Reply +switch ($MESSAGE[0]) { + case '/ping': + // Friendly reply + telegram("Hi!"); + break; + case '/time': + // Send current time + $now = date('d-m-Y H:i:s'); + telegram("The current time is $now"); + break; + case '/chatid': + // Show chatid + telegram("$TELEGRAM_CHATID"); + break; + case '/picture': + // Reply image (use of different function) + telegram_pic("URL OF IMAGE","YOUR CAPTION"); + break; + } +?>