rebuilding architecture; resolved #1, resolved #2, resolved #3, resolved #4, resolved #5, resolved #6

This commit is contained in:
2025-02-01 23:06:28 +07:00
parent 922cbaa09b
commit 845a9d04e0
17 changed files with 1422 additions and 95 deletions

View File

@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace svoboda\negotiator\models\telegram;
// Files of the project
use svoboda\negotiator\models\core,
svoboda\negotiator\models\account,
svoboda\negotiator\models\enumerations\language;
// Framework for Telegram
use Zanzara\Zanzara,
Zanzara\Context as context,
Zanzara\Telegram\Type\Input\InputFile as file_input,
Zanzara\Telegram\Type\File\Document as document,
Zanzara\Middleware\MiddlewareNode as node,
Zanzara\Telegram\Type\User as user;
// Baza database
use mirzaev\baza\record;
// Built-in libraries
use Exception as exception,
Error as error;
/**
* Telegram settings
*
* @package svoboda\negotiator\models\telegram
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class settings extends core
{
/**
* Settings: language
*
* Responce for the command: "/language"
*
* Sends a language selection menu
*
* @param context $context Request data from Telegram
*
* @return void
*/
public static function settings_language(context $context): void
{
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Initializing the title
$title = '🌏 *' . $localization['language_settings'] . '*';
// Declaring the buffer of generated keyboard with languages
$keyboard = [];
// Initializing the iterator of rows
$row = 0;
foreach (language::cases() as $language) {
// Iterating over languages
// Initializing the row
$keyboard[$row] ??= [];
// Writing the language choose button into the buffer of generated keyboard with languages
$keyboard[$row][] = [
'text' => ($language->flag() ? $language->flag() . ' ' : '') . $language->label($language),
'callback_data' => 'settings_language_' . $language->name
];
// When reaching 4 buttons in a row, move to the next row
if (count($keyboard[$row]) === 4) ++$row;
}
// Sending the message
$context->sendMessage(
<<<TXT
$title
TXT,
[
'reply_markup' => [
'inline_keyboard' => $keyboard
],
'disable_notification' => true
]
)
->then(function ($message) use ($context) {
//
});
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
}

View File

@@ -0,0 +1,274 @@
<?php
declare(strict_types=1);
namespace svoboda\negotiator\models\telegram;
// Files of the project
use svoboda\negotiator\models\core,
svoboda\negotiator\models\account,
svoboda\negotiator\models\enumerations\language;
// Framework for Telegram
use Zanzara\Zanzara,
Zanzara\Context as context,
Zanzara\Telegram\Type\Input\InputFile as file_input,
Zanzara\Telegram\Type\File\Document as document,
Zanzara\Middleware\MiddlewareNode as node,
Zanzara\Telegram\Type\User as user;
// Baza database
use mirzaev\baza\record;
// Built-in libraries
use Exception as exception,
Error as error;
/**
* Telegram commands
*
* @package svoboda\negotiator\models\telegram
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class commands extends core
{
/**
* Menu
*
* Responce for the commands: "/start", '/menu'
*
* @param context $context Request data from Telegram
*
* @return void
*/
public static function menu(context $context): void
{
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Initializing the title
$title = '*' . $localization['svoboda'] . '*';
// Sending the message
$context->sendMessage(<<<TXT
$title
TXT)
->then(function ($message) use ($context) {
//
});
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Message
*
* Responce for the command: "/message"
*
* Start a process for creating message
*
* @param context $context Request data from Telegram
*
* @return void
*/
public static function message(context $context): void
{
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
// Initializing the title
$title = '';
// Sending the message
$context->sendMessage(<<<TXT
*⚠️ Failed to initialize your Telegram account*
TXT)
->then(function ($message) use ($context) {
//
});
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Language
*
* Responce for the command: "/language"
*
* Sends a language selection menu
*
* @param context $context Request data from Telegram
*
* @return void
*/
public static function language(context $context): void
{
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Declaring the buffer of generated keyboard with languages
$keyboard = [];
// Initializing the iterator of rows
$row = 0;
foreach (language::cases() as $language) {
// Iterating over languages
// Initializing the row
$keyboard[$row] ??= [];
// Writing the language choose button into the buffer of generated keyboard with languages
$keyboard[$row][] = [
'text' => ($language->flag() ? $language->flag() . ' ' : '') . $language->label($language),
'callback_data' => 'settings_language_' . $language->name
];
// When reaching 4 buttons in a row, move to the next row
if (count($keyboard[$row]) === 4) ++$row;
}
// Writing the button for helping lozalizing
$keyboard[++$row] = [
[
'text' => '🗂 ' . $localization['settings_language_add'],
'url' => 'https://git.svoboda.works/svoboda/negotiator/src/branch/stable/svoboda/negotiator/system/localizations'
]
];
// Sending the message
$context->sendMessage(
'🌏 *' . $localization['settings_language_title'] . '*',
[
'reply_markup' => [
'inline_keyboard' => $keyboard
],
'disable_notification' => true
]
);
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Society
*
* Responce for the command: "/society"
*
* Sends the "mushroom" image and the localized text "why so shroomious"
*
* @param context $context Request data from Telegram
*
* @return void
*/
public static function society(context $context): void
{
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Sending the message
$context->sendPhoto(
new file_input(STORAGE . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'mushroom.jpg'),
[
'caption' => $localization['why_so_shroomious'],
'disable_notification' => true
]
);
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
}

View File

@@ -0,0 +1,552 @@
<?php
declare(strict_types=1);
namespace svoboda\negotiator\models\telegram;
// Files of the project
use svoboda\negotiator\models\core,
svoboda\negotiator\models\account,
svoboda\negotiator\models\enumerations\language;
// Framework for Telegram
use Zanzara\Zanzara,
Zanzara\Context as context,
Zanzara\Telegram\Type\Input\InputFile as file_input,
Zanzara\Telegram\Type\File\Document as document,
Zanzara\Middleware\MiddlewareNode as node,
Zanzara\Telegram\Type\User as user;
// Baza database
use mirzaev\baza\record;
// Built-in libraries
use Exception as exception,
Error as error;
/**
* Telegram middlewares
*
* @package svoboda\negotiator\models\telegram
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class middlewares extends core
{
/**
* Account (middleware)
*
* Initialize or registrate the account and write it to the `account` variable inside the `$context`
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function account(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the telegram account
$telegram = $context->getEffectiveUser();
// Initializing the account
/* $account = new account()->initialize($telegram); */
$account = (new account())->initialize($telegram);
if ($account instanceof record) {
// Initialized the account
// Writing the account into the context variable
$context->set('account', $account);
// Continuation of the process
$next($context);
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Language (middleware)
*
* Implement the account language
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function language(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
if ($account->language) {
// Initialized the language parameter
try {
// Writing the account language into the context variable
$context->set('language', language::{$account->language});
} catch (error $error) {
// Not initialized the language
// Writing the default language into the context variable
$context->set('language', language::en);
}
} else {
// Not initialized the language parameter
// Writing the default language into the context variable
$context->set('language', language::en);
}
// Continuation of the process
$next($context);
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Localization (middleware)
*
* Implement the account language and initialize the localization file
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function localization(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
// Initializing the language
$language = $context->get('language');
if ($language instanceof language) {
// Initialized the language
// Initializing path to the localization file
$file = LOCALIZATIONS . DIRECTORY_SEPARATOR . strtolower($language->label()) . '.php';
if (file_exists($file) && is_readable($file)) {
// Found the localization file
// Initializing localization
$localization = require($file);
if (is_array($localization)) {
// Initializae localization
// Writing localization into the context variable
$context->set('localization', $localization);
// Continuation of the process
$next($context);
} else {
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not found the localization file
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize the localization file*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized language
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize language*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* System (middleware)
*
* Check the account for access to the system
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function system(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
if ($account->authorized_system) {
// Authorized the account to the system
// Continuation of the process
$next($context);
} else {
// Not authorized the account to the system
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Sending the message
$context->sendMessage('⛔ *' . $localization['not_authorized_system'] . '*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
// Stopping the process
$context->set('stop', true);
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Contact (middleware)
*
* Check the account for access to contact with the organization
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function contact(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
if ($account->authorized_contact) {
// Authorized the account to contact with the organization
// Continuation of the process
$next($context);
} else {
// Not authorized the account to contact with the organization
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Sending the message
$context->sendMessage('⛔ *' . $localization['not_authorized_contact'] . '*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
// Stopping the process
$context->set('stop', true);
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Request (middleware)
*
* Check the account for access to request to the organization
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function request(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
if ($account->authorized_request) {
// Authorized the account to request to the organization
// Continuation of the process
$next($context);
} else {
// Not authorized the account to request to the organization
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Sending the message
$context->sendMessage('⛔ *' . $localization['not_authorized_request'] . '*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
// Stopping the process
$context->set('stop', true);
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* Settings (middleware)
*
* Check the account for access to the settings
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function settings(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
if ($account->authorized_settings) {
// Authorized the account to the settings
// Continuation of the process
$next($context);
} else {
// Not authorized the account to the settings
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Sending the message
$context->sendMessage('⛔ *' . $localization['not_authorized_settings'] . '*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
// Stopping the process
$context->set('stop', true);
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
/**
* System settings (middleware)
*
* Check the account for access to the system settings
*
* @param context $context
* @param node $next
*
* @return void
*/
public static function system_settings(context $context, node $next): void
{
// Is the process stopped?
if ($context->get('stop')) return;
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
if ($account->authorized_system_settings) {
// Authorized the account to the system settings
// Continuation of the process
$next($context);
} else {
// Not authorized the account to the system settings
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Sending the message
$context->sendMessage('⛔ *' . $localization['not_authorized_system_settings'] . '*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
// Stopping the process
$context->set('stop', true);
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
}

View File

@@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
namespace svoboda\negotiator\models\telegram;
// Files of the project
use svoboda\negotiator\models\core,
svoboda\negotiator\models\account,
svoboda\negotiator\models\enumerations\language,
svoboda\negotiator\models\telegram\middlewares;
// Framework for Telegram
use Zanzara\Zanzara,
Zanzara\Context as context,
Zanzara\Telegram\Type\Input\InputFile as file_input,
Zanzara\Telegram\Type\File\Document as document,
Zanzara\Middleware\MiddlewareNode as node,
Zanzara\Telegram\Type\User as user;
// Baza database
use mirzaev\baza\record;
// Built-in libraries
use Exception as exception,
Error as error;
/**
* Telegram settings
*
* @package svoboda\negotiator\models\telegram
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class settings extends core
{
/**
* Language
*
* Write language into the account record
*
* @param context $context Request data from Telegram
* @param language $language The language
*
* @return void
*/
public static function language(context $context, language $language): void
{
// Initializing the account
$account = $context->get('account');
if ($account instanceof record) {
// Initialized the account
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
// Updating the account in the database
$updated = account::$database->read(
filter: fn(record $record) => $record->identifier === $account->identifier,
update: function (record &$record) use ($language) {
// Writing new language value into the record
$record->language = $language->name;
},
amount: 1
)[0] ?? null;
if ($updated instanceof record) {
// Updated the account in the database
// Writing the updated account into the context variable
$context->set('account', $updated);
middlewares::language($context, new node(function (context $context) use ($account, $updated) {
// Updated language
middlewares::localization($context, new node(function (context $context) use ($account, $updated) {
// Updated localization
// Initializing localization
$localization = $context->get('localization');
if ($localization) {
// Initialized localization
try {
// Initializing the old language
$old = language::{$account->language};
// Initializing the new language
$new = language::{$updated->language};
// Sending the message
$context->sendMessage('✅ *' . $localization['settings_language_update_success'] . '* ' . ($old->flag() ? $old->flag() . ' ' : '') . $old->label($new) . ' » ' . ($new->flag() ? $new->flag() . ' ' : '') . $new->label($new))
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
} catch (error $error) {
// Failed to send the message about language update
// Sending the message
$context->sendMessage('❎ *' . $localization['settings_language_update_fail'])
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}));
}));
} else {
// Not updated the account in the database
// Sending the message
$context->sendMessage('❎ *' . $localization['settings_language_update_fail'])
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized localization
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
} else {
// Not initialized the account
// Sending the message
$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
->then(function ($message) use ($context) {
// Ending the conversation process
$context->endConversation();
});
}
}
}