forked from Surzhikov/TelegramSiteHelper
/
getUpdates.php
175 lines (110 loc) · 5.72 KB
/
getUpdates.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
<?
set_time_limit(0);
error_reporting(E_ALL); //Выводим все ошибки и предупреждения
set_time_limit(0); //Время выполнения скрипта не ограничено
ob_implicit_flush(); //Включаем вывод без буферизации
ignore_user_abort(true); // Игнорируем закрытие окна браузера с сервером
// Connect to database
require('TelegramDBConnect.php');
$db=connectDB();
// Create new Bot Object
require('TelegramBotClass.php');
require('TelegramBotConfig.php');
$bot = new TelegramBot($tokenAPI);
$f=fopen("running",'w');
$fl=flock($f, LOCK_EX | LOCK_NB);
if($fl){
// print("I`m the only running process...<hr>");
while(true){
// Get lastUpdate ID
$sth=$db->prepare("SELECT lastUpdate FROM lastUpdate");
$sth->execute();
$a=$sth->fetch();
if($a['lastUpdate']==null){
$updateIdFrom=0;
}else{
$updateIdFrom=intval($a['lastUpdate'])+1;
}
// Get Updates with 30 sec Long-Poll
$updates=$bot->GetUpdates($updateIdFrom,null,30)->result;
foreach($updates as $up=>$update){
$updateId = $update->update_id;
$chatId = $update->message->chat->id;
$message = $update->message->text;
// echo $updateId."<br>";
if(mb_substr($message,0,11)=="/newmanager"){
$ch=explode(" ",$message);
$password=intval($ch[1]);
if($password==$managersPassword){
$firstName=$update->message->chat->first_name;
$lastName=$update->message->chat->last_name;
// Если менеджер повторно регистрируется в системе командой /newmanager_ , сперва удалим его
$sth=$db->prepare("DELETE FROM managers WHERE chatId=:chatId");
$sth->execute(array(":chatId"=>$chatId));
// Добавляем менеджера в базу
$sth=$db->prepare("INSERT INTO managers (chatId, name) VALUES (:chatId, :name)");
$sth->execute(array(":chatId"=>$chatId, ":name"=>$firstName." ".$lastName));
// И шлем ему привет
$msgSent=$bot->SendMessage($chatId,"Здравствуйте, ".$firstName."! \r\nВ этом чате вы будете получать сообщения с сайта! Приятной работы!"); // Success!
}else{
$msgSent=$bot->SendMessage($chatId,"Неверный пароль для добавления нового менеджера!"); // Success!
}
}else if($message=="/exit"){
$sth=$db->prepare("UPDATE managers SET wsChat=:wsChat WHERE chatId=:chatId");
$sth->execute(array(":chatId"=>$chatId, ":wsChat"=>null));
$firstName=$update->message->chat->first_name;
$msgSent=$bot->SendMessage($chatId, $firstName.", Вы вышли из чата");
}else if(mb_substr($message,0,6)=="/chat_"){
//Set Manager to chat
$ch=explode("/chat_",$message);
$wsChat=intval($ch[1]);
if($wsChat!=null && $wsChat!=0){
$sth=$db->prepare("UPDATE managers SET wsChat=:wsChat WHERE chatId=:chatId");
$sth->execute(array(":chatId"=>$chatId, ":wsChat"=>$wsChat));
$firstName=$update->message->chat->first_name;
$msgSent=$bot->SendMessage($chatId, $firstName.", теперь все Ваши сообщения направляются в чат #".$wsChat);
}else{
$msgSent=$bot->SendMessage($chatId, $firstName.", неверный номер чата!"); // Success!
}
}else{
$sth=$db->prepare("SELECT count(*) as count FROM managers WHERE chatId=:chatId");
$sth->execute(array(":chatId"=>$chatId));
$a=$sth->fetch();
if($a['count']==0){
$msgSent=$bot->SendMessage($chatId, "Вы не зарегистрированны как менеджер! Введите команду \r\n/newmanager пароль\r\nдля регистрации!"); // Success!
}else{
$sth=$db->prepare("SELECT wsChat FROM managers WHERE chatId=:chatId");
$sth->execute(array(":chatId"=>$chatId));
$a=$sth->fetch();
$wsChatId=$a['wsChat'];
if($a['wsChat']==null){
$firstName=$update->message->chat->first_name;
$msgSent=$bot->SendMessage($chatId, $firstName.", прежде чем отправлять сообщения, надо выбрать, в какой чат их отправлять с помощью команды\r\n/chat_#чата\r\n"); // Success!
}else{
$sth=$db->prepare("SELECT wsChatHash FROM wsChat WHERE wsChatId=:wsChatId");
$sth->execute(array(":wsChatId"=>$wsChatId));
$a=$sth->fetch();
$wsChatHash=$a['wsChatHash'];
$dbname="chatsDBs/".$wsChatHash.".db";
// Connect to database
require_once('chatDBConnect.php');
$dbChat=chatDBConnect($dbname);
$sth=$dbChat->prepare("INSERT INTO messages (mText, mCreator, mDate) VALUES (:mText, :mCreator, :mDate)");
$sth->execute(array(":mText"=>$message, ":mCreator"=>"m", ":mDate"=>time()));
$dbChat=null;
}
}
}
}
if(isset($updateId)){
$db->exec("DELETE FROM lastUpdate");
$sth=$db->prepare("INSERT INTO lastUpdate (lastUpdate) VALUES (:lastUpdate)");
$sth->execute(array(":lastUpdate"=>$updateId));
}
}
}else{
echo"Одновременно может быть запущен только один процесс.";
exit();
}
//$q=$BotNow->Status();
//$q=$BotNow->GetUpdates()->result;