forked from mohamad-amin/MohandesplusBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getUpdateCLI.php
132 lines (120 loc) · 4.26 KB
/
getUpdateCLI.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
<?php
/**
* Created by PhpStorm.
* User: Mohamad Amin
* Date: 3/26/2016
* Time: 12:58 AM
*/
require __DIR__ . '/vendor/autoload.php';
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
ignore_user_abort(true);//if caller closes the connection (if initiating with cURL from another PHP, this allows you to end the calling PHP script without ending this one)
set_time_limit(0);
$hLock=fopen(__FILE__.".lock", "w+");
if(!flock($hLock, LOCK_EX | LOCK_NB))
die("Already running. Exiting...");
$API_KEY = 'Genius :D';
$BOT_NAME = 'MohandesplusBot';
$mysql_credentials = [
'host' => '',
'user' => '',
'password' => '',
'database' => '',
];
$telegram = null;
try {
// Create Telegram API object
$telegram = new Telegram($API_KEY, $BOT_NAME);
// Enable MySQL
$telegram->enableMySQL($mysql_credentials);
// Handle telegram getUpdate request
$telegram->addCommandsPath('commands');
$telegram->setLogRequests(true);
$telegram->setLogPath($BOT_NAME . '.log');
$telegram->setLogVerbosity(3);
$telegram->setDownloadPath('images');
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// log telegram errors
echo $e;
}
function checkQueueDatabase() {
$database = new medoo([
'database_type' => 'mysql',
'database_name' => 'mohandesplusbot',
'server' => 'localhost',
'username' => 'root',
'password' => 'MohandesPlus',
'charset' => 'utf8mb4'
]);
$datas = $database->select("queue", "*");
foreach($datas as $data) {
$time = $data['Time'];
if (time() > $time) {
$tData = [];
$tData['chat_id'] = $data['Channel'];
if ($data['MessageId'] != 0) {
$tData['message_id'] = $data['MessageId'];
$tData['text'] = "";
Request::editMessageText($tData);
} else {
switch ($data['Type']) {
case 1:
$tData['text'] = $data['Text'];
$result = Request::sendMessage($tData);
break;
case 2:
if (strlen($data['Text']) > 200) {
$tData['parse_mode'] = 'Markdown';
$tData['text'] = $data['Text'].
'[ ]('.$data['Photo'].')';
Request::sendMessage($tData);
} else {
$tData['photo'] = $data['Photo'];
$tData['caption'] = $data['Text'];
Request::sendPhoto($tData);
}
break;
case 3:
$tData['video'] = $data['Video'];
$tData['caption'] = $data['Text'];
$result = Request::sendVideo($tData);
break;
case 4:
$tData['from_chat_id'] = $data['ChatId'];
$tData['message_id'] = $data['Text'];
$result = Request::forwardMessage($tData);
break;
case 5:
$tData['document'] = $data['Photo'];
$tData['caption'] = $data['Text'];
$result = Request::sendDocument($tData);
break;
}
}
if ($data['EditTime'] == 0) {
$database->delete("queue", [
"AND" => [
"Time" => $time
]
]);
} else {
$tData = [];
$tData['chat_id'] = $data['ChatId'];
$tData['text'] = var_export($result, true);
Request::sendMessage($tData);
}
}
}
}
try {
while (true) {
$telegram->handleGetUpdates();
checkQueueDatabase();
usleep(2*1000*100);
}
} catch (\Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
flock($hLock, LOCK_UN);
fclose($hLock);
unlink(__FILE__.".lock");