Repository navigation
Expand file tree
/
Copy pathhook.php
More file actions
338 lines (302 loc) · 15.5 KB
/
Copy pathhook.php
File metadata and controls
338 lines (302 loc) · 15.5 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
//ini_set("log_errors", 1);
//ini_set("error_log", "./tg.log");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
$config = require __DIR__ . '/config.php';
use Longman\TelegramBot\Request;
use Exception;
use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use PDO;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
$pdo = new PDO('mysql:host=' . $config['mysql']['host'] . ';dbname=' . $config['mysql']['database'], $config['mysql']['user'], $config['mysql']['password']);
const IDLE = 0;
const MENU = 1;
const REGISTRATION = 2;
const REG_STEP1 = 3;
const REG_STEP2 = 4;
const REG_STEP3 = 5;
const REG_SUCCESS = 6;
const REGISTERED = 7;
$file = '';
$user_id = 0;
$user_realname = '';
$user_email = '';
$user_phone = '';
class User {
public $pdo;
public $state = 0;
public $user_id = 0;
public $telegram_id = 0;
public $telegram_name;
public $first_name;
public $email;
public $phone;
public function __construct() {
echo "User created";
}
public function set_state($new_state) {
$this->state = $new_state;
switch ($this->state) {
}
}
public function greeting($chat_Id) {
Request::sendMessage(['chat_id' => $chat_Id, 'text' => "Добро пожаловать в Bot Studio!\nПредлагаем вашему вниманию коллекцию telegram-ботов на все случаи жизни.\nДля просмотра каталога ботов нажмите /catalog"]);
if (if_user_exist()) {
Request::sendMessage(['chat_id' => $chat_Id, 'text' => "User already exists in the database."]);
} else {
Request::sendMessage(['chat_id' => $chat_Id, 'text' => "User not found in the database."]);
}
}
public function registration($chat_Id) {
global $file;
global $m_Text;
global $user_realname;
global $user_email;
global $user_phone;
//Request::sendMessage(['chat_id' => $chat_Id, 'text' => "Приступаем к регистрации: ". $this->state]);
switch ($this->state) {
case REGISTRATION:
$this->state = REG_STEP1;
if (isset($file)) {
file_put_contents($file, $this->state);
}
Request::sendMessage(['chat_id' => $chat_Id, 'text' => "Для начала регистрации введите своё имя:"]);
break;
case REG_STEP1:
$user_realname = $m_Text;
$this->state = REG_STEP2;
if (isset($file)) {
file_put_contents($file, $this->state);
}
Request::sendMessage(['chat_id' => $chat_Id, 'text' => "Приятно познакомиться, " . $user_realname . "! Теперь введите ваш адрес электронной почты:"]);
break;
case REG_STEP2:
$user_email = $m_Text;
$this->state = REG_STEP3;
if (isset($file)) {
file_put_contents($file, $this->state);
}
Request::sendMessage(['chat_id' => $chat_Id, 'text' => "Ваш адрес электронной почты: " . $user_email . "! Для окончания регистрации введите ваш телефонный номер:"]);
break;
case REG_STEP3:
$user_phone = $m_Text;
$this->state = REG_SUCCESS;
if (isset($file)) {
file_put_contents($file, $this->state);
}
Request::sendMessage(['chat_id' => $chat_Id, 'text' => "Ваш номер телефона: " . $user_phone . "! Спасибо за регистрацию, теперь вам доступен Личный кабинет."]);
break;
}
}
//
public function if_user_exist() {
global $pdo;
// Check if user exists in the database
$stmt = $pdo->prepare('SELECT COUNT(*) FROM users WHERE telegram_id = :m_From_Id');
$stmt->bindParam(':m_From_Id', $this->telegram_id);
$stmt->execute();
return($stmt->fetchColumn() > 0);
}
}
$User = new User();
try {
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
echo "Telegram client created";
Longman\TelegramBot\TelegramLog::initErrorLog(__DIR__ . "/{$config['bot_username']}_error.log");
Longman\TelegramBot\TelegramLog::initDebugLog(__DIR__ . "/{$config['bot_username']}_debug.log");
Longman\TelegramBot\TelegramLog::initUpdateLog(__DIR__ . "/{$config['bot_username']}_update.log");
$telegram->enableLimiter();
$post = json_decode(Request::getInput(), true);
if (isset($post)) {
$update = new Update($post, $config['bot_username']);
if (isset($update)) {
$message = $update->getMessage();
if (isset($message)) {
$m_Id = $message->getMessageId();
$m_ThreadId = $message->getMessageThreadId();
$m_From = $message->getFrom();
if (isset($m_From)) {
$m_From_Id = $m_From->getId();
if (isset($m_From_Id)) {
$User->telegram_id = $m_From_Id;
$file = 'users/' . $User->telegram_id . '.txt';
$file_content = file_get_contents($file);
if (isset($file_content)) {
$User->state = $file_content;
}
}
$m_From_IsBot = $m_From->getIsBot();
$m_From_FirstName = $m_From->getFirstName();
$m_From_LastName = $m_From->getLastName();
$m_From_Username = $m_From->getUsername();
$m_From_LanguageCode = $m_From->getLanguageCode();
$m_From_IsPremium = $m_From->getIsPremium();
$m_From_AddedToAttachmentMenu = $m_From->getAddedToAttachmentMenu();
$m_From_CanJoinGroups = $m_From->getCanJoinGroups();
$m_From_CanReadAllGroupMessages = $m_From->getCanReadAllGroupMessages();
$m_From_SupportsInlineQueries = $m_From->getSupportsInlineQueries();
}
$m_SenderChat = $message->getSenderChat();
$m_Date = $message->getDate();
$m_Chat = $message->getChat();
if (isset($m_Chat)) {
$m_c_Id = $m_Chat->getId();
if (isset($m_c_Id)) {
Request::sendMessage(['chat_id' => $m_c_Id, 'text' => json_encode($update, JSON_PRETTY_PRINT)]);
}
}
$m_Text = $message->getText();
if (isset($m_Chat) && isset($m_Text)) {
$User->registration();
}
$m_ForwardFrom = $message->getForwardFrom();
$m_ForwardFromChat = $message->getForwardFromChat();
$m_ForwardFromMessageId = $message->getForwardFromMessageId();
$m_ForwardSignature = $message->getForwardSignature();
$m_ForwardSenderName = $message->getForwardSenderName();
$m_ForwardDate = $message->getForwardDate();
$m_IsTopicMessage = $message->getIsTopicMessage();
$m_IsAutomaticForward = $message->getIsAutomaticForward();
$m_ReplyToMessage = $message->getReplyToMessage();
$m_ViaBot = $message->getViaBot();
$m_EditDate = $message->getEditDate();
$m_HasProtectedContent = $message->getHasProtectedContent();
$m_MediaGroupId = $message->getMediaGroupId();
$m_AuthorSignature = $message->getAuthorSignature();
$m_Entities = $message->getEntities();
if (isset($m_Entities)) {
foreach ($m_Entities as $m_Entity) {
// string
// Type of the entity
$m_e_Type = $m_Entity->getType();
if (isset($m_e_Type)) {
switch ($m_e_Type) {
case "mention":
// @username
break;
case "hashtag":
// #hashtag
break;
case "cashtag":
// $USD
break;
case "bot_command":
// /start@jobs_bot
break;
case "url":
// https://telegram.org
break;
case "email":
// do-not-reply@telegram.org
break;
case "phone_number":
// +1-212-555-0123
break;
case "bold":
// bold text
break;
case "italic":
// italic text
break;
case "underline":
// underlined text
break;
case "strikethrough":
// strikethrough text
break;
case "spoiler":
// spoiler message
break;
case "code":
// monowidth string
break;
case "pre":
// monowidth block
break;
case "text_link":
// for clickable text URLs
break;
case "text_mention":
// for users without usernames
break;
case "custom_emoji":
// for inline custom emoji stickers
break;
}
}
// int
// Offset in UTF-16 code units to the start of the entity
$m_e_Offset = $m_Entity->getOffset();
// int
// Length of the entity in UTF-16 code units
$m_e_Length = $m_Entity->getLength();
// string, optional
// For "text_link" only, url that will be opened after user taps on the text
$m_e_Url = $m_Entity->getUrl();
// User, optional
// for "text_mention" only, the mentioned user
$m_e_User = $m_Entity->getUser();
// string, optional
// for "pre" only, the programming language of the entity text
$m_e_Language = $m_Entity->getLanguage();
// string, optional
// for “custom_emoji” only, unique identifier of the custom emoji
// use getCustomEmojiStickers to get full information about the sticker
$m_e_CustomEmojiId = $m_Entity->getCustomEmojiId();
if (isset($m_Text) && isset($m_e_Type) && isset($m_e_Offset) && isset($m_e_Length)) {
$m_e_Text = mb_substr($m_Text, $m_e_Offset, $m_e_Length, 'UTF-8');
if (isset($m_c_Id) && isset($m_e_Text)) {
switch ($m_e_Type) {
case "bot_command":
switch ($m_e_Text) {
case "/start":
$User->greeting($m_c_Id);
break;
case "/menu":
Request::sendMessage(['chat_id' => $m_c_Id, 'text' => "Выберите интересующий вас пункт меню:\n- Регистрация /registration\n- Каталог ботов /catalog\n- Инструкция /help"]);
break;
case "/registration":
$User->state = REGISTRATION;
$User->registration($m_c_Id);
break;
case "/help":
Request::sendMessage(['chat_id' => $m_c_Id, 'text' => "Инструкция."]);
break;
case "/catalog":
Request::sendMessage(['chat_id' => $m_c_Id, 'text' => "Каталог ботов Bot Studio.\nВыберите интересующую категорию:\n- Персональные боты /personal_bots\n- Боты для бизнеса /business_bots\n- Боты для чатов и каналов /channel_bots\n\nДля возврата в меню нажмите /menu\nДля получения инструкции нажмите /help"]);
break;
default:
Request::sendMessage(['chat_id' => $m_c_Id, 'text' => "Команда " . $m_e_Text . " не существует."]);
break;
}
break;
}
}
}
}
}
$m_CaptionEntities = $message->getCaptionEntities();
}
}
}
return Request::emptyResponse();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Silence is golden!
echo $e;
// Log telegram errors
Longman\TelegramBot\TelegramLog::error($e);
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Silence is golden!
// Uncomment this to catch log initialisation errors
echo $e;
}