-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
240 lines (214 loc) · 10.7 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JokeBot</title>
</head>
<body>
<h1>JokeBots_Bot</h1>
<a href="https://t.me/JokeBots_Bot">
<button style="cursor: pointer;">Open Bot</button>
</a>
</body>
</html>
<?php
/**
* index.php
*
* Main script to handle updates from the Telegram bot.
* Processes incoming messages and callback queries, interacts with the database,
* and sends responses based on user interactions.
*/
require 'functions.php'; // Include external functions for bot operations
// Get the raw input from the incoming update
$content = file_get_contents('php://input');
// Decode the JSON content into a PHP associative array
$update = json_decode($content, true);
// Extract user message details from the update
$update_id = $update['update_id']; // ID of the update
$chat_id = $update['message']['chat']['id']; // User chat ID
$text = $update['message']['text']; // Text of the user’s message
$message_id = $update['message']['message_id']; // ID of the message
$user_id = $update['message']['chat']['username']; // Username of the user
$user_name = $update['message']['chat']['first_name']; // First name of the user
// Extract callback query details if available
$callback_chat_id = $update['callback_query']['message']['chat']['id'] ?? null; // Chat ID from callback
$callback_data = $update['callback_query']['data'] ?? null; // Data from the callback
$callback_id = $update['callback_query']['id'] ?? null; // ID of the callback
$callback_message = $update['callback_query']['message']['text'] ?? null; // Message text from callback
$callback_message_id = $update['callback_query']['message']['message_id'] ?? null; // Message ID from callback
$bot_id = $update['callback_query']['from']['id'] ?? null; // Bot ID from callback
// Optionally log the incoming update (commented out for now)
// file_put_contents('update_log.json', print_r($content, true));
// Insert the raw update content into the `update_log` table for debugging or logging purposes
$sql = "INSERT INTO `update_log` (`id`, `json`) VALUES ('$update_id', '$content')";
$result = mysqli_query($conn, $sql);
if (!$result) {
$error = mysqli_error($conn);
msg('sendMessage', array('chat_id' => $chat_id, 'text' => $error));
}
// Check if there is any callback data, and set UID based on whether it's a callback query or a regular message
if ($callback_data != null) {
define('UID', $callback_chat_id); // Set UID for callback queries
$callback_data = cCheck($callback_data, $callback_id); // Check and process the callback data
} else {
define('UID', $chat_id); // Set UID for regular messages
}
// Get the user's language preference from the database
$uid = UID;
$sql = "SELECT lang FROM `users` WHERE `uid` = '$uid'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$lang = $row['lang'] ?? 'en'; // Default to 'en' if no language preference is set
// Define a constant for the user's language
define('LANG', $lang);
// Translate the user message to the user's language
$text = GoogleTranslate::translate(LANG, 'en', $text);
// Adjust the text if it contains certain keywords
if (strpos($text, '🏠') !== false) {
$text = 'Home | 🏠';
}
if (strpos($text, '🔄️') !== false) {
$text = 'Change Category | 🔄️';
}
// Handle user messages based on the text content
switch ($text) {
case '/start': // Command for starting the bot or the home button
case 'Home | 🏠':
// Check if the user exists in the database
$sql = "SELECT * FROM `users` WHERE `uid` = '$chat_id'";
$result = mysqli_query($conn, $sql);
// Handle database connection errors
if (!$result) {
$db_error = mysqli_error($conn);
msg('sendMessage', array('chat_id' => $chat_id, 'text' => $db_error));
die();
}
// If user exists, update their data; if not, create a new user record
if (mysqli_num_rows($result) > 0) {
$sql = "UPDATE `users` SET `name` = '$user_name', `user` = '$user_id', `type` = NULL, `cat` = NULL WHERE `uid` = '$chat_id'";
$result = mysqli_query($conn, $sql);
} else {
$sql = "INSERT INTO `users` (`uid`, `name`, `user`, `type`, `cat`, `lang`) VALUES ('$chat_id', '$user_name', '$user_id', NULL, NULL, NULL)";
$result = mysqli_query($conn, $sql);
}
// Check if the user has set their language preference
$sql = "SELECT lang FROM `users` WHERE `uid` = '$chat_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['lang'] == NULL) {
// Prompt the user to select a language if it's not set
msg('sendMessage', array('chat_id' => $chat_id, 'text' => text('language'), 'reply_markup' => keyboard('language'), 'parse_mode' => 'HTML'));
} else {
// Send welcome messages if the language is set
msg('sendMessage', array('chat_id' => $chat_id, 'text' => text('wait'), 'reply_markup' => keyboard('remove')));
msg('sendMessage', array('chat_id' => $chat_id, 'text' => text('welcome'), 'reply_markup' => keyboard('home')));
}
break;
case 'Change Category | 🔄️':
// Set user preference to 'custom' for category selection
$sql = "UPDATE `users` SET `type` = 'custom', `cat` = NULL WHERE `uid` = '$chat_id'";
$result = mysqli_query($conn, $sql);
msg('sendMessage', array('chat_id' => $chat_id, 'text' => text('wait'), 'reply_markup' => keyboard('remove')));
msg('sendMessage', array('chat_id' => $chat_id, 'text' => text('custom_cat'), 'reply_markup' => keyboard('cat')));
break;
default:
// Handle messages that are not commands or predefined options
$sql = "SELECT * FROM `users` WHERE `uid` = '$chat_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['type'] == 'custom') {
// Fetch a joke based on the user's selected category
$cat = $row['cat'];
$joke = Joke($cat, $text);
$error = $joke['error'];
$jokeCat = $joke['category'];
$jokeText = $joke['joke'];
switch ($error) {
case 'true':
msg('sendMessage', array('chat_id' => $chat_id, 'text' => text('custom_error')));
break;
default:
msg('sendMessage', array('chat_id' => $chat_id, 'text' => JokeMsg($jokeCat, $jokeText)));
break;
}
sleep(3); // Wait for 3 seconds before sending the next message
msg('sendMessage', array('chat_id' => $chat_id, 'text' => text('custom_text')));
}
break;
}
// Handle callback queries based on the data
switch ($callback_data) {
case 'home':
// Reset the user’s type to NULL and send a welcome message
$sql = "UPDATE `users` SET `type` = NULL WHERE `uid` = '$callback_chat_id'";
$result = mysqli_query($conn, $sql);
msg('editMessageText', array('chat_id' => $callback_chat_id, 'message_id' => $callback_message_id, 'text' => text('welcome'), 'reply_markup' => keyboard('home')));
break;
case 'random':
// Set the user’s type to 'random' and prompt for category selection
$sql = "UPDATE `users` SET `type` = 'random' WHERE `uid` = '$callback_chat_id'";
$result = mysqli_query($conn, $sql);
if (!$result) {
$db_error = mysqli_error($conn);
msg('sendMessage', array('chat_id' => $chat_id, 'text' => $db_error));
die();
}
msg('editMessageText', array('chat_id' => $callback_chat_id, 'message_id' => $callback_message_id, 'text' => text('random'), 'reply_markup' => keyboard('cat')));
break;
case 'custom':
// Set the user’s type to 'custom' for category selection
$sql = "UPDATE `users` SET `type` = 'custom' WHERE `uid` = '$callback_chat_id'";
$result = mysqli_query($conn, $sql);
msg('editMessageText', array('chat_id' => $callback_chat_id, 'message_id' => $callback_message_id, 'text' => text('custom_cat'), 'reply_markup' => keyboard('cat')));
break;
case 'info':
// Provide information about the bot
msg('editMessageText', array(
'chat_id' => $callback_chat_id,
'message_id' => $callback_message_id,
'text' => text('info'),
'link_preview_options' => ['url' => 'https://t.me/Meytttii'],
'reply_markup' => keyboard('info'),
'parse_mode' => 'HTML'
));
break;
case 'language':
// Prompt the user to select a language
msg('editMessageText', array('chat_id' => $callback_chat_id, 'message_id' => $callback_message_id, 'text' => text('language'), 'reply_markup' => keyboard('language'), 'parse_mode' => 'HTML'));
break;
default:
// Handle default callback query actions based on the user’s type
$sql = "SELECT * FROM `users` WHERE `uid` = '$callback_chat_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['type'] == 'random') {
// Fetch a random joke based on the selected category
msg('deleteMessage', array('chat_id' => $callback_chat_id, 'message_id' => $callback_message_id));
$cat = $callback_data;
$text = '';
$joke = Joke($cat, $text);
$error = $joke['error'];
$jokeCat = $joke['category'];
$jokeText = $joke['joke'];
switch ($error) {
case 'true':
msg('sendMessage', array('chat_id' => $callback_chat_id, 'text' => text('random_error')));
break;
default:
msg('sendMessage', array('chat_id' => $callback_chat_id, 'text' => JokeMsg($jokeCat, $jokeText)));
break;
}
sleep(3); // Wait for 3 seconds before sending the next message
msg('sendMessage', array('chat_id' => $callback_chat_id, 'text' => text('random'), 'reply_markup' => keyboard('cat')));
} elseif ($row['type'] == 'custom') {
// Update the category based on the callback data
$sql = "UPDATE `users` SET `cat` = '$callback_data' WHERE `uid` = '$callback_chat_id'";
$result = mysqli_query($conn, $sql);
msg('deleteMessage', array('chat_id' => $callback_chat_id, 'message_id' => $callback_message_id));
msg('sendMessage', array('chat_id' => $callback_chat_id, 'text' => text('custom_text'), 'reply_markup' => keyboard('custom_text')));
}
break;
}
?>