80 lines
1.8 KiB
PHP
80 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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 = [])
|
|
{
|
|
$this->robot = $robot;
|
|
if ($_ENV['ROBOT_TYPE']) {
|
|
$this->robot->auth_type = 'user';
|
|
$this->user_id = $this->robot->request('users.get', [])['id'];
|
|
} else {
|
|
$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->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']];
|
|
}
|
|
}
|