created from kodorvan/neurobot

This commit is contained in:
2026-01-06 14:02:18 +05:00
parent 913dfb8a92
commit f2eb59336e
12 changed files with 1814 additions and 4 deletions

View File

@@ -0,0 +1,393 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\telegram;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\core,
${REPO_OWNER}\${REPO_NAME}\models\account,
${REPO_OWNER}\${REPO_NAME}\models\settings,
${REPO_OWNER}\${REPO_NAME}\models\telegram\settings as telegram_settings,
${REPO_OWNER}\${REPO_NAME}\models\telegram\processes\language\select as process_language_select;
// Library for languages support
use mirzaev\languages\language;
// The library for escaping all markdown symbols
use function mirzaev\unmarkdown;
// Framework for Telegram
use Zanzara\Context as context,
Zanzara\Telegram\Type\Message as message,
Zanzara\Telegram\Type\Input\InputFile as file_input;
/**
* Telegram commands
*
* @package ${REPO_OWNER}\${REPO_NAME}\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
{
/**
* Start
*
* Responce for command: "/start"
*
* @param context $$context Request data from Telegram
*
* @return void
*/
public static function start(context $$context): void
{
static::menu($$context);
}
/**
* Menu
*
* Responce for command: '/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 account) {
// Initialized the account
// Initializing the language
$$language = $$context->get('language');
if ($$language instanceof language) {
// Initialized the language
// Initializing localization
$$localization = $$context->get('localization');
if ($$localization) {
// Initialized localization
// Initializing the title
$$title = '📋 *' . $$localization['menu_title'] . '*';
// Initializing accounts
/* $$accounts_message = '*' . $$localization['menu_accounts'] . ':* ' . ((new account)->database->count() ?? 0); */
// Initializing the account tariff
$$tariff = $$account->tariff();
// Declaring the tariff button
$$tariff_button = [];
if ($$tariff instanceof tariff) {
// Initialized the account tariff
// Initializing the tariff button
$$tariff_button = [
'text' => '🔐 ' . $$tariff->type->label($$language) . ' (' . $$tariff->used . '/' . $$tariff->tokens . ')',
'callback_data' => 'tariffs'
];
} else {
// Initialized the account tariff
// Initializing the tariff button
$$tariff_button = [
'text' => '⚠️ ' . $$localization['menu_tariff_empty'],
'callback_data' => 'tariffs'
];
}
// Initializing the account chat
$$chat = $$account->chat();
// Initializing the chto text
$$howto = $$localization['menu_howto'];
// Sending the message
$$context->sendMessage(
<<<TXT
$$title
$$howto
TXT,
[
'reply_markup' => [
'inline_keyboard' => [
/* [
[
'text' => '⚙️ ' . $$localization[''],
'callback_data' => ''
]
] */
],
'disable_notification' => true,
'remove_keyboard' => true
],
]
)->then(function (message $$message) use ($$context) {
// Sended the message
});
} else {
// Not initialized localization
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function (message $$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 $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Ending the conversation process
$$context->endConversation();
});
}
}
/**
* Account
*
* Responce for the command: "/account"
*
* Sends information about account with menu
*
* @param context $$context Request data from Telegram
*
* @return void
*/
public static function account(context $$context): void
{
// Initializing the account
$$account = $$context->get('account');
if ($$account instanceof account) {
// Initialized the account
// Initializing localization
$$localization = $$context->get('localization');
if ($$localization) {
// Initialized localization
// Initializing title for the message
$$title = '🫵 ' . $$localization['account_title'];
// Declaring buufer of rows about authorizations
$$authorizations = '';
// Initializing rows about authorization
foreach ($$account->values() as $$key => $$value) {
// Iterating over account parameters
if (str_starts_with($$key, 'authorized_')) {
// Iterating over account authorizations
// Skipping system authorizations
if (str_starts_with($$key, 'authorized_system_')) continue;
// Writing into buffer of rows about authorizations
$$authorizations .= ($$value ? '✅' : '❎') . ' *' . ($$localization["account_$$key"] ?? $$key) . ':* ' . ($$value ? $$localization['yes'] : $$localization['no']) . "\n";
}
}
// Trimming the last line break character
$$authorizations = trim($$authorizations, "\n");
// Sending the message
$$context->sendMessage(
<<<TXT
$$title
$$authorizations
TXT,
[
'reply_markup' => [
'remove_keyboard' => true,
'disable_notification' => true
],
'link_preview_options' => [
'is_disabled' => true
]
]
);
} else {
// Not initialized localization
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function (message $$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 $$message) use ($$context) {
// Ending the conversation process
$$context->endConversation();
});
}
}
/**
* Language
*
* Responce for the command: "/language"
*
* Send the 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 account) {
// Initialized the account
// Initializing language
$$language = $$context->get('language');
if ($$language instanceof language) {
// Initialized language
// Initializing localization
$$localization = $$context->get('localization');
if ($$localization) {
// Initialized localization
// Sending the language selection
process_language_select::menu(
context: $$context,
prefix: 'settings_language_',
title: '🌏 *' . $$localization['settings_select_language_title'] . '*',
description: '🌏 *' . $$localization['settings_select_language_description'] . '*'
);
} else {
// Not initialized localization
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function (message $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
} else {
// Not initialized language
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize language*')
->then(function (message $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// 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 account) {
// 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 $$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 $$message) use ($$context) {
// Ending the conversation process
$$context->endConversation();
});
}
}
}

View File

@@ -0,0 +1,469 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\telegram;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\core,
${REPO_OWNER}\${REPO_NAME}\models\account,
${REPO_OWNER}\${REPO_NAME}\models\authorizations;
// The library for languages support
use mirzaev\languages\language;
// The library for escaping all markdown symbols
use function mirzaev\unmarkdown;
// Framework for Telegram
use Zanzara\Context as context,
Zanzara\Telegram\Type\Message as message,
Zanzara\Middleware\MiddlewareNode as node;
// Built-in libraries
use Error as error;
/**
* Telegram middlewares
*
* @package ${REPO_OWNER}\${REPO_NAME}\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 into 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);
if ($$account instanceof account) {
// 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 $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
}
/**
* Authorizations (middleware)
*
* Initialize the account authorizations and write them into the `authorizations` variable inside the `$$context`
*
* @param context $$context
* @param node $$next
*
* @return void
*/
public static function authorizations(context $$context, node $$next): void
{
// Is the process stopped?
if ($$context->get('stop')) return;
// Initializing the account
$$account = $$context->get('account');
if ($$account instanceof account) {
// Initialized the account
// Initializing the account authorizations
$$authorizations = $$account->authorizations();
if ($$authorizations instanceof authorizations) {
// Initialized the account authorizations
// Writing the account authorizations into the context variable
$$context->set('authorizations', $$authorizations);
// Continuation of the process
$$next($$context);
} else {
// Not initialized the account
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize your Telegram account authorizations*')
->then(function (message $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// 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 account) {
// Initialized the account
if ($$account->language instanceof language) {
// Initialized the language parameter
try {
// Writing the account language into the context variable
$$context->set('language', $$account->language);
} catch (error $$error) {
// Not initialized the language
// Writing the default language into the context variable
$$context->set('language', LANGUAGE_DEFAULT ?? language::en);
}
} else {
// Not initialized the language parameter
// Writing the default language into the context variable
$$context->set('language', LANGUAGE_DEFAULT ?? 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 $$message) use ($$context) {
// Sended the message
// 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 account) {
// 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)) {
// Initialized the localization
// Writing localization into the context variable
$$context->set('localization', $$localization);
// Continuation of the process
$$next($$context);
} else {
// Not initialized the localization
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function (message $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
} else {
// Not initialized language
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize language*')
->then(function (message $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// 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 account) {
// Initialized the account
// Initializing the account authorizations
$$authorizations = $$context->get('authorizations');
if ($$authorizations instanceof authorizations) {
// Initialized the account authorizations
if ($$authorizations->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 $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
}
} else {
// Not initialized the account authorizations
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize your Telegram account authorizations*')
->then(function (message $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// 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 account) {
// Initialized the account
// Initializing the account authorizations
$$authorizations = $$context->get('authorizations');
if ($$authorizations instanceof authorizations) {
// Initialized the account authorizations
if ($$authorizations->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 $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
}
} else {
// Not initialized the account
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize your Telegram account authorizations*')
->then(function (message $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
}
}

View File

@@ -0,0 +1,152 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\models\telegram\processes\language;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\core;
// Library for languages support
use mirzaev\languages\language;
// Baza database
use mirzaev\baza\record;
// Framework for Telegram
use Zanzara\Context as context,
Zanzara\Telegram\Type\Message as message;
/**
* Telegram language select
*
* @package ${REPO_OWNER}\${REPO_NAME}\models\telegram\processes\language
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class select extends core
{
/**
* Language
*
* Send the language choose menu
*
* @param context $$context Request data from Telegram
* @param string $$prefix Prefix for 'callback_data' (`$$prefix . $$language->name`)
* @param string $$title Title of the message
* @param string $$description Description of the message
* @param array $$exclude Languages that will be excluded ['ru', 'en'...]
*
* @return void
*/
public static function menu(context $$context, string $$prefix, string $$title, string $$description, array $$exclude = []): void
{
// Initializing the account
$$account = $$context->get('account');
if ($$account instanceof record) {
// Initialized the account
// Initializing language
$$language = $$context->get('language');
if ($$language) {
// Initialized language
// 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;
// Initializing buffer of languages
$$languages = language::cases();
// Deleting the actual language from buffer of languages
unset($$languages[array_search($$language, $$languages, strict: true)]);
// Sorting buffer of languages by the actual language
$$languages = [$$language, ...$$languages];
foreach ($$languages as $$language) {
// Iterating over languages
// Skipping excluded languages
if (array_search($$language->name, $$exclude, strict: true) !== false) continue;
// 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' => $$prefix . $$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 === 0 && empty($$keyboard[0]) ? 0 : ++$$row] = [
[
'text' => '🗂 ' . $$localization['select_language_button_add'],
'url' => 'https://git.svoboda.works/${REPO_OWNER}\${REPO_NAME}/src/branch/stable/kodorvan/neurobot/system/localizations'
]
];
// Sending the message
$$context->sendMessage(
$$title ?? '🌏 *' . $$localization['select_language_title'] . "*\n" . ($$description ?? $$localization['select_language_description']),
[
'reply_markup' => [
'inline_keyboard' => $$keyboard,
'disable_notification' => true
],
]
);
} else {
// Not initialized localization
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize localization*')
->then(function (message $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
} else {
// Not initialized language
// Sending the message
$$context->sendMessage('⚠️ *Failed to initialize language*')
->then(function (message $$message) use ($$context) {
// Sended the message
// 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 $$message) use ($$context) {
// Sended the message
// Ending the conversation process
$$context->endConversation();
});
}
}
}