@ -23,41 +23,6 @@ $arangodb = new connection(require __DIR__ . '/../settings/arangodb.php');
ini_set ( 'display_errors' , 1 );
ini_set ( 'display_errors' , 1 );
ini_set ( 'display_startup_errors' , 1 ); */
ini_set ( 'display_startup_errors' , 1 ); */
function escape ( string $text )
{
return str_replace (
[
'#' ,
'*' ,
'_' ,
'=' ,
'.' ,
'[' ,
']' ,
'(' ,
')' ,
'-' ,
'>' ,
'<'
],
[
'\#' ,
'\*' ,
'\_' ,
'\\=' ,
'\.' ,
'\[' ,
'\]' ,
'\(' ,
'\)' ,
'\-' ,
'\>' ,
'\<'
],
$text
);
}
/**
/**
* Авторизация
* Авторизация
*
*
@ -114,7 +79,7 @@ function worker(string $id): _document|null|false
LIMIT 1
LIMIT 1
RETURN e
RETURN e
)
)
FILTER d . _id == e [ 0 ] . _to
FILTER d . _id == e [ 0 ] . _to && d . active == true
SORT d . created DESC , d . _key DESC
SORT d . created DESC , d . _key DESC
LIMIT 1
LIMIT 1
RETURN d
RETURN d
@ -207,13 +172,6 @@ function generateMenu(Context $ctx): void
if ( $account = authorization ( $ctx -> getMessage () ? -> getFrom () ? -> getId () ? ? $ctx -> getCallbackQuery () -> getFrom () -> getId ())) {
if ( $account = authorization ( $ctx -> getMessage () ? -> getFrom () ? -> getId () ? ? $ctx -> getCallbackQuery () -> getFrom () -> getId ())) {
// Успешная авторизация
// Успешная авторизация
if ( ! $account -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( $account -> banned ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( ! ( $worker = worker ( $account -> getId ())) -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( $worker -> fired ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else {
// Активен аккаунт
$ctx -> sendMessage ( '👋 Здравствуйте, ' . preg_replace ( '/([._\-()!#])/' , '\\\$1' , $account -> name [ 'first' ]), [
$ctx -> sendMessage ( '👋 Здравствуйте, ' . preg_replace ( '/([._\-()!#])/' , '\\\$1' , $account -> name [ 'first' ]), [
'reply_markup' => [
'reply_markup' => [
'inline_keyboard' => [
'inline_keyboard' => [
@ -227,7 +185,6 @@ function generateMenu(Context $ctx): void
$ctx -> setChatDataItem ( " menu " , $message );
$ctx -> setChatDataItem ( " menu " , $message );
});
});
}
}
}
}
}
/**
/**
@ -236,11 +193,10 @@ function generateMenu(Context $ctx): void
* @ param int $amount Количество
* @ param int $amount Количество
* @ param ? string $date З а какую дату ( unixtime )
* @ param ? string $date З а какую дату ( unixtime )
* @ param int $page Страница
* @ param int $page Страница
* @ param _document $worker Сотрудник
*
*
* @ return Cursor
* @ return Cursor
*/
*/
function requests ( int $amount = 5 , ? string $date = null , int $page = 1 , _document $worker ): Cursor
function requests ( int $amount = 5 , ? string $date = null , int $page = 1 ): Cursor
{
{
global $arangodb ;
global $arangodb ;
@ -261,12 +217,11 @@ function requests(int $amount = 5, ?string $date = null, int $page = 1, _documen
[
[
'query' => sprintf (
'query' => sprintf (
// d.date < %s там специально, не менять на <=
// d.date < %s там специально, не менять на <=
" FOR d IN task FILTER ((d.date >= %s && d.date < %s && d.start >= '05:00') || (d.date >= %s && d.date < %s && d.start < '05:00')) && d.worker == null && d.market != null && d.confirmed != true && d.published == true && d.completed != true && (FOR m IN market FILTER m.id == d.market && IS_ARRAY(m.bans) SORT m.created DESC, m._key DESC LIMIT 1 RETURN !POSITION(m.bans, \" %s \" ))[0] SORT d.created DESC, d._key DESC LIMIT %d, %d RETURN d" ,
" FOR d IN task FILTER ((d.date >= %s && d.date < %s && d.start >= '05:00') || (d.date >= %s && d.date < %s && d.start < '05:00')) && d.worker == null && d.market != null && d.confirmed != true && d.published == true && d.completed != true SORT d.created DESC, d._key DESC LIMIT %d, %d RETURN d" ,
$from = ( new DateTime ( " @ $date " )) -> setTime ( 0 , 0 ) -> format ( 'U' ),
$from = ( new DateTime ( " @ $date " )) -> setTime ( 0 , 0 ) -> format ( 'U' ),
$to = ( new DateTime ( " @ $date " )) -> modify ( '+1 day' ) -> setTime ( 0 , 0 ) -> format ( 'U' ),
$to = ( new DateTime ( " @ $date " )) -> modify ( '+1 day' ) -> setTime ( 0 , 0 ) -> format ( 'U' ),
$to ,
$to ,
( new DateTime ( " @ $date " )) -> modify ( '+2 day' ) -> setTime ( 0 , 0 ) -> format ( 'U' ),
( new DateTime ( " @ $date " )) -> modify ( '+2 day' ) -> setTime ( 0 , 0 ) -> format ( 'U' ),
$worker -> id ,
$offset ,
$offset ,
$amount + $offset - ( $page > 0 )
$amount + $offset - ( $page > 0 )
),
),
@ -300,58 +255,20 @@ function requests_previous(Context $ctx): void
}
}
function request_choose ( Context $ctx ) : void
function request_choose ( Context $ctx ) : void
{
if (( $account = authorization ( $ctx -> getCallbackQuery () -> getFrom () -> getId ())) instanceof _document ) {
// Авторизован
if ( ! $account -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( $account -> banned ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( ! ( $worker = worker ( $account -> getId ())) -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( $worker -> fired ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else {
// Активен аккаунт
// Инициализация ключа инстанции task в базе данных
preg_match ( '/\->\s#(\d+)\n/' , $ctx -> getCallbackQuery () -> getMessage () -> getText (), $matches );
// Запись ключа инстанции task (заявка на которую регистрируется сотрудник)
$ctx -> setChatDataItem ( " request_confirmation_target " , $matches [ 1 ]);
// Запрос подтверждения
$ctx -> sendMessage ( " ⚡ *Подтверждение записи* \n \n " . preg_replace ( '/(^[^:\s\n\r]+:)/m' , '*$1*' , preg_replace ( '/(\\\#\d+)/' , '*$1*' , escape ( $ctx -> getCallbackQuery () -> getMessage () -> getText ()))) . " \n \n *⚠️ Вы подтверждаете отправку запроса?* " , [
'reply_markup' => [
'inline_keyboard' => [
[
[ 'text' => 'Подтвердить' , 'callback_data' => 'request_confirmed' ],
[ 'text' => 'Отменить' , 'callback_data' => 'request_rejected' ]
]
]
]
]) -> then ( function ( $message ) use ( $ctx ) {
// Запись сообщения в кеш (на случай необходимости е г о удаления)
$ctx -> setChatDataItem ( " request_confirmation " , $message );
});
}
}
}
function request_confirmed ( Context $ctx ) : void
{
{
global $arangodb ;
global $arangodb ;
if (( $account = authorization ( $ctx -> getCallbackQuery () -> getFrom () -> getId ())) instanceof _document ) {
if (( $account = authorization ( $ctx -> getCallbackQuery () -> getFrom () -> getId ())) instanceof _document ) {
// Авторизован
// Авторизован
$ctx -> getChatDataItem ( " request_confirmation_target " ) -> then ( function ( $_key ) use ( $ctx , $arangodb , $account ) {
// Инициализация ключа инстанции task в базе данных
// Прочитана запрашиваемая заявка
preg_match ( '/\->\s#(\d+)\n/' , $ctx -> getCallbackQuery () -> getMessage () -> getText (), $matches );
$_key = $matches [ 1 ];
// Инициализация инстанции task в базе данных (выбранного задания)
// Инициализация инстанции task в базе данных (выбранного задания)
$task = collection :: search ( $arangodb -> session , sprintf ( " FOR d IN task FILTER d._key == '%s' && d.published == true && d.completed != true && worker == null RETURN d" , $_key ));
$task = collection :: search ( $arangodb -> session , sprintf ( " FOR d IN task FILTER d._key == '%s' && d.published == true && d.completed != true RETURN d " , $_key ));
if ( $task instanceof _document ) {
if ( $worker = worker ( $account -> getId ())) {
// Найдена заявка (подразумевается, что не занята)
if ( $worker ? ? = worker ( $account -> getId ())) {
// Найден сотрудник
// Найден сотрудник
// Запись идентификатора нового сотрудника
// Запись идентификатора нового сотрудника
@ -363,25 +280,17 @@ function request_confirmed(Context $ctx): void
if ( document :: update ( $arangodb -> session , $task )) {
if ( document :: update ( $arangodb -> session , $task )) {
// Записано обновление в базу данных
// Записано обновление в базу данных
$ctx -> getChatDataItem ( " request_all " ) -> then ( function ( $requests = []) use ( $ctx ) {
$ctx -> getChatDataItem ( " request_all " ) -> then ( function ( $requests = []) use ( $ctx , $_key ) {
// Удаление сообщений связанных с запросом
// Удаление сообщений связанных с запросом
foreach ( $requests ? ? [] as $_message ) $ctx -> deleteMessage ( $_message -> getChat () -> getId (), $_message -> getMessageId ());
foreach ( $requests ? ? [] as $_message ) $ctx -> deleteMessage ( $_message -> getChat () -> getId (), $_message -> getMessageId ());
});
$ctx -> setChatDataItem ( " request_all " , []);
$ctx -> setChatDataItem ( " request_all " , []);
$ctx -> getChatDataItem ( " request_confirmation " ) -> then ( function ( $message ) use ( $ctx ) {
$ctx -> sendMessage ( " ✅ *Заявка принята:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
$ctx -> deleteMessage ( $message -> getChat () -> getId (), $message -> getMessageId ());
});
$ctx -> setChatDataItem ( " request_confirmation_target " , null );
$ctx -> sendMessage ( " ✅ *Вы зарегистрировались на заявку:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
generateMenu ( $ctx );
generateMenu ( $ctx );
});
});
// End of the process
// End of the process
$ctx -> endConversation ();
$ctx -> endConversation ();
} else $ctx -> sendMessage ( " ❎ *Н е удалось принять заявку:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
generateMenu ( $ctx );
});
});
} else $ctx -> sendMessage ( " ❎ *Н е удалось принять заявку:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
} else $ctx -> sendMessage ( " ❎ *Н е удалось принять заявку:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
generateMenu ( $ctx );
generateMenu ( $ctx );
@ -389,41 +298,14 @@ function request_confirmed(Context $ctx): void
} else $ctx -> sendMessage ( " ❎ *Н е удалось принять заявку:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
} else $ctx -> sendMessage ( " ❎ *Н е удалось принять заявку:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
generateMenu ( $ctx );
generateMenu ( $ctx );
});
});
});
}
}
}
}
function request_rejected ( Context $ctx ) : void
{
$ctx -> getChatDataItem ( " request_confirmation_target " ) -> then ( function ( $_key ) use ( $ctx ) {
// Прочитана запрашиваемая заявка
$ctx -> getChatDataItem ( " request_confirmation " ) -> then ( function ( $message ) use ( $ctx ) {
$ctx -> deleteMessage ( $message -> getChat () -> getId (), $message -> getMessageId ());
});
$ctx -> setChatDataItem ( " request_confirmation_target " , null );
$ctx -> sendMessage ( " ✅ *Вы отменили регистрацию на заявку:* \ # $_key " , [ 'reply_markup' => [ 'remove_keyboard' => true ]]) -> then ( function () use ( $ctx ) {
generateMenu ( $ctx );
});
// End of the process
$ctx -> endConversation ();
});
}
function day ( Context $ctx ) : void
function day ( Context $ctx ) : void
{
{
if ( ( $account = authorization ( $ctx -> getMessage () ? -> getFrom () ? -> getId () ? ? $ctx -> getCallbackQuery () -> getFrom () -> getId ( ) )) instanceof _document ) {
if ( authorization ( $ctx -> getMessage () ? -> getFrom () ? -> getId () ? ? $ctx -> getCallbackQuery () -> getFrom () -> getId ()) instanceof _document ) {
// Авторизован
// Авторизован
if ( ! $account -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( $account -> banned ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( ! ( $worker = worker ( $account -> getId ())) -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( $worker -> fired ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else {
// Активен аккаунт
// Инициализация буфера клавиатуры
// Инициализация буфера клавиатуры
$keyboard = [];
$keyboard = [];
@ -450,24 +332,16 @@ function day(Context $ctx): void
$ctx -> nextStep ( " search " );
$ctx -> nextStep ( " search " );
}
}
}
}
}
function search ( Context $ctx ) : void
function search ( Context $ctx ) : void
{
{
global $arangodb ;
global $arangodb ;
if ( ( $account = authorization ( $ctx -> getMessage () ? -> getFrom () ? -> getId () ? ? $ctx -> getCallbackQuery () -> getFrom () -> getId ( ) )) instanceof _document ) {
if ( authorization ( $ctx -> getMessage () ? -> getFrom () ? -> getId () ? ? $ctx -> getCallbackQuery () -> getFrom () -> getId ( )) instanceof _document ) {
// Авторизован
// Авторизован
if ( ! $account -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
$ctx -> getChatDataItem ( 'requests_page' ) -> then ( function ( $page ) use ( $ctx , $arangodb ) {
else if ( $account -> banned ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( ! ( $worker = worker ( $account -> getId ())) -> active ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else if ( $worker -> fired ) $ctx -> sendMessage ( '⚠️ Свяжитесь с оператором' );
else {
// Активен аккаунт
$ctx -> getChatDataItem ( 'requests_page' ) -> then ( function ( $page ) use ( $ctx , $arangodb , $worker ) {
// Найдена текущая страница
// Найдена текущая страница
// Значение страницы по умолчанию
// Значение страницы по умолчанию
@ -476,9 +350,9 @@ function search(Context $ctx): void
$ctx -> setChatDataItem ( 'requests_page' , 1 );
$ctx -> setChatDataItem ( 'requests_page' , 1 );
}
}
$generate = function ( $date ) use ( $ctx , $page , $arangodb , $worker ) {
$generate = function ( $date ) use ( $ctx , $page , $arangodb ) {
// Поиск заявок в ArangoDB
// Поиск заявок в ArangoDB
$tasks = requests ( 4 , ( string ) $date , $page , $worker );
$tasks = requests ( 4 , ( string ) $date , $page );
// Подсчёт количества прочитанных заявок из базы данных
// Подсчёт количества прочитанных заявок из базы данных
$count = $tasks -> getCount ();
$count = $tasks -> getCount ();
@ -604,7 +478,6 @@ function search(Context $ctx): void
});
});
});
});
}
}
}
}
}
$config = new Config ();
$config = new Config ();
@ -667,8 +540,6 @@ $bot->onCommand('start', function (Context $ctx) use ($stop): void {
$bot -> onCbQueryData ([ 'requests_next' ], fn ( $ctx ) => requests_next ( $ctx ));
$bot -> onCbQueryData ([ 'requests_next' ], fn ( $ctx ) => requests_next ( $ctx ));
$bot -> onCbQueryData ([ 'requests_previous' ], fn ( $ctx ) => requests_previous ( $ctx ));
$bot -> onCbQueryData ([ 'requests_previous' ], fn ( $ctx ) => requests_previous ( $ctx ));
$bot -> onCbQueryData ([ 'request_choose' ], fn ( $ctx ) => request_choose ( $ctx ));
$bot -> onCbQueryData ([ 'request_choose' ], fn ( $ctx ) => request_choose ( $ctx ));
$bot -> onCbQueryData ([ 'request_confirmed' ], fn ( $ctx ) => request_confirmed ( $ctx ));
$bot -> onCbQueryData ([ 'request_rejected' ], fn ( $ctx ) => request_rejected ( $ctx ));
$bot -> onCommand ( 'day' , fn ( $ctx ) => day ( $ctx ));
$bot -> onCommand ( 'day' , fn ( $ctx ) => day ( $ctx ));
$bot -> onCbQueryData ([ 'day' ], fn ( $ctx ) => day ( $ctx ));
$bot -> onCbQueryData ([ 'day' ], fn ( $ctx ) => day ( $ctx ));