forked from mahdiMGF2/mirzabot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbotapi.php
More file actions
267 lines (238 loc) · 8.74 KB
/
botapi.php
File metadata and controls
267 lines (238 loc) · 8.74 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
require_once 'config.php';
function telegram($method, $datas = [], $token = null)
{
global $APIKEY;
$token = $token === null ? $APIKEY : $token;
$url = "https://api.telegram.org/bot" . $token . "/" . $method;
if (isset($datas['message_thread_id']) && intval($datas['message_thread_id']) <= 0) {
unset($datas['message_thread_id']);
}
$ch = curl_init($url);
if ($ch === false) {
error_log('Unable to initialise cURL for Telegram request.');
return [
'ok' => false,
'description' => 'Unable to initialise cURL for Telegram request.'
];
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
$rawResponse = curl_exec($ch);
if ($rawResponse === false) {
$curlError = curl_error($ch);
curl_close($ch);
if ($curlError !== '') {
error_log('Telegram request failed: ' . $curlError);
}
return [
'ok' => false,
'description' => $curlError !== '' ? $curlError : 'Telegram request failed.'
];
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$decodedResponse = json_decode($rawResponse, true);
if (!is_array($decodedResponse)) {
$logSnippet = substr($rawResponse, 0, 200);
error_log(sprintf('Invalid response from Telegram API (HTTP %d): %s', $httpCode, $logSnippet));
return [
'ok' => false,
'error_code' => $httpCode,
'description' => 'Invalid response received from Telegram.'
];
}
if (isset($decodedResponse['ok']) && !$decodedResponse['ok']) {
error_log(json_encode($decodedResponse));
}
return $decodedResponse;
}
function sendmessage($chat_id,$text,$keyboard,$parse_mode,$bot_token = null){
if(intval($chat_id) == 0)return ['ok' => false];
return telegram('sendmessage',[
'chat_id' => $chat_id,
'text' => $text,
'reply_markup' => $keyboard,
'parse_mode' => $parse_mode,
],$bot_token);
}
function sendDocument($chat_id, $documentPath, $caption) {
return telegram('sendDocument',[
'chat_id' => $chat_id,
'document' => new CURLFile($documentPath),
'caption' => $caption,
]);
}
function forwardMessage($chat_id,$message_id,$chat_id_user){
return telegram('forwardMessage',[
'from_chat_id'=> $chat_id,
'message_id'=> $message_id,
'chat_id'=> $chat_id_user,
]);
}
function sendphoto($chat_id,$photoid,$caption){
telegram('sendphoto',[
'chat_id' => $chat_id,
'photo'=> $photoid,
'caption'=> $caption,
]);
}
function sendvideo($chat_id,$videoid,$caption){
telegram('sendvideo',[
'chat_id' => $chat_id,
'video'=> $videoid,
'caption'=> $caption,
]);
}
function senddocumentsid($chat_id,$documentid,$caption){
telegram('sendDocument',[
'chat_id' => $chat_id,
'document'=> $documentid,
'caption'=> $caption,
]);
}
function Editmessagetext($chat_id, $message_id, $text, $keyboard,$parse_mode = 'HTML'){
return telegram('editmessagetext', [
'chat_id' => $chat_id,
'message_id' => $message_id,
'text' => $text,
'reply_markup' => $keyboard,
'parse_mode' => $parse_mode,
]);
}
function deletemessage($chat_id, $message_id){
telegram('deletemessage', [
'chat_id' => $chat_id,
'message_id' => $message_id,
]);
}
function getFileddire($photoid){
return telegram('getFile', [
'file_id' => $photoid,
]);
}
function pinmessage($from_id,$message_id){
return telegram('pinChatMessage', [
'chat_id' => $from_id,
'message_id' => $message_id,
]);
}
function unpinmessage($from_id){
return telegram('unpinAllChatMessages', [
'chat_id' => $from_id,
]);
}
function answerInlineQuery($inline_query_id,$results){
return telegram('answerInlineQuery', [
"inline_query_id" => $inline_query_id,
"results" => json_encode($results)
]);
}
function convertPersianNumbersToEnglish($string) {
$persian_numbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
$english_numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
return str_replace($persian_numbers, $english_numbers, $string);
}
function isDuplicateUpdate($updateId)
{
if (!is_numeric($updateId) || $updateId <= 0) {
return false;
}
$cacheDir = __DIR__ . '/storage/cache';
if (!is_dir($cacheDir) && !mkdir($cacheDir, 0775, true) && !is_dir($cacheDir)) {
return false;
}
$cacheFile = $cacheDir . '/recent_updates.json';
$handle = fopen($cacheFile, 'c+');
if ($handle === false) {
return false;
}
try {
if (!flock($handle, LOCK_EX)) {
fclose($handle);
return false;
}
rewind($handle);
$contents = stream_get_contents($handle);
$recentUpdates = $contents ? json_decode($contents, true) : [];
if (!is_array($recentUpdates)) {
$recentUpdates = [];
}
$now = time();
$timeToLive = 120; // seconds
// Drop expired entries
foreach ($recentUpdates as $id => $timestamp) {
if (!is_numeric($timestamp) || ($now - (int)$timestamp) > $timeToLive) {
unset($recentUpdates[$id]);
}
}
if (array_key_exists($updateId, $recentUpdates)) {
flock($handle, LOCK_UN);
fclose($handle);
return true;
}
$recentUpdates[$updateId] = $now;
// keep size reasonable
if (count($recentUpdates) > 200) {
asort($recentUpdates);
$recentUpdates = array_slice($recentUpdates, -200, null, true);
}
$encoded = json_encode($recentUpdates);
if ($encoded !== false) {
rewind($handle);
ftruncate($handle, 0);
fwrite($handle, $encoded);
}
flock($handle, LOCK_UN);
fclose($handle);
} catch (Throwable $e) {
try {
flock($handle, LOCK_UN);
} catch (Throwable $ignored) {
}
fclose($handle);
return false;
}
return false;
}
// #-----------------------------#
$update = json_decode(file_get_contents("php://input"), true);
$update_id = $update['update_id'] ?? 0;
if (isDuplicateUpdate($update_id)) {
http_response_code(200);
exit;
}
$from_id = $update['message']['from']['id'] ?? $update['callback_query']['from']['id'] ?? $update["inline_query"]['from']['id'] ?? 0;
$time_message = $update['message']['date'] ?? $update['callback_query']['date'] ?? $update["inline_query"]['date'] ?? 0;
$is_bot = $update['message']['from']['is_bot'] ?? false;
$chat_member = $update['chat_member'] ?? null;
$language_code = strtolower($update['message']['from']['language_code'] ?? $update['callback_query']['from']['language_code'] ?? "fa");
$Chat_type = $update["message"]["chat"]["type"] ?? $update['callback_query']['message']['chat']['type'] ?? '';
$text = $update["message"]["text"] ?? '';
if(isset($update['pre_checkout_query'])){
$Chat_type = "private";
$from_id = $update['pre_checkout_query']['from']['id'];
}
$text =convertPersianNumbersToEnglish($text);
$text_inline = $update["callback_query"]["message"]['text'] ?? '';
$message_id = $update["message"]["message_id"] ?? $update["callback_query"]["message"]["message_id"] ?? 0;
$time_message = $update["message"]["date"] ?? $update["callback_query"]["date"] ?? 0;
$photo = $update["message"]["photo"] ?? 0;
$document = $update["message"]["document"] ?? 0;
$fileid = $update["message"]["document"]["file_id"] ?? 0;
$photoid = $photo ? end($photo)["file_id"] : '';
$caption = $update["message"]["caption"] ?? '';
$video = $update["message"]["video"] ?? 0;
$videoid = $video ? $video["file_id"] : 0;
$forward_from_id = $update["message"]["reply_to_message"]["forward_from"]["id"] ?? 0;
$datain = $update["callback_query"]["data"] ?? '';
$last_name = $update['message']['from']['last_name'] ?? $update["callback_query"]["from"]["last_name"] ?? $update["inline_query"]['from']['last_name'] ?? '';
$first_name = $update['message']['from']['first_name'] ?? $update["callback_query"]["from"]["first_name"] ?? $update["inline_query"]['from']['first_name'] ?? '';
$username = $update['message']['from']['username'] ?? $update['callback_query']['from']['username'] ?? $update["callback_query"]["from"]["username"] ?? 'NOT_USERNAME';
$user_phone =$update["message"]["contact"]["phone_number"] ?? 0;
$contact_id = $update["message"]["contact"]["user_id"] ?? 0;
$callback_query_id = $update["callback_query"]["id"] ?? 0;
$inline_query_id = $update["inline_query"]["id"] ?? 0;
$query = $update["inline_query"]["query"] ?? 0;