This repository has been archived by the owner on Dec 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.php
206 lines (185 loc) · 5.09 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
<?php
require('config.php');
require('functions.php');
# if is /start will require totp.php
if (!isset($_GET['Token'])) {
exit;
} elseif (sha1($_GET['Token'] == YSITD_Token)) { # YSITD Send Message
$chatID = $_GET['user_id'];
$options = Array(
'text' => $_GET['text'],
);
$tmp = sendMsg($options);
var_dump($tmp['result']['chat']);
exit;
} elseif(sha1($_GET['Token']) != Telegram_Token) { # Is from Telegram or not.
exit;
}
$json = file_get_contents('php://input') . PHP_EOL;
$datas = json_decode($json, True);
$data = $datas['message'];
$userID = $data['from']['id'];
$chatID = $data['chat']['id'];
sendAction();
if ($chatID == YSITD_ChatID) {
if (isset($datas['new_chat_participant'])) {
sendInGroup($datas['new_chat_participant']['id'], True);
}
exit;
}
/* Start of Check User */
if (substr($chatID, 0, 1) == '-' && !in_array($chatID, $allow_chans)) {
sendMsg(Array(
'chat_id' => $chatID,
'text' => '這個 bot 不是給群組用的喔!',
), True);
}
if (substr($chatID, 0, 1) == '-') {
exit;
}
if (!isset($data['from']['username'])) {
$options = Array(
'text' => '麻煩設定一下 Username 喔!' . "\n" . '設定完後麻煩請用 /start [code] 驗證' ."\n" . 'ex. "/start 426892"' . "\n" . '(code 可於攤位取得)',
);
sendMsg($options, True);
exit;
}
if (in_array($userID, $Admin_list)) {
if (isset($data['forward_from'])) {
$name = 'User Info: ' . "\n";
$name .= 'UID: ' . $data['forward_from']['id'] . "\n";
if (isset($data['forward_from']['username'])) {
$name .= 'username: @' . $data['forward_from']['username'] . "\n";
}
$name .= 'First name: ' . $data['forward_from']['first_name'] . "\n";
if (isset($data['forward_from']['last_name'])) {
$name .= 'Last name: ' . $data['forward_from']['last_name'] . "\n";
}
$options = Array(
'text' => $name,
);
sendMsg($options, True);
exit;
}
} else {
if (isset($data['forward_from'])) {
$options = Array(
'text' => 'Forward?!',
);
sendMsg($options, True);
exit;
}
}
/* End of Check User */
sendLog();
/* Start of Command */
if (substr($data['text'], 0, 1) == '/') {
$cmd = substr($data['text'], 1);
$cmd = explode(' ', $cmd, 2);
$arg = $cmd[1];
$cmd = $cmd[0];
$cmd = strtolower($cmd);
switch ($cmd) {
case 'start':
if (!preg_match('/^[0-9]{6}$/', $arg)) {
$options = Array(
'text' => '您尚未驗證喔!' . "\n" . '請用 /start [code] 驗證' ."\n" . 'ex. "/start 426892"' . "\n" . '(code 可於攤位取得)',
);
sendMsg($options, True);
exit;
}
require('totp.php');
$ga = new PHPGangsta_GoogleAuthenticator();
$checkResult = $ga->verifyCode(TOTP_Secret, $arg, 10); // 2 = 2*30sec clock tolerance
if (!$checkResult) {
$options = Array(
'text' => '驗證碼錯誤!' . "\n" .'請用 /start [code] 驗證' ."\n" . 'ex. "/start 426892"' . "\n" . '(code 可於攤位取得)',
);
sendMsg($options, True);
exit;
}
$field = Array(
'token' => API_TOKEN,
'user_id' => $userID,
'username' => $data['from']['username'],
'first_name' => $data['from']['first_name'],
'last_name' => ((isset($data['from']['last_name'])) ? $data['from']['last_name'] : ''),
);
$url = API_URL . 'post_answer';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, True);
curl_setopt($curl, CURLOPT_POSTFIELDS, $field);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
curl_exec($curl);
curl_close($curl);
$options = Array(
'parse_mode' => 'Markdown',
'text' => '歡迎加入 YSITD!' . "\n\n" . '[Telegram 群組](https://telegram.me/joinchat/' . group_joinlink . ')' . "\n" . '參與闖關活動請點擊 /question',
);
sendMsg($options, True);
break;
case 'help':
$text = '點擊 /question 開始作答' . "\n";
$text .= '輸入 /me 查看當前已作答題數、答對題數' . "\n";
$text .= '顯示 YSITD 群組連結請點 /link' . "\n";
$text .= 'Tips: 答題越多抽中機率越高喔!' . "\n";
$options = Array(
'text' => $text,
);
sendMsg($options, True);
break;
case 'question':
newQust();
break;
case 'me':
showUserStatus($userID);
break;
case 'link':
$options = Array(
'parse_mode' => 'Markdown',
'text' => 'YSITD Telegram Group Link: [Click Me](https://telegram.me/joinchat/' . group_joinlink . ')',
);
sendMsg($options, True);
break;
case 'id':
$options = Array(
'text' => $userID,
);
sendMsg($options, True);
break;
case 'join':
sendInGroup($userID, True);
break;
case 'cancel':
$options = Array(
'hide_keyboard' => True,
);
$options = json_encode($options);
$options = Array(
'reply_markup' => $options,
'text' => 'Canceled.',
);
sendMsg($options, True);
break;
default:
$options = Array(
'text' => '無此指令',
);
sendMsg($options, True);
}
exit;
}
/* End of Command */
if (preg_match('/^[1-4]\. .+/', $data['text'])) {
$ans = substr($data['text'], 0, 1);
sendAns($ans);
newQust();
exit;
}
if (preg_match('/\'.+(--|\/\*)/', $data['text'])) {
$options = Array(
'text' => 'Are you try to hack me?',
);
sendMsg($options, True);
}