Обновил лонгполл, мелкие исправления

This commit is contained in:
Paradise 2020-09-08 18:55:21 +03:00
parent 9d38d69445
commit b3571bcecf
4 changed files with 63 additions and 17 deletions

View File

@ -27,8 +27,7 @@
"psr/log": "1.*", "psr/log": "1.*",
"monolog/monolog": ">=1.6", "monolog/monolog": ">=1.6",
"jasny/error-handler": "^0.2.0", "jasny/error-handler": "^0.2.0",
"vlucas/phpdotenv": "5.*", "vlucas/phpdotenv": "5.*"
"guzzlehttp/guzzle": "^7.0"
}, },
"require-dev": { "require-dev": {
"phpdocumentor/phpdocumentor": ">=2.9", "phpdocumentor/phpdocumentor": ">=2.9",

View File

@ -6,24 +6,74 @@ namespace VK\API;
class LongPoll extends LongPollAbstract 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 = []) public function __construct(object $robot, array $params = [])
{ {
return; $this->robot = $robot;
echo get_class($robot), PHP_EOL;
die;
if ($_ENV['ROBOT_TYPE']) { if ($_ENV['ROBOT_TYPE']) {
$this->vk->auth_type = 'user'; $this->robot->auth_type = 'user';
$this->user_id = $data['id']; $this->user_id = $this->robot->request('users.get', [])['id'];
} else { } else {
$this->vk->auth_type = 'group'; $this->robot->auth_type = 'group';
$this->group_id = $this->vk->request('groups.getById', [])[0]['id']; $this->group_id = $this->robot->request('groups.getById', [])['id'];
$this->vk->request('groups.setLongPollSettings', [ $this->robot->request('groups.setLongPollSettings', [
'group_id' => $this->group_id, 'group_id' => $this->group_id,
'enabled' => 1, 'enabled' => 1,
'api_version' => $this->vk->version, 'api_version' => $this->robot->version,
'message_new' => 1, 'message_new' => 1,
]); ]);
} }
$this->getLongPollServer(); $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']];
}
} }

View File

@ -7,4 +7,5 @@ namespace VK\API;
abstract class LongPollAbstract abstract class LongPollAbstract
{ {
abstract public function __construct(object $robot, array $params = []); abstract public function __construct(object $robot, array $params = []);
} abstract public function getRobotInfo();
}

View File

@ -34,14 +34,10 @@ class User extends RobotAbstract
*/ */
public function auth(string $token, float $version) public function auth(string $token, float $version)
{ {
if ($token instanceof auth) { if ($token instanceof auth) {
$this->auth = $token; $this->auth = $token;
$this->version = $version; $this->version = $version;
$this->token = $this->auth->getAccessToken(); $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 { } else {
$this->token = $token; $this->token = $token;
$this->version = $version; $this->version = $version;