Skip to content

Commit e99d81f

Browse files
committed
🔥 Добавил обработку сообщений
1 parent 6b3d61e commit e99d81f

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/Messages.php

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ public function __construct(User $user) {
88
$this->user = $user;
99
}
1010

11+
public function listen($listen) {
12+
if (empty($this->user->token)) die('Не указан токен!');
13+
$this->user->VkApiRequest()->getLongPollServer();
14+
15+
while ($data = $this->user->VkApiRequest()->getRequest()) {
16+
if (!isset($data["ts"])) {
17+
echo ("\nTIMESTAMP не получен...\n");
18+
continue;
19+
}
20+
21+
$updates = $data['updates'];
22+
if (count($updates) == 0) continue;
23+
24+
foreach ($updates as $key => $updates) {
25+
if (isset($updates[0])) {
26+
27+
@list($id, $idontknow1, $idontknow2, $peer_id, $timestamp, $text_message) = $updates;
28+
if ($id == 4) { // Добавление нового сообщения.
29+
$from = isset($updates[6]['from']) ?:
30+
$listen($peer_id, $text_message, $updates);
31+
}
32+
}
33+
}
34+
}
35+
}
36+
1137
public function sendMessage(string $text, $peer_ids, array $args = []) {
1238
return $this->user->VkApiRequest()->api('messages.send', [
1339
'random_id' => rand(),

src/VkApiRequest.php

+42
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ class VkApiRequest {
44

55
private $user;
66

7+
public $key;
8+
public $ts;
9+
public $server;
10+
11+
public $vkdata;
12+
13+
const PEER_ID = 2000000000;
14+
715
public function __construct(User $user) {
816
$this->user = $user;
917
}
@@ -12,6 +20,40 @@ public function setToken(string $token): void {
1220
$this->user->token = $token;
1321
}
1422

23+
public function getLongPollServer(): array {
24+
$data = $this->api("messages.getLongPollServer", ['lp_version' => 3]);
25+
echo ("\nСсылка лонгпулла обновлена\n");
26+
return list($this->key, $this->server, $this->ts) = [$data['key'], $data['server'], $data['ts']];
27+
}
28+
29+
public function getRequest(): array {
30+
$result = $this->getData();
31+
if (isset($result["failed"])) {
32+
if ($result["failed"] == 1) {
33+
unset($this->ts);
34+
$this->ts = $result["ts"];
35+
} else {
36+
$this->getLongPollServer();
37+
$result = $this->getData();
38+
}
39+
}
40+
41+
$this->ts = $result["ts"];
42+
return $result;
43+
}
44+
45+
public function getData(): array {
46+
$data = json_decode($this->curl_post('https://' . $this->server . '?' . http_build_query([
47+
'act' => 'a_check',
48+
'key' => $this->key,
49+
'ts' => $this->ts,
50+
'wait' => 25,
51+
'mode' => 2,
52+
'version' => 3
53+
])), 1);
54+
return $data;
55+
}
56+
1557
public function call(string $url) {
1658
$sendRequest = json_decode(
1759
(function_exists('curl_init')) ? self::curl_post($url) : file_get_contents($url),

tests/UserBot.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php require_once "../autoload.php";
2+
3+
/**
4+
* Суть скрипта:
5+
* => Обычный бот для страницы, с возможностью ответа
6+
*/
7+
8+
$user = new User("токен");
9+
10+
$user->getMessages()->listen(function ($peer_id, $text, $data) use ($user) {
11+
12+
if ($text == 'hello')
13+
$user->getMessages()->sendMessage('world :3', $peer_id);
14+
15+
});

0 commit comments

Comments
 (0)