119 lines
2.9 KiB
PHP
119 lines
2.9 KiB
PHP
<?php
|
||
|
||
// Фреймворк Telegram
|
||
use Zanzara\Zanzara;
|
||
use Zanzara\Context;
|
||
use Zanzara\Config;
|
||
|
||
// Фреймворк для Google Sheets
|
||
use Flow\ETL\Adapter\GoogleSheet\GoogleSheetRange,
|
||
Flow\ETL\Adapter\GoogleSheet\GoogleSheetExtractor,
|
||
Flow\ETL\Adapter\GoogleSheet\Columns,
|
||
Flow\ETL\Flow,
|
||
Flow\ETL\Config as _config,
|
||
Flow\ETL\FlowContext,
|
||
Flow\ETL\Row\Entry,
|
||
Flow\ETL\Row,
|
||
Flow\ETL\DSL\To,
|
||
Flow\ETL\DSL\From;
|
||
|
||
// Фреймворк для Google API
|
||
use Google\Client,
|
||
Google\Service\Sheets,
|
||
Google\Service\Sheets\ValueRange;
|
||
|
||
|
||
require __DIR__ . '/../../../../../../vendor/autoload.php';
|
||
|
||
/* ini_set('error_reporting', E_ALL);
|
||
ini_set('display_errors', 1);
|
||
ini_set('display_startup_errors', 1); */
|
||
|
||
$config = new Config();
|
||
$config->setParseMode(Config::PARSE_MODE_MARKDOWN);
|
||
|
||
$bot = new Zanzara(require(__DIR__ . '/../settings/key.php'), $config);
|
||
|
||
$timer;
|
||
|
||
$member;
|
||
|
||
$bot->onMessage(function (Context $ctx) {
|
||
if ($members = $ctx->getMessage()->getNewChatMembers()) {
|
||
// Новый аккаунт в чате
|
||
|
||
$ctx->sendMessage('⚠️ Введите ваш табельный номер для авторизации', ['reply_to_message_id' => $ctx->getMessage()->getMessageId()])
|
||
->then(function () use ($ctx, $members) {
|
||
|
||
global $member;
|
||
|
||
// Инициализация идентификатора сотрудника
|
||
$member = $id = $members[0]->getId();
|
||
|
||
global $timer;
|
||
|
||
$timer = React\Promise\Timer\sleep(180);
|
||
|
||
$timer->then(
|
||
function ($value) use ($ctx, $id) {
|
||
// Изгнание из чата
|
||
$ctx->kickChatMember($ctx->getMessage()->getChat()->getId(), $id);
|
||
|
||
$ctx->wipeCache();
|
||
|
||
$ctx->endConversation();
|
||
},
|
||
function () {
|
||
}
|
||
);
|
||
|
||
$ctx->nextStep('check');
|
||
});
|
||
}
|
||
});
|
||
|
||
function check(Context $ctx)
|
||
{
|
||
global $member;
|
||
|
||
if ($member === $ctx->getMessage()->getFrom()->getId()) {
|
||
// Отправителем табельного номера является тот кто подключился к серверу
|
||
|
||
$id = $ctx->getMessage()->getText();
|
||
|
||
$settings = json_decode(require(__DIR__ . '/../settings/workers/google.php'), true);
|
||
$document = require(__DIR__ . '/../settings/workers/document.php');
|
||
$sheets = require(__DIR__ . '/../settings/workers/sheets.php');
|
||
|
||
$client = new Client();
|
||
$client->setScopes(Sheets::SPREADSHEETS);
|
||
$client->setAuthConfig($settings);
|
||
$api = new Sheets($client);
|
||
|
||
foreach ($sheets as $sheet) {
|
||
$rows = (new Flow())->read(new GoogleSheetExtractor($api, $document, new Columns($sheet, 'A', 'A'), true, 1000));
|
||
|
||
foreach ($rows->fetch(10000) as $row) {
|
||
// Перебор строк
|
||
|
||
if ($id === $row->toArray()['ID']) {
|
||
$ctx->sendMessage("✅ Авторизован сотрудник: $id");
|
||
|
||
global $timer;
|
||
|
||
// Отмена блокировки
|
||
$timer->cancel();
|
||
|
||
$ctx->endConversation();
|
||
|
||
return;
|
||
}
|
||
}
|
||
|
||
$ctx->sendMessage("⛔ Не найден сотрудник: $id");
|
||
}
|
||
}
|
||
}
|
||
|
||
$bot->run();
|