@@ -15,7 +15,7 @@ use stdClass,
/**
* Режимы отправки сообщений
*/
enum send
enum message_ send
{
/** Обычная отправка */
case simple ;
@@ -27,19 +27,16 @@ enum send
/**
* Режимы генерации идентификатора сессии доставки сообщения
*/
enum generate
enum random_id
{
/** Генерация: time() */
/** Генерация: time() (нельзя отправить более чем 1 сообщение в секунду) */
case date ;
/** Генерация: rand() */
case random ;
/** Генерация: random_bytes(10 ) */
/** Генерация: random_int(0, PHP_INT_MAX ) */
case crypto ;
/** Генерация: sodium_crypto_generichash() */
case hash ;
}
/**
@@ -58,14 +55,24 @@ enum generate
final class messages extends method
{
/**
* @var send $send_mode Режим отправки сообщений
* @var message_ send $send_mode Режим отправки сообщений
*/
protected send $send_mode = send :: simple ;
protected message_ send $send_mode = message_ send:: simple ;
/**
* @var generate $generate_mode Режим генерации идентификатора сессии доставки сообщения
* @var random_id $generate_mode Режим генерации идентификатора сессии доставки сообщения
*/
protected generate $generate_mode = generate :: date ;
protected random_id $generate_mode = random_id :: crypto ;
/**
* @var int|string|array|null $destination Получатель
*/
protected int | string | array | null $destination = null ;
/**
* @var string|null $text Текст
*/
protected string | null $text = null ;
/**
* @var ?int $lat Географическая ширина
@@ -121,60 +128,324 @@ final class messages extends method
* Конструктор
*
* @param robot $robot Робот
* @param int|string|array|null $receiver Получатель
*/
public function __construct ( protected robot $robot , mixed ... $properties )
{
// Быстрая отправка
if ( ! empty ( $properties )) $this -> send ( ... $properties );
}
/**
* Записать: режим отправки сообщений
*
* @param message_send $send_mode Режим отправки сообщений
*
* @return static
*/
public function send_mode ( message_send $send_mode ) : static
{
// Запись в свойство
$this -> __set ( 'send_mode' , $send_mode );
return $this ;
}
/**
* Записать: режим генерации идентификатора сессии доставки сообщения
*
* @param random_id $generate_mode Режим генерации идентификатора сессии доставки сообщения
*
* @return static
*/
public function generate_mode ( random_id $generate_mode ) : static
{
// Запись в свойство
$this -> __set ( 'generate_mode' , $generate_mode );
return $this ;
}
/**
* Записать: получатель
*
* @param int|string|array|null $destination Получатель
*
* @return static
*/
public function destination ( int | string | array | null $destination ) : static
{
// Запись в свойство
$this -> __set ( 'destination' , $destination );
return $this ;
}
/**
* Записать: текст
*
* @param string|null $text Текст
*/
public function __construct (
protected robot $robot ,
int | string | array | null $receiver = null ,
protected string | null $text = null
) {
if ( isset ( $this -> text , $receiver )) {
// Быстрая отправка
$this -> send ( $receiver );
}
}
/**
* Записать текст
*
* @param string $text Текст
* @return static
*/
public function text ( string $text ) : self
public function text ( string | null $text ) : static
{
// Записать текст
if ( ! isset ( $this -> text )) {
$this -> text = $text ;
} else {
$this -> text .= $text ;
}
// Запись в свойство
$this -> __set ( ' text' , $text );
return $this ;
}
/**
* Записать сообщения для пересылки
* Записать: географическая ширина
*
* @param $ids Идентификаторы сообщений
* @param ?int $lat Географическая ширина
*
* @return static
*/
public function forward ( ... $ids ) : self
public function lat ( ? int $lat ) : static
{
// Запись
$this -> forward = array_merge ( $this -> forward ? ? [] , $ids );
// Запись в свойство
$this -> __set ( 'lat' , $lat );
return $this ;
}
/**
* Записать сообщение для отве та
* Записать: географическая долго та
*
* @param $id Идентификатор сообщения
* @param ?int $long Географическая долгота
*
* @return static
*/
public function reply ( int $id ) : self
public function long ( ? int $long ) : static
{
// Запись
$this -> reply = $id ;
// Запись в свойство
$this -> __set ( 'long' , $long ) ;
return $this ;
}
/**
* Записать: идентификатор сообщения, на которое требуется ответить
*
* @param ?int $reply_to Идентификатор сообщения, на которое требуется ответить
*
* @return static
*/
public function reply_to ( ? int $reply_to ) : static
{
// Запись в свойство
$this -> __set ( 'reply_to' , $reply_to );
return $this ;
}
/**
* Записать: идентификатор сообщения, на которое требуется ответить
*
* @param ?int $reply Идентификатор сообщения, на которое требуется ответить
*
* @return static
*/
public function reply ( ? int $reply ) : static
{
// Запись в свойство
$this -> __set ( 'reply_to' , $reply );
return $this ;
}
/**
* Записать: идентификатор сообщения, на которое требуется ответить
*
* @param ?int $to Идентификатор сообщения, на которое требуется ответить
*
* @return static
*/
public function to ( ? int $to ) : static
{
// Запись в свойство
$this -> __set ( 'reply_to' , $to );
return $this ;
}
/**
* Записать: идентификаторы пересылаемых сообщений
*
* @param ?array $forward_messages Идентификаторы пересылаемых сообщений
*
* @return static
*/
public function forward_messages ( ? array $forward_messages ) : static
{
// Запись в свойство
$this -> __set ( 'forward_messages' , $forward_messages );
return $this ;
}
/**
* Записать: идентификаторы пересылаемых сообщений
*
* @param ?array $forward Идентификаторы пересылаемых сообщений
*
* @return static
*/
public function forward ( ? array $forward ) : static
{
// Запись в свойство
$this -> __set ( 'forward_messages' , $forward );
return $this ;
}
/**
* Записать: идентификатор стикера
*
* @param ?string $sticker_id Идентификатор стикера
*
* @return static
*/
public function sticker_id ( ? string $sticker_id ) : static
{
// Запись в свойство
$this -> __set ( 'sticker_id' , $sticker_id );
return $this ;
}
/**
* Записать: идентификатор стикера
*
* @param ?string $sticker Идентификатор стикера
*
* @return static
*/
public function sticker ( ? string $sticker ) : static
{
// Запись в свойство
$this -> __set ( 'sticker_id' , $sticker );
return $this ;
}
/**
* Записать: полезная нагрузка
*
* @param ?string $payload Полезная нагрузка
*
* @return static
*/
public function payload ( ? string $payload ) : static
{
// Запись в свойство
$this -> __set ( 'payload' , $payload );
return $this ;
}
/**
* Записать: не создавать представление ссылки в сообщении?
*
* @param bool $dont_parse_links Н е создавать представление ссылки в сообщении?
*
* @return static
*/
public function dont_parse_links ( bool $dont_parse_links ) : static
{
// Запись в свойство
$this -> __set ( 'dont_parse_links' , $dont_parse_links );
return $this ;
}
/**
* Записать: создавать представление ссылки в сообщении?
*
* @param bool $payload Cо зда ва ть представление ссылки в сообщении?
*
* @return static
*/
public function links ( bool $links ) : static
{
// Запись в свойство
$this -> __set ( 'dont_parse_links' , ! $links );
return $this ;
}
/**
* Записать: отключить уведомление о б упоминании в сообщении?
*
* @param bool $disable_mentions Отключить уведомление о б упоминании в сообщении?
*
* @return static
*/
public function disable_mentions ( bool $disable_mentions ) : static
{
// Запись в свойство
$this -> __set ( 'disable_mentions' , $disable_mentions );
return $this ;
}
/**
* Записать: включить уведомление о б упоминании в сообщении?
*
* @param bool $mentions Включить уведомление о б упоминании в сообщении?
*
* @return static
*/
public function mentions ( bool $mentions ) : static
{
// Запись в свойство
$this -> __set ( 'disable_mentions' , ! $mentions );
return $this ;
}
/**
* Записать: интент
*
* @param ?string $intent Интент
*
* @return static
*/
public function intent ( ? string $intent ) : static
{
// Запись в свойство
$this -> __set ( 'intent' , $intent );
return $this ;
}
/**
* Записать: число, которое будет использоваться для работы с интентами
*
* @param ?int $subscribe_id Число, которое будет использоваться для работы с интентами
*
* @return static
*/
public function subscribe_id ( ? int $subscribe_id ) : static
{
// Запись в свойство
$this -> __set ( 'subscribe_id' , $subscribe_id );
return $this ;
}
/**
* Записать: число, которое будет использоваться для работы с интентами
*
* @param ?int $subscribe Число, которое будет использоваться для работы с интентами
*
* @return static
*/
public function subscribe ( ? int $subscribe ) : static
{
// Запись в свойство
$this -> __set ( 'subscribe_id' , $subscribe );
return $this ;
}
@@ -184,7 +455,7 @@ final class messages extends method
*
* @see https://vk.com/dev/messages.send
*
* @param int|string|null $receiver Получатель
* @param int|string|null $destination Получатель
* @param ?string $message Сообщение
* @param ?int $lat Географическая ширина
* @param ?int $long Географическая долгота
@@ -200,7 +471,7 @@ final class messages extends method
* @param bool $disable_mentions Отключить уведомление о б упоминании в сообщении?
* @param ?string $intent Интент
* @param ?int $subscribe_id Число, которое будет использоваться для работы с интентами
* @param int|string|null $random_id Идентификатор сессии доставки сообщения (защита от повторных отправок)
* @param ? int $random_id Идентификатор сессии доставки сообщения (защита от повторных отправок)
*
* @return int|array Идентификатор успешно отправленного сообщения или ответ сервера (подразумевается ошибка)
*
@@ -212,7 +483,7 @@ final class messages extends method
* 5. Добавить content_source
*/
public function send (
int | string | null $receiver ,
int | string | null $destination = null ,
? string $message = null ,
? int $lat = null ,
? int $long = null ,
@@ -228,75 +499,75 @@ final class messages extends method
bool $disable_mentions = false ,
? string $intent = null ,
? int $subscribe_id = null ,
int| string | null $random_id = null ,
? int $random_id = null ,
) : int | array {
// Реинициализация настроек
$this -> robot -> api -> reinit ();
// Инициализация получателя
if ( is_in t ( $receiver )) ( $id = $receiver - 2000000000 ) > 0 ? $this -> robot -> api [ 'peer_id' ] = $receiver : $this -> robot -> api [ 'chat_id' ] = $id ;
else if ( is_array ( $receiver )) $this -> robot -> api [ 'peer_ids ' ] = implode ( ',' , $receiver ) ;
else if ( is_string ( $receiver )) $this -> robot -> api [ 'domain ' ] = $receiver ;
if ( isse t ( $destination )) $this -> destination = $destination ;
if ( is_int ( $this -> destination )) ( $id = $this -> destination - 2000000000 ) > 0 ? $this -> robot -> api [ 'peer_id' ] = $this -> destination : $this -> robot -> api [ 'chat_id' ] = $id ;
else if ( is_array ( $this -> destination )) $this -> robot -> api [ 'peer_ids ' ] = implode ( ',' , $this -> destination ) ;
else if ( is_string ( $this -> destination )) $this -> robot -> api [ 'domain' ] = $this -> destination ;
// Инициализация идентификатора сессии доставки сообщения (защита от повторных отправок)
$this -> robot -> api [ 'random_id' ] = $random_id ? ? match ( $this -> generate_mode ) {
generate :: date => time (),
generate :: random => rand (),
generate :: crypto => random_bytes ( 10 ),
generate :: hash => sodium_crypto_generichash ( random_bytes ( 1 0)),
default => time ()
random_id :: date => time (),
random_id :: random => rand (),
random_id :: crypto => random_int ( 0 , PHP_INT_MAX ),
default => random_int ( 0 , PHP_INT_MAX )
};
// Инициализация текста в настройках API
if ( isset ( $message )) $this -> robot -> api [ 'message' ] = $message ;
else if ( isset ( $this -> text )) $this -> robot -> api [ 'message' ] = $this -> text ;
if ( isset ( $message )) $this -> text = $message ;
if ( isset ( $this -> text )) $this -> robot -> api [ 'message' ] = $this -> text ;
// Инициализация широты
if ( isset ( $lat )) $this -> robot -> api [ 'lat' ] = $lat ;
else if ( isset ( $this -> lat )) $this -> robot -> api [ 'lat' ] = $this -> lat ;
if ( isset ( $lat )) $this -> lat = $lat ;
if ( isset ( $this -> lat )) $this -> robot -> api [ 'lat' ] = $this -> lat ;
// Инициализация долготы
if ( isset ( $long )) $this -> robot -> api [ 'long' ] = $long ;
else if ( isset ( $this -> long )) $this -> robot -> api [ 'long' ] = $this -> t longext ;
if ( isset ( $long )) $this -> long = $long ;
if ( isset ( $this -> long )) $this -> robot -> api [ 'long' ] = $this -> long ;
// Инициализация вложений
if ( isset ( $attachments )) $this -> robot -> api [ 'attachment' ] = implode ( ',' , $attachments ) ;
else if ( isset ( $this -> robot -> api -> data ) && $this -> robot -> api -> __get ( 'data' ) !== []) $this -> robot -> api [ 'attachment' ] = implode ( ',' , $this -> robot -> api -> __get ( 'data' ));
if ( isset ( $attachments )) $this -> robot -> api -> data = $attachments ;
if ( isset ( $this -> robot -> api -> data ) && $this -> robot -> api -> __get ( 'data' ) !== []) $this -> robot -> api [ 'attachment' ] = implode ( ',' , $this -> robot -> api -> __get ( 'data' ));
// Инициализация сообщения, на которое требуется ответить
if ( isset ( $reply_to )) $this -> robot -> api [ 'reply_to' ] = $reply_to ;
else if ( isset ( $this -> reply )) $this -> robot -> api [ 'reply_to' ] = $this -> reply ;
if ( isset ( $reply_to )) $this -> reply = $reply_to ;
if ( isset ( $this -> reply )) $this -> robot -> api [ 'reply_to' ] = $this -> reply ;
// Инициализация пересылаемых сообщений
if ( isset ( $forward_messages )) $this -> robot -> api [ ' forward_messages' ] = implode ( ',' , $forward_messages ) ;
else if ( isset ( $this -> forward_messages )) $this -> robot -> api [ 'forward_messages' ] = implode ( ',' , $this -> forward_messages );
if ( isset ( $forward_messages )) $this -> forward_messages = $forward_messages ;
if ( isset ( $this -> forward_messages )) $this -> robot -> api [ 'forward_messages' ] = implode ( ',' , $this -> forward_messages );
// Инициализация стикера
if ( isset ( $sticker_id )) $this -> robot -> api [ 'sticker_id' ] = $sticker_id ;
else if ( isset ( $this -> sticker_id )) $this -> robot -> api [ 'sticker_id' ] = $this -> sticker_id ;
if ( isset ( $this -> sticker_id )) $this -> robot -> api [ 'sticker_id' ] = $this -> sticker_id ;
// Инициализация полезной нагрузки
if ( isset ( $payload )) $this -> robot -> api [ ' payload' ] = $payload ;
else if ( isset ( $this -> payload )) $this -> robot -> api [ 'payload' ] = $this -> payload ;
if ( isset ( $payload )) $this -> payload = $payload ;
if ( isset ( $this -> payload )) $this -> robot -> api [ 'payload' ] = $this -> payload ;
// Инициализация пользовательского соглашения
// $this->robot->api['content_source'] = $this->robot->content_source;
// Инициализация "не создавать представление ссылки в сообщении?"
if ( $dont_parse_links ) $this -> robot -> api [ ' dont_parse_links' ] = 1 ;
else if ( $this -> dont_parse_links ) $this -> robot -> api [ 'dont_parse_links' ] = 1 ;
if ( isset ( $dont_parse_links )) $this -> dont_parse_links = $ dont_parse_links;
if ( $this -> dont_parse_links ) $this -> robot -> api [ 'dont_parse_links' ] = 1 ;
// Инициализация "отключить уведомление о б упоминании в сообщении?"
if ( $disable_mentions ) $this -> robot -> api [ ' disable_mentions' ] = 1 ;
else if ( $this -> disable_mentions ) $this -> robot -> api [ 'disable_mentions' ] = 1 ;
if ( isset ( $disable_mentions )) $this -> disable_mentions = $disable_mentions ;
if ( $this -> disable_mentions ) $this -> robot -> api [ 'disable_mentions' ] = 1 ;
// Инициализация интентов
if ( isset ( $intent )) $this -> robot -> api [ 'intent' ] = $intent ;
else if ( isset ( $this -> intent )) $this -> robot -> api [ 'intent' ] = $this -> intent ;
if ( isset ( $this -> intent )) $this -> robot -> api [ 'intent' ] = $this -> intent ;
// Инициализация числа, которое будет использоваться для работы с интентами
if ( isset ( $subscribe_id )) $this -> robot -> api [ ' subscribe_id' ] = $subscribe_id ;
else if ( isset ( $this -> subscribe_id )) $this -> robot -> api [ 'subscribe_id' ] = $this -> subscribe_id ;
if ( isset ( $subscribe_id )) $this -> subscribe_id = $subscribe_id ;
if ( isset ( $this -> subscribe_id )) $this -> robot -> api [ 'subscribe_id' ] = $this -> subscribe_id ;
// Проверка сформированного сообщения
if ( ! $this -> robot -> api -> offsetExists ( 'message' ) && ! $this -> robot -> api -> offsetExists ( 'attachment' )) throw new Exception ( 'Сообщение должно содержать текст, либо вложение' );
@@ -309,7 +580,7 @@ final class messages extends method
throw new Exception ( 'ВКонтакте: ' . $request -> error -> error_msg , $request -> error -> error_code );
}
if ( $this -> send_mode === send :: check ) {
if ( $this -> send_mode === message_ send:: check ) {
// Надёжная доставка сообщения
if ( ! empty ( $request [ " response " ])) {
@@ -328,7 +599,7 @@ final class messages extends method
//!!!!!!!!!!!!!!!!!!!!!!!!!
// Повторная отправка
$this -> send ( $receiver );
$this -> send ( $destination );
}
} else {
}
@@ -390,38 +661,6 @@ final class messages extends method
return $request -> response ;
}
/**
* Записать свойство
*
* @param string $name Название
* @param mixed $value Значение
*
* @return void
*/
public function __set ( string $name , mixed $value ) : void
{
match ( $name ) {
default => throw new Exception ( " Свойство $name не найдено " , 404 )
};
}
/**
* Прочитать свойство
*
* @param string $name Название
*
* @return mixed
*/
public function __get ( string $name ) : mixed
{
return match ( $name ) {
'text' => $this -> text ? ? throw new Exception ( 'Текст не инициализирован' ),
'forward' => empty ( $this -> forward ) ? throw new Exception ( 'Сообщения для пересылки не инициализированы' ) : $this -> forward ,
'reply' => isset ( $this -> reply ) ? $this -> reply : throw new Exception ( 'Сообщение для ответа не инициализировано' ),
default => throw new Exception ( " Свойство $name не найдено " , 404 )
};
}
/**
* Получить информацию о сообщении по id
*
@@ -459,4 +698,63 @@ final class messages extends method
return $request -> response ;
}
/**
* Записать свойство
*
* @param string $name Название
* @param mixed $value Значение
*
* @return void
*/
public function __set ( string $name , mixed $value ) : void
{
match ( $name ) {
'send_mode' => $this -> send_mode = $value ,
'generate_mode' => $this -> generate_mode = $value ,
'destination' => $this -> destination = $value ,
'text' => $this -> text = $value ,
'lat' , 'latitude' => $this -> lat = $value ,
'long' , 'longitude' => $this -> long = $value ,
'reply_to' , 'reply' , 'to' => $this -> reply_to = $value ,
'forward_messages' => $this -> forward_messages = $value ,
'sticker_id' , 'sticker' => $this -> sticker_id = $value ,
'payload' => $this -> payload = $value ,
'dont_parse_links' => $this -> dont_parse_links = $value ,
'parse_links' , 'parse' => $this -> dont_parse_links = ! $value ,
'disable_mentions' => $this -> disable_mentions = $value ,
'mentions' => $this -> disable_mentions = ! $value ,
'intent' => $this -> intent = $value ,
'subscribe_id' => $this -> subscribe_id = $value ,
default => throw new Exception ( " Свойство $name не найдено " , 404 )
};
}
/**
* Прочитать свойство
*
* @param string $name Название
*
* @return mixed
*/
public function __get ( string $name ) : mixed
{
return match ( $name ) {
'send_mode' => $this -> send_mode ,
'generate_mode' => $this -> generate_mode ,
'destination' => $this -> destination ,
'text' => $this -> text ,
'lat' , 'latitude' => $this -> lat ,
'long' , 'longitude' => $this -> long ,
'reply_to' , 'reply' , 'to' => $this -> reply_to ,
'forward_messages' => $this -> forward_messages ,
'sticker_id' , 'sticker' => $this -> sticker_id ,
'payload' => $this -> payload ,
'dont_parse_links' , 'parse_links' , 'parse' => $this -> dont_parse_links ,
'disable_mentions' , 'mentions' => $this -> disable_mentions ,
'intent' => $this -> intent ,
'subscribe_id' => $this -> subscribe_id ,
default => throw new Exception ( " Свойство $name не найдено " , 404 )
};
}
}