-
Notifications
You must be signed in to change notification settings - Fork 3
/
BotEngine.php
151 lines (131 loc) · 4.61 KB
/
BotEngine.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
<?php
require_once "BotEngine.php";
# Ruta del JSON
define("JSON", "respuestas.json");
# Define si se coloca modo debug
define("DEBUG", false);
# Definir métodos dependiendo el tipo de mensaje
define("METHOD", array(
"text" => "sendMessage",
"photo" => "sendPhoto",
"sticker" => "sendSticker",
"document" => "sendDocument",
"voice" => "sendVoice",
"audio" => "sendAudio",
"video" => "sendVideo"
));
class BotEngine
{
private $httpService;
function __construct($httpService)
{
$this->httpService = $httpService;
}
public function run(int $update_id) : int
{
$results = $this->get_pending_messages($update_id);
foreach ($results as $result) {
$update_id = $result->update_id;
$this->process_message($result);
}
return $update_id;
}
private function get_pending_messages(int $update_id) : array
{
$str = $this->httpService->send("getUpdates", array("offset"=>($update_id + 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
{
if(!isset($result->message)) {
return;
}
if (isset($result->message->entities)) { # Si se recibe un comando
$this->process_command($result);
} elseif (isset($result->message->new_chat_participant)) { # Si entra un nuevo miembro
$this->send_new_member_reply($result);
} else { # Los otros casos
$this->send_respuestas_reply($result);
}
$this->send_files($result);
}
private function process_command($result) : void
{
$message = $result->message;
$length = $message->entities[0]->length;
$command = substr($message->text, 0, $length);
if ($message->entities[0]->type == "bot_command") {
switch ($command) {
case '/start':
case '/help':
break;
case '/link':
}
}
}
private function send_new_member_reply(stdClass $result) : void
{
$str = file_get_contents(JSON);
$obj = json_decode($str, true);
$response = array_rand($obj['ingreso'], 1);
$key = $obj['ingreso'][$response];
$this->send_reply($result, $key, $response);
}
private function send_respuestas_reply(stdClass $result) : void
{
$text = isset($result->message->text) ? $result->message->text : "";
$str = file_get_contents(JSON);
$obj = json_decode($str, true);
foreach ($obj['respuestas'] as $k => $v) {
$re = '/'.$k.'/i';
preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0);
if (count($matches) == 0) {
continue;
}
$response = array_rand($obj['respuestas'][$k], 1);
$key = $obj['respuestas'][$k][$response];
$this->send_reply($result, $key, $response);
}
}
private function send_files(stdClass $result) : void
{
# Envía archivos al privado para que este develva el file_id que se
# puede usar en el json
if ($result->message->chat->type != 'private') {
return;
}
$this->send_file($result, "sticker");
$this->send_file($result, "document");
$this->send_file($result, "audio");
$this->send_file($result, "video");
if (isset($result->message->photo)) {
$key = count($result->message->photo) - 1;
$text = '"'.$result->message->photo[$key]->file_id.'":"photo"';
$this->send_reply($result, "text", $text);
}
}
private function send_file(stdClass $result, string $file_type) : void
{
if (isset($result->message->$file_type)) {
$text = '"'.$result->message->$file_type->file_id.'":"'.$file_type.'"';
$this->send_reply($result, "text", $text);
}
}
private function send_reply(stdClass $result, string $key, string $value) : void
{
$params = array(
"chat_id" => $result->message->chat->id,
$key => $value,
"reply_to_message_id" => $result->message->message_id
);
$response = $this->httpService->send(METHOD[$key], $params);
if (DEBUG) print_r(json_decode($response));
}
}