Skip to content

Commit

Permalink
🔥 Добавил обработку сообщений
Browse files Browse the repository at this point in the history
  • Loading branch information
FunnyRain committed Apr 13, 2021
1 parent 6b3d61e commit e99d81f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ public function __construct(User $user) {
$this->user = $user;
}

public function listen($listen) {
if (empty($this->user->token)) die('Не указан токен!');
$this->user->VkApiRequest()->getLongPollServer();

while ($data = $this->user->VkApiRequest()->getRequest()) {
if (!isset($data["ts"])) {
echo ("\nTIMESTAMP не получен...\n");
continue;
}

$updates = $data['updates'];
if (count($updates) == 0) continue;

foreach ($updates as $key => $updates) {
if (isset($updates[0])) {

@list($id, $idontknow1, $idontknow2, $peer_id, $timestamp, $text_message) = $updates;
if ($id == 4) { // Добавление нового сообщения.
$from = isset($updates[6]['from']) ?:
$listen($peer_id, $text_message, $updates);
}
}
}
}
}

public function sendMessage(string $text, $peer_ids, array $args = []) {
return $this->user->VkApiRequest()->api('messages.send', [
'random_id' => rand(),
Expand Down
42 changes: 42 additions & 0 deletions src/VkApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ class VkApiRequest {

private $user;

public $key;
public $ts;
public $server;

public $vkdata;

const PEER_ID = 2000000000;

public function __construct(User $user) {
$this->user = $user;
}
Expand All @@ -12,6 +20,40 @@ public function setToken(string $token): void {
$this->user->token = $token;
}

public function getLongPollServer(): array {
$data = $this->api("messages.getLongPollServer", ['lp_version' => 3]);
echo ("\nСсылка лонгпулла обновлена\n");
return list($this->key, $this->server, $this->ts) = [$data['key'], $data['server'], $data['ts']];
}

public function getRequest(): array {
$result = $this->getData();
if (isset($result["failed"])) {
if ($result["failed"] == 1) {
unset($this->ts);
$this->ts = $result["ts"];
} else {
$this->getLongPollServer();
$result = $this->getData();
}
}

$this->ts = $result["ts"];
return $result;
}

public function getData(): array {
$data = json_decode($this->curl_post('https://' . $this->server . '?' . http_build_query([
'act' => 'a_check',
'key' => $this->key,
'ts' => $this->ts,
'wait' => 25,
'mode' => 2,
'version' => 3
])), 1);
return $data;
}

public function call(string $url) {
$sendRequest = json_decode(
(function_exists('curl_init')) ? self::curl_post($url) : file_get_contents($url),
Expand Down
15 changes: 15 additions & 0 deletions tests/UserBot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php require_once "../autoload.php";

/**
* Суть скрипта:
* => Обычный бот для страницы, с возможностью ответа
*/

$user = new User("токен");

$user->getMessages()->listen(function ($peer_id, $text, $data) use ($user) {

if ($text == 'hello')
$user->getMessages()->sendMessage('world :3', $peer_id);

});

0 comments on commit e99d81f

Please sign in to comment.