-
Notifications
You must be signed in to change notification settings - Fork 3
/
BotTranquilidadEngine.php
94 lines (79 loc) · 2.72 KB
/
BotTranquilidadEngine.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
<?php
# Define si se coloca modo debug
define("DEBUG", true);
define("BOTNAME", 'crul_test_bot');
define("LA_TRANQUILIDAD", "CgADBAADSgIAApJSCFAQy7SJpIxWCgI");
define("LA_TRANQUILIDAD_TXT", "Lo que más se valora ...");
define("LA_TRANQUILIDAD_TIMEOUT", 1 * 60 * 60); # in seconds ([hours] * 60 * 60)
class BotTranquilidadEngine
{
private $httpService;
private $last_message_update_id;
function __construct($httpService)
{
$this->httpService = $httpService;
}
public function run() : void
{
$results = $this->get_last_message();
foreach ($results as $result) {
$this->process_message($result);
}
}
private function get_last_message() : array
{
$str = $this->httpService->send("getUpdates", array("offset"=> -1 ));
$json = json_decode($str);
$result_prop = "result";
if (!isset($json->$result_prop)) {
print($str);
return [];
}
if (DEBUG) if (count($json->result) > 0) print_r($json->result);
return $json->result;
}
private function process_message($result) : void
{
$time_since_last_message = $this->time_from($result->message->date);
if ($this->last_message_update_id == $result->update_id) {
if (DEBUG) print("Ignore message and sleep ".LA_TRANQUILIDAD_TIMEOUT."\n");
sleep(LA_TRANQUILIDAD_TIMEOUT);
return;
}
if (DEBUG) {
print("\n");
print("message->date = ".$result->message->date."\n");
print("time() = ".time()."\n");
print("time_since_last_msg = ".$time_since_last_message."\n");
print("\n");
}
if ($time_since_last_message < LA_TRANQUILIDAD_TIMEOUT) {
$sleep_time = (LA_TRANQUILIDAD_TIMEOUT - $time_since_last_message);
if (DEBUG) print("Sleeping ".$sleep_time."\n");
sleep($sleep_time);
} else {
$this->last_message_update_id = $result->update_id;
$is_last_messsage_la_tranquilidad = ($result->message->from->username == BOTNAME and $result->message->text == LA_TRANQUILIDAD);
if ($is_last_messsage_la_tranquilidad) {
if (DEBUG) print("Sleeping LA_TRANQUILIDAD_TIMEOUT\n");
sleep(LA_TRANQUILIDAD_TIMEOUT);
} else {
if (DEBUG) print("Sending LA_TRANQUILIDAD\n");
$this->send_message($result);
}
}
}
private function send_message(stdClass $result) : void
{
$params = array(
"chat_id" => $result->message->chat->id,
"caption" => LA_TRANQUILIDAD_TXT,
"document" => LA_TRANQUILIDAD
);
$response = $this->httpService->send("sendDocument", $params);
if (DEBUG) print_r(json_decode($response));
}
private function time_from($reference) {
return time() - $reference;
}
}