-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
253 lines (223 loc) · 11.2 KB
/
main.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
<?php
/**
* Telegram Bot Patrimoni Trasparenti Lic. MIT . Dati Lic. CC-BY-SA openPolis
* @author Francesco Piero Paolicelli @piersoft derivato da parte di codice di @emergenzaprato
*/
include("Telegram.php");
include("settings_t.php");
class mainloop{
const MAX_LENGTH = 4096;
function start($telegram,$update)
{
date_default_timezone_set('Europe/Rome');
$today = date("Y-m-d H:i:s");
$text = $update["message"] ["text"];
$chat_id = $update["message"] ["chat"]["id"];
$user_id=$update["message"]["from"]["id"];
$location=$update["message"]["location"];
$reply_to_msg=$update["message"]["reply_to_message"];
$this->shell($telegram,$text,$chat_id,$user_id,$location,$reply_to_msg);
$db = NULL;
}
//gestisce l'interfaccia utente
function shell($telegram,$text,$chat_id,$user_id,$location,$reply_to_msg)
{
date_default_timezone_set('Europe/Rome');
$today = date("Y-m-d H:i:s");
if (strpos($text,'/') !== false) {
$text=str_replace("/","",$text);
}
if ($text == "/start" || $text == "Informazioni" || $text == "start") {
$img = curl_file_create('logo.png','image/png');
$contentp = array('chat_id' => $chat_id, 'photo' => $img);
$telegram->sendPhoto($contentp);
$reply = "Benvenuto. Questo è un servizio automatico (bot da Robot) per i dati raccolti su ".NAME.". In questo bot puoi ricercare i parlamentari eletti per Cognome oppure cliccare su Filtro o Ricerca per istruzioni. In qualsiasi momento scrivendo /start ti ripeterò questo messaggio di benvenuto.Questo bot è stato realizzato da @piersoft. Il progetto e il codice sorgente sono liberamente riutilizzabili con licenza MIT. I dati utilizzati sono presenti su http://patrimoni.openpolis.it con lic. CC-BY-SA";
$content = array('chat_id' => $chat_id, 'text' => $reply,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
$log=$today. ",new_info,," .$chat_id. "\n";
file_put_contents(LOG_FILE, $log, FILE_APPEND | LOCK_EX);
$this->create_keyboard_temp($telegram,$chat_id);
exit;
}
elseif ($text == "Ricerca") {
$reply = "Scrivi il Cognome da cercare anteponendo il carattere - , ad esempio: -Renzi";
$content = array('chat_id' => $chat_id, 'text' => $reply,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
// $log=$today. ";new chat started;" .$chat_id. "\n";
$this->create_keyboard_temp($telegram,$chat_id);
exit;
}elseif($location!=null)
{
// $this->location_manager($telegram,$user_id,$chat_id,$location);
// exit;
}
elseif(strpos($text,'/') === false){
if(strpos($text,'?') !== false || strpos($text,'-') !== false || strpos($text,'i:') !== false || strpos($text,'c:') !== false){
$text=str_replace("?","",$text);
$text=str_replace("-","",$text);
$filtro="upper(C)";
$location="Sto cercando il parlamentare nel cui cognome è presente: ".$text;
if (strpos($text,'i:') !== false){
$text=str_replace("i:","",$text);
$location="Sto cercando i parlamentari eletti,ordinati per reddito dichiarato, con incarico: ".$text;
$filtro="upper(F)";
}else if (strpos($text,'c:') !== false){
$text=str_replace("c:","",$text);
$location="Sto cercando i parlamentari,ordinati per reddito dichiarato, eletti nella circoscrizione: ".$text;
$filtro="upper(I)";
}
$content = array('chat_id' => $chat_id, 'text' => $location,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
$text=str_replace(" ","%20",$text);
$text=strtoupper($text);
// $urlgd ="https://spreadsheets.google.com/tq?tqx=out:csv&tq=SELECT%20A%2CC%2CD%2CG%2CH%2CP%2CL%2CM%2CO%2CJ%2CK%20WHERE%20upper(C)%20like%20%27%25";
$urlgd ="https://spreadsheets.google.com/tq?tqx=out:csv&tq=SELECT%20%2A%20WHERE%20".$filtro."%20like%20%27%25";
$urlgd .=$text;
$urlgd .="%25%27%20AND%20N%20IS%20NOT%20NULL&key=".GDRIVEKEY."&gid=".GDRIVEGID2;
$inizio=1;
$homepage ="";
$csv = array_map('str_getcsv',file($urlgd));
$csv=str_replace(array("\r", "\n"),"",$csv);
$count = 0;
foreach($csv as $data=>$csv1){
$count = $count+1;
}
if ($count ==0){
$location="Nessun risultato trovato";
$content = array('chat_id' => $chat_id, 'text' => $location,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
}
if ($count >200){
$location="Troppe risposte per il criterio scelto. Ti preghiamo di fare una ricerca più circoscritta";
$content = array('chat_id' => $chat_id, 'text' => $location,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
exit;
}
function decode_entities($text) {
$text=htmlentities($text, ENT_COMPAT,'ISO-8859-1', true);
$text= preg_replace('/&#(\d+);/me',"chr(\\1)",$text); #decimal notation
$text= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$text); #hex notation
$text= html_entity_decode($text,ENT_COMPAT,"UTF-8"); #NOTE: UTF-8 does not work!
return $text;
}
$homepage .="\n";
// sort($csv);
//define a comparison function
function cmp($a, $b) {
if ($a[9] == $b[9]) {
return 0;
}
return ($a[9] < $b[9]) ? -1 : 1;
}
usort($csv, "cmp");
for ($i=$inizio;$i<$count;$i++){
$homepage .="\n";
if (strtoupper($csv[$i][1]) !="NOME"){
$homepage .=strtoupper($csv[$i][1])." ".strtoupper($csv[$i][2])."\n";
if($csv[$i][9] !=NULL) $homepage .="Totale 730 dichiarato: € ".$csv[$i][9]."\n";
$homepage .="Per i dettagli digita: ".$csv[$i][0]." oppure clicca: /".$csv[$i][0]."\n";
$homepage .="____________\n";
}
}
$chunks = str_split($homepage, self::MAX_LENGTH);
foreach($chunks as $chunk) {
$content = array('chat_id' => $chat_id, 'text' => $chunk,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
}
$log=$today. ",ricerca,".$text."," .$chat_id. "\n";
file_put_contents(LOG_FILE, $log, FILE_APPEND | LOCK_EX);
}else if (strpos($text,'Filtro') !== false){
// $text=str_replace("?","",$text);
$location="Puoi fare una ricerca filtrata:\nPer circoscrizione scrivi c:regione esempio c:umbria\nPer incarico scrivi i:incarico esempio i:camera (a scelta tra camera,senato,governo)";
$content = array('chat_id' => $chat_id, 'text' => $location,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
// $text=str_replace(" ","%20",$text);
// $urlgd ="https://spreadsheets.google.com/tq?tqx=out:csv&tq=SELECT%20A%2CC%2CD%2CG%2CH%2CP%2CL%2CM%2CO%2CJ%2CK%20WHERE%20N%20IS%20NOT%20NULL";
}elseif (strpos($text,'/') === false || strpos($text,'1') !== false || strpos($text,'2') !== false || strpos($text,'3') !== false || strpos($text,'4') !== false || strpos($text,'5') !== false || strpos($text,'6') !== false || strpos($text,'7') !== false || strpos($text,'8') !== false || strpos($text,'9') !== false || strpos($text,'0') !== false ){
$text=str_replace("/","",$text);
$location="Sto cercando l'ID: ".$text;
$content = array('chat_id' => $chat_id, 'text' => $location,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
// $urlgd ="https://spreadsheets.google.com/tq?tqx=out:csv&tq=SELECT%20A%2CC%2CD%2CG%2CH%2CP%2CL%2CM%2CO%2CJ%2CK%20WHERE%20A%20%3D%20";
$urlgd ="https://spreadsheets.google.com/tq?tqx=out:csv&tq=SELECT%20%2A%20WHERE%20A%20%3D%20";
$urlgd .=$text;
$urlgd .="%20&key=".GDRIVEKEY."&gid=".GDRIVEGID2;
$inizio=1;
$homepage ="";
$csv = array_map('str_getcsv',file($urlgd));
$csv=str_replace(array("\r", "\n"),"",$csv);
$count = 0;
foreach($csv as $data=>$csv1){
$count = $count+1;
}
if ($count ==0 || $count ==1){
$location="Nessun risultato trovato";
$content = array('chat_id' => $chat_id, 'text' => $location,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
}
function decode_entities($text) {
$text=htmlentities($text, ENT_COMPAT,'ISO-8859-1', true);
$text= preg_replace('/&#(\d+);/me',"chr(\\1)",$text); #decimal notation
$text= preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)",$text); #hex notation
$text= html_entity_decode($text,ENT_COMPAT,"UTF-8"); #NOTE: UTF-8 does not work!
return $text;
}
for ($i=$inizio;$i<$count;$i++){
$thumb="http://op_openparlamento_images.s3.amazonaws.com/parlamentari/thumb/".$csv[1][0].".jpeg";
$ch = curl_init($thumb);
$urlfile="log/temp".$csv[1][0].".png";
$fp = fopen($urlfile, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$img = curl_file_create($urlfile,'image/png');
$contentp = array('chat_id' => $chat_id, 'photo' => $img);
$telegram->sendPhoto($contentp);
$homepage .="\n";
if($csv[$i][1] !=NULL) $homepage .="Nome: ".$csv[$i][1]."\n";
if($csv[$i][2] !=NULL) $homepage .="Cognome: ".$csv[$i][2]."\n";
if($csv[$i][3] !=NULL) $homepage .="Data di nascita: ".$csv[$i][3]."\n";
if($csv[$i][4] !=NULL) $homepage .="Professione: ".$csv[$i][4]."\n";
if($csv[$i][5] !=NULL) $homepage .="Incarico: ".$csv[$i][5]."\n";
if($csv[$i][6] !=NULL) $homepage .="Lista di elezione/partito: ".$csv[$i][6]."\n";
if($csv[$i][7] !=NULL) $homepage .="Gruppo parlamentare: ".$csv[$i][7]."\n";
if($csv[$i][8] !=NULL) $homepage .="Circoscrizione di elezione: ".$csv[$i][8]."\n";
if($csv[$i][9] !=NULL) $homepage .="Totale 730 dichiarato: € ".$csv[$i][9]."\n";
if($csv[$i][10] !=NULL) $homepage .="Totale 730 coniuge: € ".$csv[$i][10]."\n";
if($csv[$i][11] !=NULL) $homepage .="Totale contributi: € ".$csv[$i][11]."\n";
if($csv[$i][12] !=NULL) $homepage .="Totale spese elettorali: ".$csv[$i][12]."\n";
if($csv[$i][13] !=NULL) $homepage .="Indice di completezza: ".$csv[$i][13]."\n";
if($csv[$i][14] !=NULL) $homepage .="Note completezza: ".$csv[$i][14]."\n";
if($csv[$i][15] !=NULL) $homepage .="Num. fabbricati: ".$csv[$i][15]."\n";
if($csv[$i][16] !=NULL) $homepage .="Num. terreni: ".$csv[$i][16]."\n";
if($csv[$i][17] !=NULL) $homepage .="Num. beni mobili: ".$csv[$i][17]."\n";
if($csv[$i][18] !=NULL) $homepage .="Num. di partecipazioni (azioni/quote) in società: ".$csv[$i][18]."\n";
if($csv[$i][19] !=NULL) $homepage .="Num. di incarichi di amministratore di società: ".$csv[$i][19]."\n";
$csv[$i][2]=str_replace(" ","-",$csv[$i][2]);
$csv[$i][1]=str_replace(" ","-",$csv[$i][1]);
$homepage .="Dettagli completi su:\nhttp://patrimoni.openpolis.it/#/scheda/".$csv[$i][2]."-".$csv[$i][1]."/".$csv[$i][0];
$homepage .="\n____________\n";
}
$chunks = str_split($homepage, self::MAX_LENGTH);
foreach($chunks as $chunk) {
$content = array('chat_id' => $chat_id, 'text' => $chunk,'disable_web_page_preview'=>true);
$telegram->sendMessage($content);
}
$log=$today. ",ricerca,".$text."," .$chat_id. "\n";
file_put_contents(LOG_FILE, $log, FILE_APPEND | LOCK_EX);
}
$this->create_keyboard_temp($telegram,$chat_id);
exit;
}
}
function create_keyboard_temp($telegram, $chat_id)
{
$option = array(["Filtro","Ricerca"],["Informazioni"]);
$keyb = $telegram->buildKeyBoard($option, $onetime=false);
$content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "[Digita l'ID,fai una ricerca con - oppure con c: o i: ]");
$telegram->sendMessage($content);
}
}
?>