Created
October 30, 2023 19:31
-
-
Save alizadeh118/0daabd694d90b0672aa51db2c2eb7b84 to your computer and use it in GitHub Desktop.
PHP script for a Telegram bot that forwards incoming messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// URL for Telegram API functions | |
const TELEGRAM_API_URL = "https://api.telegram.org/botYOUR-TOKEN/"; | |
// Get the JSON data from the incoming POST request | |
$data = json_decode(file_get_contents("php://input"), true); | |
// Check if the message is set and is not "/start" | |
if (isset($data["message"]) && (!isset($data["message"]["text"]) || $data["message"]["text"] !== "/start")) { | |
$chat_id = $data["message"]["chat"]["id"]; | |
$caption = isset($data["message"]["caption"]) ? $data["message"]["caption"] : null; | |
if (isset($data["message"]["text"])) { // Text message | |
$text = $data["message"]["text"]; | |
$sendMessageData = [ | |
"chat_id" => $chat_id, | |
"text" => $text | |
]; | |
file_get_contents(TELEGRAM_API_URL . "sendMessage?" . http_build_query($sendMessageData)); | |
} elseif (isset($data["message"]["photo"])) { // Photo message | |
$photo_id = $data["message"]["photo"][0]["file_id"]; | |
$sendMessageData = [ | |
"chat_id" => $chat_id, | |
"photo" => $photo_id, | |
"caption" => $caption | |
]; | |
file_get_contents(TELEGRAM_API_URL . "sendPhoto?" . http_build_query($sendMessageData)); | |
} elseif (isset($data["message"]["audio"])) { // Audio message | |
$audio_id = $data["message"]["audio"]["file_id"]; | |
$sendMessageData = [ | |
"chat_id" => $chat_id, | |
"audio" => $audio_id, | |
"caption" => $caption | |
]; | |
file_get_contents(TELEGRAM_API_URL . "sendAudio?" . http_build_query($sendMessageData)); | |
} elseif (isset($data["message"]["document"])) { // Document message | |
$document_id = $data["message"]["document"]["file_id"]; | |
$sendMessageData = [ | |
"chat_id" => $chat_id, | |
"document" => $document_id, | |
"caption" => $caption | |
]; | |
file_get_contents(TELEGRAM_API_URL . "sendDocument?" . http_build_query($sendMessageData)); | |
} elseif (isset($data["message"]["video"])) { // Video message | |
$video_id = $data["message"]["video"]["file_id"]; | |
$sendMessageData = [ | |
"chat_id" => $chat_id, | |
"video" => $video_id, | |
"caption" => $caption | |
]; | |
file_get_contents(TELEGRAM_API_URL . "sendVideo?" . http_build_query($sendMessageData)); | |
} else { // Other unsupported message types | |
$sendMessageData = [ | |
"chat_id" => $chat_id, | |
"text" => "This type of message is not supported by the bot." | |
]; | |
file_get_contents(TELEGRAM_API_URL . "sendMessage?" . http_build_query($sendMessageData)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment