-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.php
300 lines (226 loc) · 8.14 KB
/
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
<?php
//https://geogram.tcrouzet.com/telegram.php
//exit("not running");
$startTime = microtime(true);
$logBuffer = [];
define("DEBUG",false);
ini_set('log_errors', 'On');
ini_set('error_log', __DIR__ . '/logs/error_php.log');
require_once(__DIR__ . '/admin/secret.php');
require_once(__DIR__ . '/admin/filemanager.php');
require_once(__DIR__ . '/admin/functions.php');
require_once(__DIR__ . '/admin/functions_robot.php');
require_once(__DIR__ . '/admin/callback.php');
$content = file_get_contents("php://input");
$update = json_decode($content, true);
lecho($update);
if (!isset($update["update_id"]) || $update["update_id"] <= 0) {
lexit("Inavid update_id");
}
set_time_limit(60);
ini_set('display_errors', 1);
include (__DIR__ . '/vendor/autoload.php');
$telegram = new Telegram(TELEGRAM_BOT_TOKEN);
init();
$fileManager = new FileManager();
prepare_queries();
lmicrotime();
//CALLBACK
if( isset($update["callback_query"])){
callback_manager($update["callback_query"]);
}
//NEW CHAT
if( isset($update["my_chat_member"])){
ChatMemberUpdate($update["my_chat_member"]);
}
if( isset($update["message"])){
$message = $update["message"];
}elseif( isset($update["edited_message"])) {
$message = $update["edited_message"];
}
$message_id = $message["message_id"];
//Timestamp
if( isset($message["edit_date"]) ){
$timestamp = $message["edit_date"];
}else{
$timestamp = $message["date"];
}
//chatid
$brut_chatid = $message["chat"]["id"];
$chatid = round($brut_chatid);
//lecho("chatid",$chatid);
$chat_title = get_chatitle($message);
if ( empty($chat_title) ){
lexit("empty chat_title");
}
//TEST
if( isset($message["group_chat_created"])){
lexit("NewGroupCreated");
}
//MIGGRATE
if( isset($message["migrate_to_chat_id"])){
NewChatID($message);
}
$chat_obj = get_chat($chatid);
if(empty($chat_obj)){
ShortLivedMessage($brut_chatid,"Geogram don't know this group: $chatid. I'm creating a new one.");
if (insert_chat($chatid, $chat_title)){
$chat_obj = get_chat($chatid);
}else{
ShortLivedMessage($brut_chatid,"Can't create a new one, contact Geogram admin.");
lexit("No $chatid");
}
}
//lecho($chat_obj);
//userid
$brut_userid=get_userid($message["from"]);
$userid = round($brut_userid);
//MENU
if( isset($message["text"]) && $message["text"] == "/menu"){
if($chat_obj["menuid"]){
$response = $telegram->deleteMessage(['chat_id' => $brut_chatid, 'message_id' => $chat_obj["menuid"]]);
}
$response = $telegram->deleteMessage(['chat_id' => $brut_chatid, 'message_id' => $message_id]);
$menuid = sendMenu($chat_obj);
update_menuid($brut_chatid,$menuid);
lexit("Menu");
}
//STOPED
if($chat_obj['stop']>0 && $chat_obj['stop']<time()){
ShortLivedMessage($brut_chatid,"This group was stopped for Geogram. Your message will not be processed. Ask admin to restart group.");
lexit("stoped");
}
//LOCATION
if(isset($message["location"])){
lecho("Location find ",$userid);
$username = get_username($message["from"]);
if(empty($username)){
$telegram->deleteMessage(array('chat_id' => $chatid,'message_id' => $message_id));
lexit("no username");
}
//No more than one location/10minutes except for the admin and test purpose
if(!(is_admin($chat_obj, $userid) && $chat_obj['real_time']==0)){
$query = "SELECT EXISTS(SELECT 1 FROM logs WHERE userid = ? AND timestamp > (UNIX_TIMESTAMP() - 600))";
$stmt_lastuser = $mysqli->prepare($query);
$stmt_lastuser->bind_param("i", $userid);
$stmt_lastuser->execute();
$result = $stmt_lastuser->get_result()->fetch_row();
if ($result && $result[0]) {
//Posted location during last 10 minutes
if($chat_obj['real_time']==0){
$telegram->deleteMessage(array('chat_id' => $chatid,'message_id' => $message_id));
ShortLivedMessage($brut_chatid,"$username, you are too fast. Next localisation will be possible in 10 minutes.");
}
lexit("Too fast");
}
}
$latitude = $message["location"]["latitude"];
$longitude = $message["location"]["longitude"];
//nerest_point_on_route
$r = nearest_point($latitude,$longitude,$chatid,$userid);
if($r){
$gpx_point = $r["point"];
$km = round($r["km"]);
$dev = round($r["dev"]);
}else{
$gpx_point = -1;
$km = 0;
$dev = 0;
}
//Save
lmicrotime("savelogSart");
$stmt_insertlog->bind_param('iisiddidd', $chatid, $userid, $username, $timestamp, $latitude, $longitude, $gpx_point, $km, $dev);
$stmt_insertlog->execute();
lmicrotime("savelogEnd");
if(isset($message["location"]["live_period"]) && $chat_obj['real_time']==1){
//Real time
}else{
//Normal
todelete($chat_obj,$message_id,2);
ShortLivedMessage($brut_chatid,"$username, your are on the map!");
}
lexit("End location");
}
//REPLY
if( isset($message["reply_to_message"])){
if(is_admin($chat_obj, $userid)){
ReplyManager($chatid, $message_id, $message, REPLY_RENAME);
ReplyManager($chatid, $message_id, $message, REPLY_TIMEDIFF);
ReplyManager($chatid, $message_id, $message, REPLY_DESCRIPTION);
ReplyManager($chatid, $message_id, $message, REPLY_SHARE);
ReplyManager($chatid, $message_id, $message, REPLY_FUTURE);
lexit("Admin Reply unknown");
}
}
//TEXT
if( isset($message["text"])){
//user comment
$query = "UPDATE logs SET comment = JSON_SET(comment, '$.T".$timestamp."', '".$message["text"]."') WHERE chatid = $chatid AND userid = $userid AND timestamp = (SELECT MAX(timestamp) FROM logs WHERE chatid = $chatid AND userid = $userid)";
$result = $mysqli->query($query);
if(!$result){
lecho("Error sql 2 - no log for the user\n");
}
todelete($chat_obj,$message_id);
lexit("text");
}
//PHOTO
if( isset($message["photo"])){
$max_index = max(array_keys($message["photo"]));
$file_id = $message["photo"][$max_index]["file_id"];
$file = $telegram->getFile($file_id);
if($file["ok"]){
$photo = $fileManager->chatimg($chatid,$userid,$timestamp);
lecho($photo);
$telegram->downloadFile($file['result']['file_path'], $photo['full_path']);
if(file_exists($photo['full_path'])){
lecho("Photo OK ".$photo['full_path']);
$query = "UPDATE logs SET comment = JSON_SET(comment, '$.P".$photo['pname']."', '".$photo['relative']."') WHERE chatid = $chatid AND userid = $userid AND timestamp = (SELECT MAX(timestamp) FROM logs WHERE chatid = $chatid AND userid = $userid)";
//dump($query);
$result = $mysqli->query($query);
if(!$result){
lexit("Error sql 3");
}
}
}
todelete($chat_obj,$message_id);
lexit("Photo");
//End photo
}
//CHAT PROFILE
if(isset($message["new_chat_photo"]) && is_admin($chat_obj, $userid)){
lecho("NewChatPhoto");
$max_index = max(array_keys($message["new_chat_photo"]));
$file_id = $message["new_chat_photo"][$max_index]["file_id"];
$file = $telegram->getFile($file_id);
if($file["ok"]){
$photo = $fileManager->chatphoto($chatid);
$telegram->downloadFile($file['result']['file_path'], $photo);
if(file_exists($photo)){
lecho("Chat Photo OK ".$photo);
set_photo($chatid);
}
}else{
ShortLivedMessage($brut_chatid,"Can't download chat profile - error:201");
}
todelete($chat_obj,$message_id);
lexit("ChatPhoto");
}
//FILE
if(isset($message["document"]) && is_admin($chat_obj, $userid)){
if (substr($message["document"]["file_name"], -4) === ".gpx") {
//GPX OK
lecho("New GPX");
$file = $telegram->getFile($message["document"]["file_id"]);
if($file["ok"]){
virtual_finish();
ShortLivedMessage($brut_chatid,"Your GPX is processing.");
$source = $fileManager->chatgpx_source($chatid);
$telegram->downloadFile($file['result']['file_path'], $source);
gpx2base($source);
}
}
todelete($chat_obj,$message_id);
lexit("GPX");
}
lexit("Unknown");
?>