diff --git a/composer.json b/composer.json index dcbdbc2..6a10e9e 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,7 @@ "psr/log": "1.*", "monolog/monolog": ">=1.6", "jasny/error-handler": "^0.2.0", - "vlucas/phpdotenv": "5.*", - "guzzlehttp/guzzle": "^7.0" + "vlucas/phpdotenv": "5.*" }, "require-dev": { "phpdocumentor/phpdocumentor": ">=2.9", diff --git a/system/API/LongPoll.php b/system/API/LongPoll.php index 6505121..024e171 100644 --- a/system/API/LongPoll.php +++ b/system/API/LongPoll.php @@ -6,24 +6,74 @@ namespace VK\API; class LongPoll extends LongPollAbstract { + /** + * Объект взаимодействия лонгпола + * + * @var object + */ + private $robot; + + /** + * Тип объекта: пользователь или группа + * + * @var string + */ + private $auth_type; + + /** + * Ключ сессии + * + * @var string + */ + private $key; + + /** + * @var int + */ + private $user_id; + + /** + * @var int + */ + private $group_id; + + /** + * @var int + */ + private $ts; + + /** + * @var string + */ + private $server; + public function __construct(object $robot, array $params = []) { - return; - echo get_class($robot), PHP_EOL; - die; + $this->robot = $robot; if ($_ENV['ROBOT_TYPE']) { - $this->vk->auth_type = 'user'; - $this->user_id = $data['id']; + $this->robot->auth_type = 'user'; + $this->user_id = $this->robot->request('users.get', [])['id']; } else { - $this->vk->auth_type = 'group'; - $this->group_id = $this->vk->request('groups.getById', [])[0]['id']; - $this->vk->request('groups.setLongPollSettings', [ + $this->robot->auth_type = 'group'; + $this->group_id = $this->robot->request('groups.getById', [])['id']; + $this->robot->request('groups.setLongPollSettings', [ 'group_id' => $this->group_id, 'enabled' => 1, - 'api_version' => $this->vk->version, + 'api_version' => $this->robot->version, 'message_new' => 1, ]); } $this->getLongPollServer(); } + + public function getLongPollServer() + { + if ($this->robot->auth_type == 'user') { + $data = $this->robot->request('messages.getLongPollServer', ['need_pts' => 1, 'lp_version' => 3]); + } else { + $data = $this->robot->request('groups.getLongPollServer', ['group_id' => $this->group_id]); + } + unset($this->key, $this->server, $this->ts); + list($this->key, $this->server, $this->ts) = [$data['key'], $data['server'], $data['ts']]; + } } diff --git a/system/API/LongPollAbstract.php b/system/API/LongPollAbstract.php index 3ff4578..1bc7b71 100644 --- a/system/API/LongPollAbstract.php +++ b/system/API/LongPollAbstract.php @@ -7,4 +7,5 @@ namespace VK\API; abstract class LongPollAbstract { abstract public function __construct(object $robot, array $params = []); -} \ No newline at end of file + abstract public function getRobotInfo(); +} diff --git a/system/Robots/User.php b/system/Robots/User.php index e89d369..e3dfb7d 100644 --- a/system/Robots/User.php +++ b/system/Robots/User.php @@ -34,14 +34,10 @@ class User extends RobotAbstract */ public function auth(string $token, float $version) { - if ($token instanceof auth) { + if ($token instanceof auth) { $this->auth = $token; $this->version = $version; $this->token = $this->auth->getAccessToken(); - } else if (isset($also_version)) { - $this->auth = new Auth($token, $version); - $this->token = $this->auth->getAccessToken(); - $this->version = $also_version; } else { $this->token = $token; $this->version = $version;