-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtg_bot_messenger.php
58 lines (51 loc) · 2.14 KB
/
tg_bot_messenger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
define('BOT_TOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('SPECIFIC_USER_ID', 'XXXXXXXXXXXXXXXXXXXXXXX'); //telegram id of the user you want to send messages to
define('ENCRYPTION_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); //encryption key for the bot
define('API_URL', 'https://api.telegram.org/bot' . BOT_TOKEN . '/');
function sendMessage($chat_id, $text) {
$url = API_URL . 'sendMessage';
$data = [
'chat_id' => $chat_id,
'text' => $text,
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
],
];
$context = stream_context_create($options);
file_get_contents($url, false, $context);
}
function encrypt($data) {
$iv = openssl_random_pseudo_bytes(16);
$encrypted = openssl_encrypt($data, 'AES-256-CBC', ENCRYPTION_KEY, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
function decrypt($data) {
list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
return openssl_decrypt($encrypted_data, 'AES-256-CBC', ENCRYPTION_KEY, 0, $iv);
}
$update = json_decode(file_get_contents('php://input'), true);
if (isset($update['message'])) {
$message = $update['message'];
$chat_id = $message['chat']['id'];
$text = $message['text'];
if ($chat_id == SPECIFIC_USER_ID) {
if (preg_match('/Anonymous message \(User: (.+)\):/', $text, $matches)) {
$encrypted_chat_id = $matches[1];
$user_chat_id = decrypt($encrypted_chat_id);
$reply = trim(explode("\n", $text, 2)[1]);
sendMessage($user_chat_id, "Reply from support:\n$reply");
sendMessage(SPECIFIC_USER_ID, "Your reply has been sent to the user.");
}
}
if ($chat_id != SPECIFIC_USER_ID) {
$encrypted_chat_id = encrypt($chat_id);
$forwarded_message = "Anonymous message (User: $encrypted_chat_id):\n$text";
sendMessage(SPECIFIC_USER_ID, $forwarded_message);
sendMessage($chat_id, "Thank you for your message! We have received it anonymously.");
}
}