-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtelegram-bot-custom.php
276 lines (228 loc) · 10.6 KB
/
telegram-bot-custom.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
<?php
/*
Plugin Name: Telegram Bot & Channel (Custom)
Description: My Custom Telegram Plugin
Author: My name
Version: 1
*/
add_action('telegram_parse','telegramcustom_parse', 10, 2);
function telegramcustom_parse( $telegram_user_id, $text ) {
$plugin_post_id = telegram_getid( $telegram_user_id );
if ( !$plugin_post_id ) {
return;
}
if ( $text == 'Invia' || $text == 'invia' ) {
$desc = get_post_meta( $plugin_post_id, 'telegram_custom_description', true );
$lat = get_post_meta( $plugin_post_id, 'telegram_last_latitude', true );
$long = get_post_meta( $plugin_post_id, 'telegram_last_longitude', true );
if ( !$lat > 0 || !$long > 0 ) {
telegram_sendmessage( $telegram_user_id, 'Tramite la 📎 invia per prima cosa la tua posizione');
return;
}
if ( !$desc ) {
telegram_sendmessage( $telegram_user_id, 'Errore. Tramite la 📎 invia per prima cosa la tua posizione');
}
get_post_meta($plugin_post_id, 'telegram_username', true) ? $t_user = get_post_meta($plugin_post_id, 'telegram_username', true) : $t_user = '';
$arr = array(
'lat' => $lat,
'lon' => $long,
'telegram_id' => $telegram_user_id,
'telegram_username' => $t_user,
'wordpress_id' => $plugin_post_id,
'description' => $desc,
'stato' => 1
);
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'segnalazioni',
$arr,
array( '%s', '%s', '%d', '%s', '%d', '%s' )
);
delete_post_meta( $plugin_post_id, 'telegram_custom_description' );
delete_post_meta( $plugin_post_id, 'telegram_custom_state' );
delete_post_meta( $plugin_post_id, 'telegram_last_latitude' );
delete_post_meta( $plugin_post_id, 'telegram_last_longitude' );
telegram_create_github_issue( $arr );
telegram_sendmessage( $telegram_user_id, 'Segnalazione registrata. Grazie');
telegramcustom_send_check();
} else if ( $text == 'info' || $text == '/help' ) {
telegram_sendmessage( $telegram_user_id, 'Bot creato per http://terremotocentroitalia.info/'.PHP_EOL.'Per info e suggerimenti: @Milmor');
delete_post_meta( $plugin_post_id, 'telegram_custom_description' );
delete_post_meta( $plugin_post_id, 'telegram_custom_state' );
delete_post_meta( $plugin_post_id, 'telegram_last_latitude' );
delete_post_meta( $plugin_post_id, 'telegram_last_longitude' );
} else if ( $text == 'mappa' ) {
telegram_sendmessage( $telegram_user_id, 'http://goo.gl/24YcqW');
delete_post_meta( $plugin_post_id, 'telegram_custom_description' );
delete_post_meta( $plugin_post_id, 'telegram_custom_state' );
delete_post_meta( $plugin_post_id, 'telegram_last_latitude' );
delete_post_meta( $plugin_post_id, 'telegram_last_longitude' );
} else if ( $text == '/stato' || $text == 'stato' ) {
global $wpdb;
$conteggio = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}segnalazioni WHERE telegram_id=".$telegram_user_id.";" );
telegram_sendmessage( $telegram_user_id, 'Hai inviato '.$conteggio.' segnalazioni.'.PHP_EOL.'http://goo.gl/zdByBS');
} else if ( $text == '/segnala' || $text == 'segnala') {
telegram_sendmessage( $telegram_user_id, 'Tramite la 📎 invia per prima cosa la tua posizione');
delete_post_meta( $plugin_post_id, 'telegram_custom_description' );
delete_post_meta( $plugin_post_id, 'telegram_custom_state' );
delete_post_meta( $plugin_post_id, 'telegram_last_latitude' );
delete_post_meta( $plugin_post_id, 'telegram_last_longitude' );
} else if ( get_post_meta( $plugin_post_id, 'telegram_custom_state', true ) == 'description_wait' && $text != '') {
$lat = get_post_meta( $plugin_post_id, 'telegram_last_latitude', true );
$long = get_post_meta( $plugin_post_id, 'telegram_last_longitude', true );
if ( !$lat > 0 || !$long > 0 ) {
telegram_sendmessage( $telegram_user_id, 'Tramite la 📎 invia per prima cosa la tua posizione');
delete_post_meta( $plugin_post_id, 'telegram_custom_description' );
delete_post_meta( $plugin_post_id, 'telegram_custom_state' );
delete_post_meta( $plugin_post_id, 'telegram_last_latitude' );
delete_post_meta( $plugin_post_id, 'telegram_last_longitude' );
return;
}
update_post_meta( $plugin_post_id, 'telegram_custom_description', sanitize_text_field($text) );
telegram_sendmessage( $telegram_user_id, 'Inviami una foto o digita "invia" per inoltrare una segnalazione di solo testo');
}
return;
}
function telegram_parse_photo_s ( $telegram_user_id, $photo ) {
$plugin_post_id = telegram_getid( $telegram_user_id );
if ( !get_post_meta( $plugin_post_id, 'telegram_custom_description', true ) ) {
telegram_sendmessage( $telegram_user_id, 'Errore. Inviare prima una descrizione.');
die();
}
$lat = get_post_meta( $plugin_post_id, 'telegram_last_latitude', true );
$long = get_post_meta( $plugin_post_id, 'telegram_last_longitude', true );
if ( !$lat > 0 || !$long > 0 ) {
telegram_sendmessage( $telegram_user_id, 'Tramite la 📎 invia per prima cosa la tua posizione');
return;
}
$url = telegram_download_file( $telegram_user_id, $photo[2]['file_id'] );
if ( $url ) {
$desc = get_post_meta( $plugin_post_id, 'telegram_custom_description', true );
get_post_meta($plugin_post_id, 'telegram_username', true) ? $t_user = get_post_meta($plugin_post_id, 'telegram_username', true) : $t_user = '';
global $wpdb;
$arr = array(
'lat' => $lat,
'lon' => $long,
'telegram_id' => $telegram_user_id,
'telegram_username' => $t_user,
'wordpress_id' => $plugin_post_id,
'description' => $desc,
'url' => $url,
'stato' => 1
);
$wpdb->insert(
$wpdb->prefix . 'segnalazioni',
$arr,
array( '%s', '%s', '%d', '%s', '%d', '%s', '%s' )
);
telegram_sendmessage( $telegram_user_id, 'Foto registrata. Grazie');
delete_post_meta( $plugin_post_id, 'telegram_custom_description' );
delete_post_meta( $plugin_post_id, 'telegram_custom_state' );
delete_post_meta( $plugin_post_id, 'telegram_last_latitude' );
delete_post_meta( $plugin_post_id, 'telegram_last_longitude' );
telegram_create_github_issue( $arr );
telegramcustom_send_check();
}
} add_action('telegram_parse_photo','telegram_parse_photo_s', 10, 2);
add_action('telegram_parse_location','telegramcustom_c_parse_location', 10, 3);
function telegramcustom_c_parse_location ( $telegram_user_id, $lat, $long ) {
$plugin_post_id = telegram_getid( $telegram_user_id );
if ( !$plugin_post_id ) {
return;
}
if ( get_post_meta( $plugin_post_id, 'telegram_custom_state', true ) == 'position_wait' ) {
$int = telegram_location_haversine_distance ( 42.7, 13.24, $lat, $long, $earthRadius = 6371);
telegram_sendmessage( $telegram_user_id, 'Posizione ricevuta ('.$int.' km dall\'epicentro)');
telegram_sendmessage( $telegram_user_id, 'Inviami una descrizione');
update_post_meta( $plugin_post_id, 'telegram_custom_state', 'description_wait' );
} else {
$int = telegram_location_haversine_distance ( 42.7, 13.24, $lat, $long, $earthRadius = 6371);
telegram_sendmessage( $telegram_user_id, 'Posizione ricevuta ('.$int.' km dall\'epicentro)');
telegram_sendmessage( $telegram_user_id, 'Inviami una descrizione');
update_post_meta( $plugin_post_id, 'telegram_custom_state', 'description_wait' );
}
}
function telegramcustom_send_check() {
telegram_sendmessage( '63480147', '*ADMIN*'.PHP_EOL.'nuova issue: http://goo.gl/zdByBS');
telegram_sendmessage( '61359892', '*ADMIN*'.PHP_EOL.'nuova issue: http://goo.gl/zdByBS');
telegram_sendmessage( '166538229', '*ADMIN*'.PHP_EOL.'nuova issue: http://goo.gl/zdByBS');
}
function telegram_create_github_issue( $arr ) {
$title = $arr['description'];
if ( $arr['url'] ) {
$body .= '!['.$arr['description'].']('.$arr['url'].')'.PHP_EOL;
}
$body .= '{ "lat": '.$arr["lat"].', "lon": '.$arr["lon"].', "wordpress_id": '.$arr["wordpress_id"].', "telegram_username": '.$arr["telegram_username"].' }';
$body .= PHP_EOL.'[Link Mappa](http://www.piersoft.it/terremotocentro/#20/'.$arr['lat'].'/'.$arr['lon'].')';
$data = array(
'title' => $title,
'body' => $body,
'labels' => array( 'Telegram' )
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n"."Authorization: Basic ".base64_encode( "milesimarco:<TOKEN>" )."\r\n",
'content' => json_encode( $data )
)
);
$context = stream_context_create( $options );
ini_set("user_agent","Opera/9.80 (Windows NT 6.1; U; Edition Campaign 21; en-GB) Presto/2.7.62 Version/11.00");
$result = file_get_contents( 'https://api.github.com/repos/emergenzeHack/terremotocentro_segnalazioni/issues', false, $context);
$array_response = (array)json_decode($result, TRUE);
$url_issue = $array_response['html_url'];
global $wpdb;
$wpdb->update(
$wpdb->prefix . 'segnalazioni',
array(
'github_issue' => $url_issue
),
array(
'id' => $arr[1]
),
array( '%s' ),
array( '%d')
);
telegram_sendmessage( $telegram_user_id, 'issue creata'.PHP_EOL.'http://goo.gl/zdByBS');
}
function telegramcustom_csv_pull() {
$where = '';
if ( isset( $_GET[ 'mese' ] ) && absint( $_GET[ 'mese' ] ) ) {
$where = ' AND MONTH(time)='.$_GET[ 'mese' ];
}
if ( isset( $_GET[ 'giorno' ] ) ) {
if ( absint( $_GET[ 'giorno' ] ) ) {
$where .= ' AND DAY(time)='.$_GET[ 'giorno' ];
} else if ( $_GET['giorno'] == 'oggi') {
$where = ' AND DAY(time)='.date("d");
}
}
global $wpdb;
$file = 'segnalazioni';
$results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}segnalazioni WHERE stato=1".$where.";",ARRAY_A);
if (empty($results)) {
print '"id","lat","lon","telegram_id","wordpress_id","description","url","time","stato"';
die();
}
$csv_output = '"'.implode('","',array_keys($results[0])).'"';
foreach ($results as $row) {
$csv_output .= "\r\n".'"'.implode('","',$row).'"';
}
if ( $_GET[ 'get-csv' ] == 'true' ) {
$filename = $file."_".date("Y-m-d_H-i",time());
header( "Access-Control-Allow-Origin: *");
header( "Content-type: text/csv; charset=utf-8");
header( "Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
}
print $csv_output;
die();
}
function telegram_c_plugin_parse_request() {
global $wp_query;
if ( isset( $_GET[ 'get-csv' ] ) ) {
telegramcustom_csv_pull();
}
}
add_action('template_redirect', 'telegram_c_plugin_parse_request');
?>