telegram_php_webhook/telegram_webhook.php

109 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2023-03-09 14:58:38 +01:00
<?php
/*
Author: info@meer-web.nl
Web: https://meer-web.nl
2023-04-12 16:40:50 +02:00
Version: 2.1.0
2023-03-09 14:58:38 +01:00
#URL to get the webhook info
https://api.telegram.org/bot<BOT_ID>/getWebhookInfo
# URL to set the webhook
curl -F "url=https://<DOMAIN.TLD>/<YOUR_WEBHOOK_PHP>" 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.
2023-04-12 16:35:08 +02:00
picture - Post a picture
gif - Post a gif
2023-03-09 14:58:38 +01:00
*/
2023-04-12 16:35:08 +02:00
/********************** Variables to set **********************/
// Set your Bot ID.
$TELEGRAM_BOT='123456789:ABCDEFXXXXXXXXXXXXXXXXXXXXX';
2023-03-09 14:58:38 +01:00
2023-04-12 16:35:08 +02:00
/********************** Start script **********************/
// Telegram function which you can call
2023-04-12 16:40:50 +02:00
function telegram($METHOD, $MSG) {
global $TELEGRAM_BOT, $TELEGRAM_CHATID, $CAPTION;
2023-04-12 16:35:08 +02:00
if ($METHOD == 'TXT') {
$url='https://api.telegram.org/bot'.$TELEGRAM_BOT.'/sendMessage';$data=array('chat_id'=>$TELEGRAM_CHATID,'text'=>$MSG);
}
if ($METHOD == 'IMG') {
if ($CAPTION != '') {
$url='https://api.telegram.org/bot'.$TELEGRAM_BOT.'/sendPhoto';$data=array('chat_id'=>$TELEGRAM_CHATID,'photo'=>$IMG,'caption'=>$CAPTION);
} else {
$url='https://api.telegram.org/bot'.$TELEGRAM_BOT.'/sendPhoto';$data=array('chat_id'=>$TELEGRAM_CHATID,'photo'=>$IMG);
}
}
if ($METHOD == 'GIF') {
if ($CAPTION != '') {
$url='https://api.telegram.org/bot'.$TELEGRAM_BOT.'/sendAnimation';$data=array('chat_id'=>$TELEGRAM_CHATID,'animation'=>$GIF,'caption'=>$CAPTION);
} else {
$url='https://api.telegram.org/bot'.$TELEGRAM_BOT.'/sendAnimation';$data=array('chat_id'=>$TELEGRAM_CHATID,'animation'=>$GIF);
}
}
2023-03-09 14:58:38 +01:00
$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_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);
// Get first name of user sending message
$FIRSTNAME = $TELEGRAM_RCV["message"]["from"]["first_name"];
$LASTNAME = $TELEGRAM_RCV["message"]["from"]["last_name"];
$USERID = $TELEGRAM_RCV["message"]["from"]["id"];
2023-03-09 14:58:38 +01:00
// Trim off botname
if(strpos($MESSAGE[0], '@') !== false){
$COMMAND = explode('@', $MESSAGE[0]);
$COMMAND = $COMMAND[0];
} else {
$COMMAND = $MESSAGE[0];
}
2023-04-12 16:35:08 +02:00
// Set empty caption
$CAPTION = '';
2023-03-09 14:58:38 +01:00
// Reply
switch ($MESSAGE[0]) {
case '/ping':
// Friendly reply
2023-04-12 16:35:08 +02:00
$MSG = "Hi $FIRSTNAME!";
2023-04-12 16:40:50 +02:00
telegram('TXT', "$MSG");
2023-03-09 14:58:38 +01:00
break;
case '/time':
// Send current time
2023-04-12 16:35:08 +02:00
$MSG = "The current time is " . date('d-m-Y H:i:s');
2023-04-12 16:40:50 +02:00
telegram('TXT', "$MSG");
2023-03-09 14:58:38 +01:00
break;
case '/chatid':
// Show chatid
2023-04-12 16:35:08 +02:00
$MSG = "$TELEGRAM_CHATID";
2023-04-12 16:40:50 +02:00
telegram('TXT', "$MSG");
2023-03-09 14:58:38 +01:00
break;
case '/picture':
2023-04-12 16:35:08 +02:00
// Show an image with caption (or leave caption empty)
$MSG = "https://www.creativefabrica.com/wp-content/uploads/2022/04/11/Turtle-Graphics-3812807.jpg";
$CAPTION = 'Here you have a little turtle';
2023-04-12 16:40:50 +02:00
telegram('IMG', "$MSG");
2023-03-09 14:58:38 +01:00
break;
2023-04-12 16:35:08 +02:00
case '/gif':
// Show a gif with caption (or leave caption empty)
$MSG = "https://media2.giphy.com/media/LMVkZXubrWzcaZNq10/giphy.gif";
$CAPTION = 'Here you have a swimming turtle';
2023-04-12 16:40:50 +02:00
telegram('GIF', "$MSG");
2023-04-12 16:35:08 +02:00
break;
2023-03-09 14:58:38 +01:00
}
2023-04-12 16:35:08 +02:00
?>