-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
237 lines (183 loc) · 6.7 KB
/
bot.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
<?php
error_reporting(1);
require('telegram.php');
require('functions.php');
require('db.php');
$bot_username = 'thepolite_bot';
$server_link = 'https://myserver.com';
/***********************
CONSTANTES
***********************/
define("TXT_ENTER_GROUP", "🇺🇸 Be polite\n\n🇧🇷Seja educado\n\nI need admin access to do some magic");
define("TXT_START_PRIVATE", "🇺🇸 Welcome to the Polite bot. Add me in groups as administrator and I will delete, edit and resend unpolite messages. If I am not an admin, I will not be able to delete messages\n\n🇧🇷Bem vindo ao Polite Bot. Me adicione em grupos como administrador e eu irei deletar, editar e reenviar mensagens mal educadas. Se eu não for admin, não poderei deletar as mensagens");
define("TXT_RANKING", "🇧🇷 Veja o Ranking\n\n🇺🇸 See the ranking");
define("TXT_HELP", "Ajuda");
/***********************
RECEBIM. DE JSON
***********************/
$json = file_get_contents("php://input");
if(strstr($json,'_"message"')){
$json = str_replace('_"message"','"message"',$json);
}
if(strstr($json,'_"inline_query"')){
$json = str_replace('_"inline_query"','"inline_query"',$json);
}
$dados = json_decode($json,true);
//debug(MY_ID, API, $json);
//Chat ID
$chat_id = $dados['message']['chat']['id'];
/***********************
EVENTOS
***********************/
//Entrada em Grupo
if($dados['message']['new_chat_participant']){
if($dados['message']['new_chat_participant']['username'] == $bot_username){
sendMessage($chat_id,TXT_ENTER_GROUP);
$group_id = $dados['message']['chat']['id'];
$group_name = $dados['message']['chat']['title'];
db_add_new_group($group_id, $group_name);
}
}
//Saída do grupo
if($dados['message']['left_chat_participant']){
if($dados['message']['left_chat_participant']['username'] == $bot_username){
$group_id = $dados['message']['chat']['id'];
db_leave_group($group_id);
}
}
/***********************
ORIGEM DA REQUISIÇÃO
PV | GROUP | INLINE
***********************/
//Private
if($dados['message']['chat']['type'] == 'private'){
$nome_do_user = $dados['message']['from']['first_name'];
$username = $dados['message']['from']['username'];
$first_name = $dados['message']['from']['first_name'];
$last_name = $dados['message']['from']['last_name'];
if(!$last_name){
$last_name = "-";
}
if(!$username){
$username = "-";
}
$language_code = $dados['message']['from']['language_code'];
db_new_private_user($chat_id, $first_name, $last_name, $username, $language_code);
//$timestamp = $dados['message']['date'];
//date_default_timezone_set('America/Sao_Paulo');
//$data = date("d-m-Y\ H:i:s", $timestamp);
$termo = $dados['message']['text'];
switch($termo){
case '/start':
sendChatAction($chat_id, 'typing');
sendMessage($chat_id,TXT_START_PRIVATE,$id_reply = "",$reply_markup = "");
break;
case '/ranking':
sendChatAction($chat_id, 'typing');
$inline_keyboard =
[
[
['text' => '📊 Ranking', 'url' => $server_link . '/ranking/']
],
];
$reply_markup = json_encode(['inline_keyboard' => $inline_keyboard]);
sendMessage($chat_id,TXT_RANKING,$id_reply = "",$reply_markup);
break;
default:
//Security Check command = param1,param2
if($chat_id == MY_ID){
//Get command
$command_and_args = explode('=',$termo);
$command = trim($command_and_args[0]);
$all_args = trim($command_and_args[1]);
//Args
$args = explode(',',$all_args);
if($command == "broadcast"){
$msg = trim($args[0]);
$type = trim($args[1]);
broadcast($msg, $type);
}
}
break;
}
}
//Group
else if($dados['message']['chat']['type'] == 'group' || $dados['message']['chat']['type'] == 'supergroup'){
$group_name = $dados['message']['chat']['title'];
$group_username = $dados['message']['chat']['username'];
$user_id = $dados['message']['from']['id'];
$first_name = $dados['message']['from']['first_name'];
$last_name = $dados['message']['from']['last_name'];
$username = $dados['message']['from']['username'];
if(!$last_name){
$last_name = "";
}
if(!$group_username){
$group_username = "-";
}
$name_of_user = $first_name.' '.$last_name;
if(!$username){
$identifier = $name_of_user;
}
else{
$identifier = '@'.$username;
}
$termo = $dados['message']['text'];
//Command or other term
switch($termo){
case '/ranking@'.$bot_username:
sendChatAction($chat_id, 'typing');
$inline_keyboard =
[
[
['text' => '📊 Ranking', 'url' => $server_link . '/ranking/']
],
];
$reply_markup = json_encode(['inline_keyboard' => $inline_keyboard]);
sendMessage($chat_id,TXT_RANKING,$id_reply = "",$reply_markup);
break;
default:
$id_msg = $dados['message']['message_id'];
$id_to_reply = $dados['message']['reply_to_message']['message_id'];
if(!$id_to_reply){
$id_to_reply = '';
}
$polite = search_for_unpolite_terms($termo);
if($termo != $polite){//$polite will be modified version of $term, if there is any unpolite terms
//Delete the message
deleteMessage($chat_id, $id_msg);
//Send unpolite alert message
$message = "*Polite version of " .$identifier. " message :\n\n💬*"."_".$polite."_\n\n❌ *Oh no! Polite points of this group was decreased*";
$inline_keyboard =
[
[
['text' => '📊 Ranking', 'url' => $server_link . '/ranking/']
],
];
$reply_markup = json_encode(['inline_keyboard' => $inline_keyboard]);
sendMessage($chat_id,$message,'',$reply_markup);
increment_unpolite_occurrency($chat_id);
}
//Was not unpolite, but let's check if was polite, and not 'neutral'
else{
//Search for polite terms
$find_polites = search_for_polite_terms($termo);
if($find_polites){
//Send polite alert message
$message = "*✅ Nice! Polite points of this group was increased*";
$inline_keyboard =
[
[
['text' => '📊 Ranking', 'url' => $server_link . '/ranking/']
],
];
$reply_markup = json_encode(['inline_keyboard' => $inline_keyboard]);
sendMessage($chat_id,$message,$id_msg,$reply_markup);
increment_polite_occurrency($chat_id);
}
}
break;
}
}
die();
?>