-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup telegram.php
412 lines (367 loc) · 19.9 KB
/
backup telegram.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*ALTER DATABASE telegram CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE chat CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE quistions CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE repeared CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;*/
$servername ='localhost';
$username = 'userTelegram';
$password = 'aa951753';
$database = 'telegramBot';
$conn = mysqli_connect($servername, $username, $password, $database);
// mysqli_select_db($conn, $database);
$sSQL= 'SET CHARACTER SET utf8';
mysqli_query($conn,$sSQL);
ini_set("error_reporting", E_All); // to use telegram
$botToken = "302919708:AAGcqbRr4QLqq_Qn1PTEiFrXnYgE7bZ5Uxs";
$website = "https://api.telegram.org/bot".$botToken;
$update = file_get_contents('php://input'); // to get message
$update = json_decode($update, TRUE); // appear as jason
$chatId = $update["message"]["chat"]["id"]; // get id of user
$chatName = $update["message"]["chat"]["first_name"]; // get name of user
$message = $update["message"]["text"]; // get message from user
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
if(mysqli_num_rows($result) == 0) // if user isn't registered in database must be registered
{
$query = "insert into `chat` (`chat_id`,`solans`,`solT`,`solF`,`grade`,`name`) values ('$chatId','','0','0','0','$chatName')";
$result = mysqli_query($conn,$query);
$query_2 = "insert into `repeared` (`chat_id`,`q1`,`q2`,`q3`,`q4`,`q5`,`q6`,`q7`,`q8`,`q9`,`q10`,`q11`,`q12`) values ('$chatId','','','','','','','','','','','','')";
$result_2 = mysqli_query($conn,$query_2);
$query_2 = "insert into `field` (`chat_id`,`field_name`) values ('$chatId','')";
$result_2 = mysqli_query($conn,$query_2);
}
$exit_message = "";
if($message == '[exit]')
{
$message = '/start';
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
//$data[2]++;$data[3]++;
$exit_message .= "الاجابات الصحيحة = ".$data[2]."\nعدد الاجابات الخاطئة = ".$data[3]."\n\n\n";
}
$sol = false; // determine if the mesage is anwer or quistion
if ($message != '/start' && $message !='[سيرة]' && $message != '[معلومات عامة]' && $message != '[مسابقات قرآنية]' && $message != '[أسئلة حديثية]' && $message != '[أسئلة فقهية]' && $message != '[ألغاز]') {
$result = mysqli_query($conn,"select * from `field` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
$swap = $message;
$message = $data[1];
$sol = $swap;
}
else
{
mysqli_query($conn,"UPDATE `field` SET `field_name` = '$message' WHERE chat_id = '$chatId'");
}
switch ($message) {//chicking on message
case '/start':
mysqli_query($conn,"UPDATE `chat` SET `solans` = '' , `solT` = '0' , `solF` = '0' WHERE chat_id = '$chatId'");
mysqli_query($conn,"UPDATE `repeared` SET `q1` = '' , `q2` = '' , `q3` = '' , `q4` = '' , `q5` = '' , `q6` = '' , `q7` = '' , `q8` = '' , `q9` = '' , `q10` = '' , `q11` = '' , `q12` = '' WHERE chat_id = '$chatId'");
$keyboard = array(array("[مسابقات قرآنية]","[معلومات عامة]","[سيرة]"),array("[أسئلة حديثية]","[أسئلة فقهية]","[ألغاز]"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
$exit_message .= "اختر احد هذه الاقسام التالية";
sendMessage($chatId,$exit_message,$reply);
// sendPhoto($chatId);
break;
/*---------*/
/*---------*/
/*---------*/
case '[سيرة]':
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
$sum1=$data[2]; $sum2=$data[3];
$sum = $sum1 + $sum2 + 1;
if ($sol != false) // checking if there is a solution
{
if ($sol == $data[1]) { // checking if the solution is true
mysqli_query($conn,"UPDATE `chat` SET `solT` = `solT`+'1' WHERE chat_id = '$chatId'");
$data[2] = $data[2] + 1;
}
else// if the solution is false
{
mysqli_query($conn,"UPDATE `chat` SET `solF` = `solF`+'1' WHERE chat_id = '$chatId'");
$data[3] = $data[3] + 1;
}
}
if ($data[2]+$data[3] == 12) { // terminate the program after three quistions
$keyboard = array(array("/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '' , `solT` = '0' , `solF` = '0', `grade` = '$data[2]' WHERE chat_id = '$chatId'");
mysqli_query($conn,"UPDATE `repeared` SET `q1` = '' , `q2` = '' , `q3` = '' , `q4` = '' , `q5` = '' , `q6` = '' , `q7` = '' , `q8` = '' , `q9` = '' , `q10` = '' , `q11` = '' , `q12` = '' WHERE chat_id = '$chatId'");
sendMessage($chatId,"الاجابات الصحيحة = ".$data[2]."\nعدد الاجابات الخاطئة = ".$data[3],$reply);
}
else // asking anther quistion
{
$results = mysqli_query($conn,"SELECT * FROM `quistions` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results); // select random quistion from table quistions
$results_q = mysqli_query($conn,"SELECT * FROM `repeared` WHERE `chat_id` = '$chatId'");
$data_q = mysqli_fetch_row($results_q); // select all quistions that have been asked
while (in_array($data[1], $data_q)) //checking if the quistion is repeated
{
$results = mysqli_query($conn,"SELECT * FROM `quistions` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results);// then get anther quistion not repeated befor
}
mysqli_query($conn,"UPDATE `repeared` SET `q".$sum."` = '$data[1]' WHERE chat_id = '$chatId'");
/* print button to choice an answer */
$keyboard = array(array($data[2],$data[3],$data[4]),array($data[5],"[exit]","/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '$data[6]' WHERE chat_id = '$chatId'");
sendMessage($chatId,$data[1],$reply);
}
break;
/*---------*/
/*---------*/
/*---------*/
case '[معلومات عامة]':
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
$sum1=$data[2]; $sum2=$data[3];
$sum = $sum1 + $sum2 + 1;
if ($sol != false) // checking if there is a solution
{
if ($sol == $data[1]) { // checking if the solution is true
mysqli_query($conn,"UPDATE `chat` SET `solT` = `solT`+'1' WHERE chat_id = '$chatId'");
$data[2] = $data[2] + 1;
}
else// if the solution is false
{
mysqli_query($conn,"UPDATE `chat` SET `solF` = `solF`+'1' WHERE chat_id = '$chatId'");
$data[3] = $data[3] + 1;
}
}
if ($data[2]+$data[3] == 12) { // terminate the program after three quistions
$keyboard = array(array("/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '' , `solT` = '0' , `solF` = '0', `grade` = '$data[2]' WHERE chat_id = '$chatId'");
mysqli_query($conn,"UPDATE `repeared` SET `q1` = '' , `q2` = '' , `q3` = '' , `q4` = '' , `q5` = '' , `q6` = '' , `q7` = '' , `q8` = '' , `q9` = '' , `q10` = '' , `q11` = '' , `q12` = '' WHERE chat_id = '$chatId'");
sendMessage($chatId,"الاجابات الصحيحة = ".$data[2]."\nعدد الاجابات الخاطئة = ".$data[3],$reply);
}
else // asking anther quistion
{
$results = mysqli_query($conn,"SELECT * FROM `GI` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results); // select random quistion from table quistions
$results_q = mysqli_query($conn,"SELECT * FROM `repeared` WHERE `chat_id` = '$chatId'");
$data_q = mysqli_fetch_row($results_q); // select all quistions that have been asked
while (in_array($data[1], $data_q)) //checking if the quistion is repeated
{
$results = mysqli_query($conn,"SELECT * FROM `GI` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results);// then get anther quistion not repeated befor
}
mysqli_query($conn,"UPDATE `repeared` SET `q".$sum."` = '$data[1]' WHERE chat_id = '$chatId'");
/* print button to choice an answer */
$keyboard = array(array($data[2],$data[3],$data[4]),array($data[5],"[exit]","/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '$data[6]' WHERE chat_id = '$chatId'");
sendMessage($chatId,$data[1],$reply);
}
break;
/*---------*/
/*---------*/
/*---------*/
case '[مسابقات قرآنية]':
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
$sum1=$data[2]; $sum2=$data[3];
$sum = $sum1 + $sum2 + 1;
if ($sol != false) // checking if there is a solution
{
if ($sol == $data[1]) { // checking if the solution is true
mysqli_query($conn,"UPDATE `chat` SET `solT` = `solT`+'1' WHERE chat_id = '$chatId'");
$data[2] = $data[2] + 1;
}
else// if the solution is false
{
mysqli_query($conn,"UPDATE `chat` SET `solF` = `solF`+'1' WHERE chat_id = '$chatId'");
$data[3] = $data[3] + 1;
}
}
if ($data[2]+$data[3] == 12) { // terminate the program after three quistions
$keyboard = array(array("/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '' , `solT` = '0' , `solF` = '0', `grade` = '$data[2]' WHERE chat_id = '$chatId'");
mysqli_query($conn,"UPDATE `repeared` SET `q1` = '' , `q2` = '' , `q3` = '' , `q4` = '' , `q5` = '' , `q6` = '' , `q7` = '' , `q8` = '' , `q9` = '' , `q10` = '' , `q11` = '' , `q12` = '' WHERE chat_id = '$chatId'");
sendMessage($chatId,"الاجابات الصحيحة = ".$data[2]."\nعدد الاجابات الخاطئة = ".$data[3],$reply);
}
else // asking anther quistion
{
$results = mysqli_query($conn,"SELECT * FROM `quraan` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results); // select random quistion from table quistions
$results_q = mysqli_query($conn,"SELECT * FROM `repeared` WHERE `chat_id` = '$chatId'");
$data_q = mysqli_fetch_row($results_q); // select all quistions that have been asked
while (in_array($data[1], $data_q)) //checking if the quistion is repeated
{
$results = mysqli_query($conn,"SELECT * FROM `quraan` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results);// then get anther quistion not repeated befor
}
mysqli_query($conn,"UPDATE `repeared` SET `q".$sum."` = '$data[1]' WHERE chat_id = '$chatId'");
/* print button to choice an answer */
$keyboard = array(array($data[2],$data[3],$data[4]),array($data[5],"[exit]","/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '$data[6]' WHERE chat_id = '$chatId'");
sendMessage($chatId,$data[1],$reply);
}
break;
/*---------*/
/*---------*/
/*---------*/
case '[أسئلة حديثية]':
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
$sum1=$data[2]; $sum2=$data[3];
$sum = $sum1 + $sum2 + 1;
if ($sol != false) // checking if there is a solution
{
if ($sol == $data[1]) { // checking if the solution is true
mysqli_query($conn,"UPDATE `chat` SET `solT` = `solT`+'1' WHERE chat_id = '$chatId'");
$data[2] = $data[2] + 1;
}
else// if the solution is false
{
mysqli_query($conn,"UPDATE `chat` SET `solF` = `solF`+'1' WHERE chat_id = '$chatId'");
$data[3] = $data[3] + 1;
}
}
if ($data[2]+$data[3] == 12) { // terminate the program after three quistions
$keyboard = array(array("/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '' , `solT` = '0' , `solF` = '0', `grade` = '$data[2]' WHERE chat_id = '$chatId'");
mysqli_query($conn,"UPDATE `repeared` SET `q1` = '' , `q2` = '' , `q3` = '' , `q4` = '' , `q5` = '' , `q6` = '' , `q7` = '' , `q8` = '' , `q9` = '' , `q10` = '' , `q11` = '' , `q12` = '' WHERE chat_id = '$chatId'");
sendMessage($chatId,"الاجابات الصحيحة = ".$data[2]."\nعدد الاجابات الخاطئة = ".$data[3],$reply);
}
else // asking anther quistion
{
$results = mysqli_query($conn,"SELECT * FROM `hades` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results); // select random quistion from table quistions
$results_q = mysqli_query($conn,"SELECT * FROM `repeared` WHERE `chat_id` = '$chatId'");
$data_q = mysqli_fetch_row($results_q); // select all quistions that have been asked
while (in_array($data[1], $data_q)) //checking if the quistion is repeated
{
$results = mysqli_query($conn,"SELECT * FROM `hades` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results);// then get anther quistion not repeated befor
}
mysqli_query($conn,"UPDATE `repeared` SET `q".$sum."` = '$data[1]' WHERE chat_id = '$chatId'");
/* print button to choice an answer */
$keyboard = array(array($data[2],$data[3],$data[4]),array($data[5],"[exit]","/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '$data[6]' WHERE chat_id = '$chatId'");
sendMessage($chatId,$data[1],$reply);
}
break;
/*---------*/
/*---------*/
/*---------*/
case '[أسئلة فقهية]':
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
$sum1=$data[2]; $sum2=$data[3];
$sum = $sum1 + $sum2 + 1;
if ($sol != false) // checking if there is a solution
{
if ($sol == $data[1]) { // checking if the solution is true
mysqli_query($conn,"UPDATE `chat` SET `solT` = `solT`+'1' WHERE chat_id = '$chatId'");
$data[2] = $data[2] + 1;
}
else// if the solution is false
{
mysqli_query($conn,"UPDATE `chat` SET `solF` = `solF`+'1' WHERE chat_id = '$chatId'");
$data[3] = $data[3] + 1;
}
}
if ($data[2]+$data[3] == 12) { // terminate the program after three quistions
$keyboard = array(array("/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '' , `solT` = '0' , `solF` = '0', `grade` = '$data[2]' WHERE chat_id = '$chatId'");
mysqli_query($conn,"UPDATE `repeared` SET `q1` = '' , `q2` = '' , `q3` = '' , `q4` = '' , `q5` = '' , `q6` = '' , `q7` = '' , `q8` = '' , `q9` = '' , `q10` = '' , `q11` = '' , `q12` = '' WHERE chat_id = '$chatId'");
sendMessage($chatId,"الاجابات الصحيحة = ".$data[2]."\nعدد الاجابات الخاطئة = ".$data[3],$reply);
}
else // asking anther quistion
{
$results = mysqli_query($conn,"SELECT * FROM `fqhe` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results); // select random quistion from table quistions
$results_q = mysqli_query($conn,"SELECT * FROM `repeared` WHERE `chat_id` = '$chatId'");
$data_q = mysqli_fetch_row($results_q); // select all quistions that have been asked
while (in_array($data[1], $data_q)) //checking if the quistion is repeated
{
$results = mysqli_query($conn,"SELECT * FROM `fqhe` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results);// then get anther quistion not repeated befor
}
mysqli_query($conn,"UPDATE `repeared` SET `q".$sum."` = '$data[1]' WHERE chat_id = '$chatId'");
/* print button to choice an answer */
$keyboard = array(array($data[2],$data[3],$data[4]),array($data[5],"[exit]","/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '$data[6]' WHERE chat_id = '$chatId'");
sendMessage($chatId,$data[1],$reply);
}
break;
/*---------*/
/*---------*/
/*---------*/
case '[ألغاز]':
$result = mysqli_query($conn,"select * from `chat` where `chat_id` = '$chatId'");
$data = mysqli_fetch_row($result);
$sum1=$data[2]; $sum2=$data[3];
$sum = $sum1 + $sum2 + 1;
if ($sol != false) // checking if there is a solution
{
if ($sol == $data[1]) { // checking if the solution is true
mysqli_query($conn,"UPDATE `chat` SET `solT` = `solT`+'1' WHERE chat_id = '$chatId'");
$data[2] = $data[2] + 1;
}
else// if the solution is false
{
mysqli_query($conn,"UPDATE `chat` SET `solF` = `solF`+'1' WHERE chat_id = '$chatId'");
$data[3] = $data[3] + 1;
}
}
if ($data[2]+$data[3] == 12) { // terminate the program after three quistions
$keyboard = array(array("/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '' , `solT` = '0' , `solF` = '0', `grade` = '$data[2]' WHERE chat_id = '$chatId'");
mysqli_query($conn,"UPDATE `repeared` SET `q1` = '' , `q2` = '' , `q3` = '' , `q4` = '' , `q5` = '' , `q6` = '' , `q7` = '' , `q8` = '' , `q9` = '' , `q10` = '' , `q11` = '' , `q12` = '' WHERE chat_id = '$chatId'");
sendMessage($chatId,"الاجابات الصحيحة = ".$data[2]."\nعدد الاجابات الخاطئة = ".$data[3],$reply);
}
else // asking anther quistion
{
$results = mysqli_query($conn,"SELECT * FROM `puzzle` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results); // select random quistion from table quistions
$results_q = mysqli_query($conn,"SELECT * FROM `repeared` WHERE `chat_id` = '$chatId'");
$data_q = mysqli_fetch_row($results_q); // select all quistions that have been asked
while (in_array($data[1], $data_q)) //checking if the quistion is repeated
{
$results = mysqli_query($conn,"SELECT * FROM `puzzle` ORDER BY RAND() LIMIT 1");
$data = mysqli_fetch_row($results);// then get anther quistion not repeated befor
}
mysqli_query($conn,"UPDATE `repeared` SET `q".$sum."` = '$data[1]' WHERE chat_id = '$chatId'");
/* print button to choice an answer */
$keyboard = array(array($data[2],$data[3],$data[4]),array($data[5],"[exit]","/start"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
mysqli_query($conn,"UPDATE `chat` SET `solans` = '$data[6]' WHERE chat_id = '$chatId'");
sendMessage($chatId,$data[1],$reply);
}
break;
}
mysqli_close($conn);
function sendMessage ($chatId,$message,$reply)
{
// $url = $GLOBALS[website]."/sendMessage?chat_id=".$chatId."&text=".urlencode($message);
$url = $GLOBALS[website]."/sendmessage?chat_id=".$chatId."&text=".urlencode($message)."&reply_markup=".$reply;
file_get_contents($url);
}
function sendPhoto ($chatId)
{
// $url = $GLOBALS[website]."/sendMessage?chat_id=".$chatId."&text=".urlencode($message);
$url = $GLOBALS[website]."/sendPhoto?chat_id=".$chatId."&photo=".file_get_contents('https://performer.azurewebsites.net/mohamed.jpg');
file_get_contents($url);
}
// 296664810
?>