Created
November 4, 2021 07:15
-
-
Save ahbanavi/282e4145e94307b3d478df0eae6e865f to your computer and use it in GitHub Desktop.
Simple Mirror for Telegram API requests
This file contains 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 | |
const PASSPHRASE = 'your-secret-pass-phrase-for-encryption'; | |
$encrypted_data = file_get_contents('php://input'); | |
$data = decrypt($encrypted_data); | |
if (!isset($data['method'], $data['request'], $data['bot_token'])){ | |
die('no method or request'); | |
} | |
$response = curl_post_json($data['bot_token'], $data['method'], $data['request']); | |
$encrypted_data = encrypt($response); | |
echo $encrypted_data; | |
// functions | |
function curl_post_json(string $bot_token, string $method, array $data): string | |
{ | |
$data_string = json_encode($data); | |
$ch = curl_init("https://api.telegram.org/bot{$bot_token}/{$method}"); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string)) | |
); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
function encrypt(string $data): string | |
{ | |
$data_json_64 = base64_encode($data); | |
$secret_key = hex2bin(PASSPHRASE); | |
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-gcm')); | |
$tag = ''; | |
$encrypted_64 = openssl_encrypt($data_json_64, 'aes-256-gcm', $secret_key, 0, $iv, $tag); | |
$json = new stdClass(); | |
$json->iv = base64_encode($iv); | |
$json->data = $encrypted_64; | |
$json->tag = base64_encode($tag); | |
return base64_encode(json_encode($json)); | |
} | |
function decrypt(string $data): array | |
{ | |
$secret_key = hex2bin(PASSPHRASE); | |
$json = json_decode(base64_decode($data), true); | |
$iv = base64_decode($json['iv']); | |
$tag = base64_decode($json['tag']); | |
$encrypted_data = base64_decode($json['data']); | |
$decrypted_data = openssl_decrypt($encrypted_data, 'aes-256-gcm', $secret_key, OPENSSL_RAW_DATA, $iv, $tag); | |
return json_decode(base64_decode($decrypted_data),True); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample JSON input for telegram apis
sendMessage
method: