-
Notifications
You must be signed in to change notification settings - Fork 16
/
parse.php
154 lines (139 loc) · 5.8 KB
/
parse.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
<?php
$json = file_get_contents('php://input');
if (!$json) {
telegram_log('Warning','Webhook URL','Endpoint was called without input.');
return;
}
$data = (array)json_decode($json, TRUE);
if ( $data['message']['message_id'] && ( $data['message']['message_id'] == get_option('wp_telegram_last_id') ) ) {
telegram_log('EXCEPTION', 'MESSAGE_ID', json_encode($json, TRUE));
die();
}
update_option('wp_telegram_last_id', $data['message']['message_id'] );
if ( $data['message']['chat']['type'] == 'private' ) {
$USERID = $data['message']['from']['id'];
$CPT = 'telegram_subscribers';
$PRIVATE = true; $GROUP = false;
$COMMAND = $data['message']['text'];
} else if ( $data['message']['chat']['type'] == 'group' || $data['message']['chat']['type'] == 'supergroup' ) {
$USERID = $data['message']['chat']['id'];
$CPT = 'telegram_groups';
$GROUP = true; $PRIVATE = false;
$COMMAND = $data['message']['text'];
} else if ( $data['my_chat_member']['chat']['type'] == 'supergroup' ) {
$USERID = $data['my_chat_member']['chat']['id'];
$CPT = 'telegram_groups';
$GROUP = true; $PRIVATE = false;
$COMMAND = '';
} else if ( $data['callback_query']['message']['text'] ) {
$USERID = $data['callback_query']['message']['chat']['id'];
$CPT = 'telegram_subscribers';
$PRIVATE = true; $GROUP = false;
$COMMAND = $data['callback_query']['data'];
} else {
telegram_log('EXCEPTION', 'CHAT TYPE', json_encode($json, TRUE));
die();
}
telegram_log('>>>>', $USERID, $COMMAND);
if (defined('WP_DEBUG') && true === WP_DEBUG) {
telegram_log('####', 'DEBUG', json_encode($json, TRUE));
}
if ( !telegram_getid( $USERID ) ) {
$p = wp_insert_post(array(
'post_title' => $USERID,
'post_content' => '',
'post_type' => $CPT,
'post_status' => 'publish',
'post_author' => 1,
));
if ( $PRIVATE ) {
update_post_meta($p, 'telegram_first_name', $data['message']['from']['first_name']);
update_post_meta($p, 'telegram_last_name', $data['message']['from']['last_name']);
update_post_meta($p, 'telegram_username', $data['message']['from']['username']);
telegram_sendmessage( $USERID, telegram_option('wmuser') );
} else if ( $GROUP ) {
$name = '';
if ( isset( $data['message']['chat']['title'] ) ) {
$name = $data['message']['chat']['title'];
} else if ( isset( $data['my_chat_member']['chat']['title'] ) ) {
$name = $data['my_chat_member']['chat']['title'];
}
update_post_meta($p, 'telegram_name', $name );
telegram_log('', '', 'Bot added to <strong>'.$data['message']['chat']['title'].'</strong>');
telegram_sendmessage( $USERID, telegram_option('wmgroup') );
}
return;
} else if ($PRIVATE) {
$POST_ID = telegram_getid( $USERID );
$fieldsToCheck = [ 'first_name', 'last_name', 'username' ];
foreach( $fieldsToCheck as $field ) {
if ( get_post_meta( $POST_ID, 'telegram_'.$field, true ) != $data['message']['from'][$field] ) {
update_post_meta( $POST_ID, 'telegram_'.$field, $data['message']['from'][$field]);
}
}
$counter = get_post_meta($POST_ID, 'telegram_counter', true);
$counter = $counter ? ++$counter : 1;
update_post_meta( $POST_ID, 'telegram_counter', $counter );
} else if ($GROUP) {
update_post_meta($o->ID, 'telegram_name', 'xx');
}
if ( isset( $data['message']['location'] ) ) {
$page = get_page_by_title( 'telegram-location', '', 'telegram_commands' );
update_post_meta( telegram_getid( $USERID ), 'telegram_last_latitude', $data['message']['location']['latitude']);
update_post_meta( telegram_getid( $USERID ), 'telegram_last_longitude', $data['message']['location']['longitude']);
telegram_sendmessage( $USERID, $page->ID );
do_action( 'telegram_parse_location', $USERID, $data['message']['location']['latitude'], $data['message']['location']['longitude']);
return;
} else if ( isset( $data['message']['photo'] ) ) {
do_action( 'telegram_parse_photo', $USERID, $data['message']['photo'] );
return;
} else if ( isset( $data['message']['document'] ) ) {
do_action( 'telegram_parse_document', $USERID, $data['message']['document'] );
return;
}
do_action( 'telegram_parse', $USERID, $COMMAND ); //EXPERIMENTAL
$ok_found = false;
if ( $COMMAND != '' ) {
query_posts('post_type=telegram_commands&posts_per_page=-1');
while (have_posts()):
the_post();
$lowertitle = strtolower( get_the_title() );
$lowermessage = strtolower( $COMMAND );
if (
( $lowertitle == $lowermessage )
||
( strpos( $lowermessage, $lowertitle.' ' ) === 0 )
||
( in_array( $lowermessage, explode(",", $lowertitle ) ) )
) {
$ok_found = true;
if ( has_post_thumbnail( get_the_id() ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium', true );
telegram_sendphoto( $USERID, get_the_id(), $image[0] );
}
else {
telegram_sendmessage( $USERID, get_the_id() );
}
}
endwhile;
}
if ( $PRIVATE ) {
switch ($data['message']['text']) {
case '/stop':
$ok_found = true;
telegram_sendmessage( $USERID, telegram_option('bmuser') );
wp_delete_post( telegram_getid( $USERID ) );
break;
default:
break;
return;
}
}
if ( $GROUP && $data['message']['left_chat_participant']['id'] == current( explode(':', telegram_option('token') ) ) ) {
wp_delete_post( telegram_getid( $USERID ) );
telegram_log('', '', 'Bot removed from <strong>'.$data['message']['chat']['title'].'</strong>');
}
if ( $PRIVATE && !$ok_found ) {
telegram_sendmessage( $USERID, telegram_option('emuser') );
}
?>