diff --git a/app/Models/Chat.php b/app/Models/Chat.php
index 8f7c735..88c30c0 100644
--- a/app/Models/Chat.php
+++ b/app/Models/Chat.php
@@ -2,7 +2,6 @@
namespace App\Models;
-use App\Rules\HexColorRule;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -83,9 +82,9 @@ public function settingsRules(): array
'watermark.opacity' => 'integer|min:0|max:100',
'watermark.text.content' => 'nullable|max:100',
'watermark.text.size' => 'integer|min:1|max:100',
- 'watermark.text.color' => [new HexColorRule],
+ 'watermark.text.color' => 'hex_color',
'watermark.border.size' => 'integer|min:0|max:10',
- 'watermark.border.color' => [new HexColorRule],
+ 'watermark.border.color' => 'hex_color',
];
}
diff --git a/app/Models/Statistic.php b/app/Models/Statistic.php
index 8f22b60..e8c9548 100644
--- a/app/Models/Statistic.php
+++ b/app/Models/Statistic.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use Carbon\CarbonImmutable;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
@@ -43,38 +44,109 @@ public function chat(): BelongsTo
public static function getStatsForBot(): array
{
- $date = now();
+ $date = CarbonImmutable::now();
- $stickersNew = self::query()
+ // stickers optimized
+ $stickersOptimizedYesterday = self::query()
->where('action', 'sticker')
->where('category', 'optimized')
- ->whereDate('collected_at', $date->toDateString())
+ ->whereBetween('collected_at', [$date->subDay()->startOfDay(), $date->subDay()->endOfDay()])
->count();
-
- $stickersTotal = self::query()
+ $stickersOptimizedToday = self::query()
->where('action', 'sticker')
->where('category', 'optimized')
+ ->whereBetween('collected_at', [$date->startOfDay(), $date->endOfDay()])
->count();
-
- $usersNewToday = Chat::query()
- ->whereDate('created_at', $date->toDateString())
+ $stickersOptimizedWeek = self::query()
+ ->where('action', 'sticker')
+ ->where('category', 'optimized')
+ ->whereBetween('collected_at', [$date->startOfWeek(), $date->endOfWeek()])
+ ->count();
+ $stickersOptimizedMonth = self::query()
+ ->where('action', 'sticker')
+ ->where('category', 'optimized')
+ ->whereBetween('collected_at', [$date->startOfMonth(), $date->endOfMonth()])
+ ->count();
+ $stickersOptimizedYear = self::query()
+ ->where('action', 'sticker')
+ ->where('category', 'optimized')
+ ->whereBetween('collected_at', [$date->startOfYear(), $date->endOfYear()])
+ ->count();
+ $stickersOptimizedTotal = self::query()
+ ->where('action', 'sticker')
+ ->where('category', 'optimized')
->count();
- $usersActiveToday = self::query()
+ //active users
+ $activeUsersYesterday = self::query()
+ ->distinct()
+ ->whereBetween('collected_at', [$date->subDay()->startOfDay(), $date->subDay()->endOfDay()])
+ ->whereNotNull('chat_id')
+ ->count('chat_id');
+ $activeUsersToday = self::query()
+ ->distinct()
+ ->whereBetween('collected_at', [$date->startOfDay(), $date->endOfDay()])
+ ->whereNotNull('chat_id')
+ ->count('chat_id');
+ $activeUsersWeek = self::query()
->distinct()
- ->whereDate('collected_at', $date->toDateString())
+ ->whereBetween('collected_at', [$date->startOfWeek(), $date->endOfWeek()])
+ ->whereNotNull('chat_id')
+ ->count('chat_id');
+ $activeUsersMonth = self::query()
+ ->distinct()
+ ->whereBetween('collected_at', [$date->startOfMonth(), $date->endOfMonth()])
+ ->whereNotNull('chat_id')
+ ->count('chat_id');
+ $activeUsersYear = self::query()
+ ->distinct()
+ ->whereBetween('collected_at', [$date->startOfYear(), $date->endOfYear()])
->whereNotNull('chat_id')
->count('chat_id');
+ // users
+ $usersYesterday = Chat::query()
+ ->whereBetween('created_at', [$date->subDay()->startOfDay(), $date->subDay()->endOfDay()])
+ ->count();
+ $usersToday = Chat::query()
+ ->whereBetween('created_at', [$date->startOfDay(), $date->endOfDay()])
+ ->count();
+ $usersWeek = Chat::query()
+ ->whereBetween('created_at', [$date->startOfWeek(), $date->endOfWeek()])
+ ->count();
+ $usersMonth = Chat::query()
+ ->whereBetween('created_at', [$date->startOfMonth(), $date->endOfMonth()])
+ ->count();
+ $usersYear = Chat::query()
+ ->whereBetween('created_at', [$date->startOfYear(), $date->endOfYear()])
+ ->count();
$usersTotal = Chat::count();
return [
- 'stickers_new' => number_format($stickersNew, thousands_separator: '˙'),
- 'stickers_total' => number_format($stickersTotal, thousands_separator: '˙'),
- 'users_new_today' => number_format($usersNewToday, thousands_separator: '˙'),
- 'users_active_today' => number_format($usersActiveToday, thousands_separator: '˙'),
- 'users_total' => number_format($usersTotal, thousands_separator: '˙'),
- 'last_update' => now()->format('Y-m-d H:i:s e'),
+ 'stickers_optimized' => [
+ 'yesterday' => number_format($stickersOptimizedYesterday, thousands_separator: '˙'),
+ 'today' => number_format($stickersOptimizedToday, thousands_separator: '˙'),
+ 'week' => number_format($stickersOptimizedWeek, thousands_separator: '˙'),
+ 'month' => number_format($stickersOptimizedMonth, thousands_separator: '˙'),
+ 'year' => number_format($stickersOptimizedYear, thousands_separator: '˙'),
+ 'total' => number_format($stickersOptimizedTotal, thousands_separator: '˙'),
+ ],
+ 'active_users' => [
+ 'yesterday' => number_format($activeUsersYesterday, thousands_separator: '˙'),
+ 'today' => number_format($activeUsersToday, thousands_separator: '˙'),
+ 'week' => number_format($activeUsersWeek, thousands_separator: '˙'),
+ 'month' => number_format($activeUsersMonth, thousands_separator: '˙'),
+ 'year' => number_format($activeUsersYear, thousands_separator: '˙'),
+ ],
+ 'users' => [
+ 'yesterday' => number_format($usersYesterday, thousands_separator: '˙'),
+ 'today' => number_format($usersToday, thousands_separator: '˙'),
+ 'week' => number_format($usersWeek, thousands_separator: '˙'),
+ 'month' => number_format($usersMonth, thousands_separator: '˙'),
+ 'year' => number_format($usersYear, thousands_separator: '˙'),
+ 'total' => number_format($usersTotal, thousands_separator: '˙'),
+ ],
+ 'last_update' => $date->format('Y-m-d H:i:s e'),
];
}
}
diff --git a/app/Rules/HexColorRule.php b/app/Rules/HexColorRule.php
deleted file mode 100644
index 061ccc1..0000000
--- a/app/Rules/HexColorRule.php
+++ /dev/null
@@ -1,16 +0,0 @@
-sendMessage(
+ text: message('stats.empty'),
+ parse_mode: ParseMode::HTML,
+ );
+
+ return;
+ }
+
$bot->sendMessage(
- text: $this->getMessage(),
+ text: $this->getMessage($data, 'stickers_optimized'),
parse_mode: ParseMode::HTML,
- disable_web_page_preview: true,
+ reply_markup: $this->getKeyboard(),
);
- stats('stats', 'command');
+ stats('command.stats');
}
- protected function getMessage(): string
+ public function updateStatsMessage(Nutgram $bot, string $value)
{
$data = Cache::get('stats');
if ($data === null) {
- return message('stats.empty');
+ $bot->editMessageText(
+ text: message('stats.empty'),
+ parse_mode: ParseMode::HTML,
+ );
+
+ return;
}
- return message('stats.full', $data);
+ $bot->editMessageText(
+ text: $this->getMessage($data, $value),
+ parse_mode: ParseMode::HTML,
+ reply_markup: $this->getKeyboard(),
+ );
+
+ $bot->answerCallbackQuery();
+ }
+
+ protected function getMessage(array $data, string $value): string
+ {
+ $title = match ($value) {
+ 'stickers_optimized' => __('stats.category.optimized'),
+ 'active_users' => __('stats.category.active_users'),
+ 'users' => __('stats.category.new_users'),
+ };
+
+ return message('stats.template', [
+ 'title' => $title,
+ ...$data[$value],
+ 'lastUpdate' => $data['last_update'],
+ ]);
+ }
+
+ protected function getKeyboard(): InlineKeyboardMarkup
+ {
+ return InlineKeyboardMarkup::make()
+ ->addRow(
+ InlineKeyboardButton::make(__('stats.category.optimized'), callback_data: 'stats:stickers_optimized'),
+ )->addRow(
+ InlineKeyboardButton::make(__('stats.category.active_users'), callback_data: 'stats:active_users'),
+ InlineKeyboardButton::make(__('stats.category.new_users'), callback_data: 'stats:users'),
+ );
}
}
diff --git a/app/Telegram/Exceptions/MessageNotModifiedException.php b/app/Telegram/Exceptions/MessageNotModifiedException.php
index 8720445..d126c26 100644
--- a/app/Telegram/Exceptions/MessageNotModifiedException.php
+++ b/app/Telegram/Exceptions/MessageNotModifiedException.php
@@ -9,4 +9,9 @@
class MessageNotModifiedException extends ApiException
{
public static ?string $pattern = '.*specified new message content and reply markup are exactly the same.*';
+
+ public function __invoke(Nutgram $bot, TelegramException $e)
+ {
+ //ignore this exception
+ }
}
diff --git a/app/Telegram/Exceptions/MessageToDeleteNotFoundException.php b/app/Telegram/Exceptions/MessageToDeleteNotFoundException.php
index d15ac7d..44db2ee 100644
--- a/app/Telegram/Exceptions/MessageToDeleteNotFoundException.php
+++ b/app/Telegram/Exceptions/MessageToDeleteNotFoundException.php
@@ -9,4 +9,9 @@
class MessageToDeleteNotFoundException extends ApiException
{
public static ?string $pattern = '.*message to delete not found.*';
+
+ public function __invoke(Nutgram $bot, TelegramException $e)
+ {
+ //ignore this exception
+ }
}
diff --git a/app/Telegram/Exceptions/MessageToEditNotFoundException.php b/app/Telegram/Exceptions/MessageToEditNotFoundException.php
index c3c010c..ffe2ea6 100644
--- a/app/Telegram/Exceptions/MessageToEditNotFoundException.php
+++ b/app/Telegram/Exceptions/MessageToEditNotFoundException.php
@@ -9,4 +9,9 @@
class MessageToEditNotFoundException extends ApiException
{
public static ?string $pattern = '.*message to edit not found.*';
+
+ public function __invoke(Nutgram $bot, TelegramException $e)
+ {
+ //ignore this exception
+ }
}
diff --git a/composer.lock b/composer.lock
index 82c2f22..91e39c3 100644
--- a/composer.lock
+++ b/composer.lock
@@ -108,16 +108,16 @@
},
{
"name": "aws/aws-sdk-php",
- "version": "3.287.1",
+ "version": "3.288.0",
"source": {
"type": "git",
"url": "https://github.com/aws/aws-sdk-php.git",
- "reference": "efe7b6e370cf12af6c2ee9503aa2aba61350ed03"
+ "reference": "6485aad8d3cfa55e3bcea2b4b347f86ee233fc33"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/efe7b6e370cf12af6c2ee9503aa2aba61350ed03",
- "reference": "efe7b6e370cf12af6c2ee9503aa2aba61350ed03",
+ "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/6485aad8d3cfa55e3bcea2b4b347f86ee233fc33",
+ "reference": "6485aad8d3cfa55e3bcea2b4b347f86ee233fc33",
"shasum": ""
},
"require": {
@@ -197,9 +197,9 @@
"support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues",
- "source": "https://github.com/aws/aws-sdk-php/tree/3.287.1"
+ "source": "https://github.com/aws/aws-sdk-php/tree/3.288.0"
},
- "time": "2023-11-20T20:12:54+00:00"
+ "time": "2023-11-21T19:08:29+00:00"
},
{
"name": "brick/math",
@@ -1576,16 +1576,16 @@
},
{
"name": "laravel/framework",
- "version": "v10.32.1",
+ "version": "v10.33.0",
"source": {
"type": "git",
"url": "https://github.com/laravel/framework.git",
- "reference": "b30e44f20d244f7ba125283e14a8bbac167f4e5b"
+ "reference": "4536872e3e5b6be51b1f655dafd12c9a4fa0cfe8"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/laravel/framework/zipball/b30e44f20d244f7ba125283e14a8bbac167f4e5b",
- "reference": "b30e44f20d244f7ba125283e14a8bbac167f4e5b",
+ "url": "https://api.github.com/repos/laravel/framework/zipball/4536872e3e5b6be51b1f655dafd12c9a4fa0cfe8",
+ "reference": "4536872e3e5b6be51b1f655dafd12c9a4fa0cfe8",
"shasum": ""
},
"require": {
@@ -1774,7 +1774,7 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
- "time": "2023-11-14T22:57:08+00:00"
+ "time": "2023-11-21T14:49:31+00:00"
},
{
"name": "laravel/prompts",
@@ -8769,16 +8769,16 @@
},
{
"name": "laravel/sail",
- "version": "v1.26.0",
+ "version": "v1.26.1",
"source": {
"type": "git",
"url": "https://github.com/laravel/sail.git",
- "reference": "c60fe037004e272efd0d81f416ed2bfc623d70b4"
+ "reference": "7a82f5aa364dbee3fd9c52fc464cf0bdd11150ed"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/laravel/sail/zipball/c60fe037004e272efd0d81f416ed2bfc623d70b4",
- "reference": "c60fe037004e272efd0d81f416ed2bfc623d70b4",
+ "url": "https://api.github.com/repos/laravel/sail/zipball/7a82f5aa364dbee3fd9c52fc464cf0bdd11150ed",
+ "reference": "7a82f5aa364dbee3fd9c52fc464cf0bdd11150ed",
"shasum": ""
},
"require": {
@@ -8830,7 +8830,7 @@
"issues": "https://github.com/laravel/sail/issues",
"source": "https://github.com/laravel/sail"
},
- "time": "2023-10-18T13:57:15+00:00"
+ "time": "2023-11-20T15:56:47+00:00"
},
{
"name": "loilo/fuse",
diff --git a/lang/en/stats.php b/lang/en/stats.php
index 4114f8f..78e4cb2 100644
--- a/lang/en/stats.php
+++ b/lang/en/stats.php
@@ -2,19 +2,21 @@
return [
- 'title' => '📊 Statistics',
- 'stickers' => [
- 'today' => '🆕 Stickers optimized today: :value',
- 'total' => '🖼 Total stickers optimized: :value',
+ 'title' => '📊 Statistics',
+ 'category' => [
+ 'optimized' => '🖼 Stickers optimized',
+ 'new_users' => '👥 Users',
+ 'active_users' => '✳️ Active users',
],
- 'users' => [
- 'today' => [
- 'new' => '🆕 New users today: :value',
- 'active' => '✳️ Active users today: :value',
- ],
- 'total' => '👥 Total users: :value',
- ],
- 'last_update' => 'Last update: :value',
+ 'last_update' => 'Last update:',
'wait' => 'Please wait next statistics update.',
+ 'range' => [
+ 'month' => 'This month: :value',
+ 'today' => 'Today: :value',
+ 'total' => 'Total: :value',
+ 'week' => 'This week: :value',
+ 'year' => 'This year: :value',
+ 'yesterday' => 'Yesterday: :value',
+ ],
];
diff --git a/lang/it/stats.php b/lang/it/stats.php
index 4ff0d4e..c924653 100644
--- a/lang/it/stats.php
+++ b/lang/it/stats.php
@@ -2,19 +2,21 @@
return [
- 'title' => '📊 Statistiche',
- 'stickers' => [
- 'today' => '🆕 Sticker ottimizzati oggi: :value',
- 'total' => '🖼 Totale sticker ottimizzati: :value',
+ 'title' => '📊 Statistiche',
+ 'category' => [
+ 'optimized' => '🖼 Sticker ottimizzati',
+ 'new_users' => '👥 Utenti',
+ 'active_users' => '✳️ Utenti attivi',
],
- 'users' => [
- 'today' => [
- 'new' => '🆕 Nuovi utenti oggi: :value',
- 'active' => '✳️ Utenti attivi oggi: :value',
- ],
- 'total' => '👥 Utenti totali: :value',
- ],
- 'last_update' => 'Ultimo aggiornamento: :value',
+ 'last_update' => 'Ultimo aggiornamento:',
'wait' => 'Attendi il prossimo aggiornamento delle statistiche.',
+ 'range' => [
+ 'month' => 'Questo mese: :value',
+ 'today' => 'Oggi: :value',
+ 'total' => 'Totale: :value',
+ 'week' => 'Questa settimana: :value',
+ 'year' => 'Quest\'anno: :value',
+ 'yesterday' => 'Ieri: :value',
+ ],
];
diff --git a/lang/localization.csv b/lang/localization.csv
index 6322f34..a7a2405 100644
--- a/lang/localization.csv
+++ b/lang/localization.csv
@@ -46,13 +46,11 @@ donate,appreciated,That'll be very much appreciated 😊,Verrà molto apprezzato
donate,terms,Terms and conditions for donation payments,Termini e condizioni per le donazioni,Zasady i warunki płatności darowizn
donate,thanks,Thank you for your donation!,Grazie per la tua donazione!,Dziękujemy za wsparcie!
donate,with,Donate with :service,Dona con :service,Wesprzyj za pomocą :service
-stats,title,📊 Statistics,📊 Statistiche,📊 Statystyki
-stats,stickers.today,🆕 Stickers optimized today: :value,🆕 Sticker ottimizzati oggi: :value,🆕 Naklejki zoptymalizowane dziś: :value
-stats,stickers.total,🖼 Total stickers optimized: :value,🖼 Totale sticker ottimizzati: :value,🖼 Wszystkie zoptymalizowane naklejki: :value
-stats,users.today.new,🆕 New users today: :value,🆕 Nuovi utenti oggi: :value,🆕 Nowi użytkownicy dzisiaj: :value
-stats,users.today.active,✳️ Active users today: :value,✳️ Utenti attivi oggi: :value,✳️ Aktywni użytkownicy dzisiaj: :value
-stats,users.total,👥 Total users: :value,👥 Utenti totali: :value,👥 Wszyscy użytkownicy: :value
-stats,last_update,Last update: :value,Ultimo aggiornamento: :value,Ostatnia aktualizacja: :value
+stats,title,📊 Statistics,📊 Statistiche,📊 Statystyki
+stats,category.optimized,🖼 Stickers optimized,🖼 Sticker ottimizzati,🖼 Naklejki zoptymalizowane
+stats,category.new_users,👥 Users,👥 Utenti,👥 Wszyscy
+stats,category.active_users,✳️ Active users,✳️ Utenti attivi,✳️ Aktywni użytkownicy
+stats,last_update,Last update:,Ultimo aggiornamento:,Ostatnia aktualizacja:
stats,wait,Please wait next statistics update.,Attendi il prossimo aggiornamento delle statistiche.,Proszę czekać na kolejną aktualizację statystyk.
settings,back,🔙 Back,🔙 Indietro,🔙 Powrót
settings,description,Here you can change the bot settings.,Qui puoi cambiare le impostazioni del bot.,Tutaj możesz zmienić ustawienia bota.
@@ -87,3 +85,9 @@ watermark,border.size.send,Enter the border size (0-10):,Inserisci la dimensione
watermark,border.color.set,🖍 Set border color,🖍 Imposta colore bordo,🖍 Ustaw kolor obramowania
watermark,border.color.send,Enter the border color (HEX):,Inserisci il colore del bordo (HEX):,Wprowadź kolor obramowania (HEX):
watermark,to_disable,"To disable the watermark, set the opacity to 0.","Per disabilitare il watermark, imposta l'opacità a 0.",
+stats,range.month,This month: :value,Questo mese: :value,
+stats,range.today,Today: :value,Oggi: :value,
+stats,range.total,Total: :value,Totale: :value,
+stats,range.week,This week: :value,Questa settimana: :value,
+stats,range.year,This year: :value,Quest'anno: :value,
+stats,range.yesterday,Yesterday: :value,Ieri: :value,
diff --git a/lang/pl/stats.php b/lang/pl/stats.php
index 1eb9f73..a9149ac 100644
--- a/lang/pl/stats.php
+++ b/lang/pl/stats.php
@@ -2,19 +2,13 @@
return [
- 'title' => '📊 Statystyki',
- 'stickers' => [
- 'today' => '🆕 Naklejki zoptymalizowane dziś: :value',
- 'total' => '🖼 Wszystkie zoptymalizowane naklejki: :value',
+ 'title' => '📊 Statystyki',
+ 'category' => [
+ 'optimized' => '🖼 Naklejki zoptymalizowane',
+ 'new_users' => '👥 Wszyscy',
+ 'active_users' => '✳️ Aktywni użytkownicy',
],
- 'users' => [
- 'today' => [
- 'new' => '🆕 Nowi użytkownicy dzisiaj: :value',
- 'active' => '✳️ Aktywni użytkownicy dzisiaj: :value',
- ],
- 'total' => '👥 Wszyscy użytkownicy: :value',
- ],
- 'last_update' => 'Ostatnia aktualizacja: :value',
+ 'last_update' => 'Ostatnia aktualizacja:',
'wait' => 'Proszę czekać na kolejną aktualizację statystyk.',
];
diff --git a/resources/views/messages/stats/full.blade.php b/resources/views/messages/stats/full.blade.php
deleted file mode 100644
index 14dd72a..0000000
--- a/resources/views/messages/stats/full.blade.php
+++ /dev/null
@@ -1,11 +0,0 @@
-@lang('stats.title')
-
-@lang('stats.stickers.today',['value'=>$stickers_new])
-@lang('stats.stickers.total',['value'=>$stickers_total])
-
-@lang('stats.users.today.new',['value'=>$users_new_today])
-@lang('stats.users.today.active',['value'=>$users_active_today])
-
-@lang('stats.users.total',['value'=>$users_total])
-
-@lang('stats.last_update',['value'=>$last_update])
diff --git a/resources/views/messages/stats/template.blade.php b/resources/views/messages/stats/template.blade.php
new file mode 100644
index 0000000..da5d713
--- /dev/null
+++ b/resources/views/messages/stats/template.blade.php
@@ -0,0 +1,13 @@
+@lang('stats.title') > {{ $title }}
+
+@lang('stats.range.yesterday', ['value' => $yesterday])
+@lang('stats.range.today', ['value' => $today])
+@lang('stats.range.week', ['value' => $week])
+@lang('stats.range.month', ['value' => $month])
+@lang('stats.range.year', ['value' => $year])
+@isset($total)
+@lang('stats.range.total', ['value' => $total])
+@endisset
+
+@lang('stats.last_update')
+{{ $lastUpdate }}
diff --git a/routes/telegram.php b/routes/telegram.php
index 7333bcb..94125a0 100644
--- a/routes/telegram.php
+++ b/routes/telegram.php
@@ -3,6 +3,7 @@
/** @var Nutgram $bot */
use App\Telegram\Commands;
+use App\Telegram\Commands\StatsCommand;
use App\Telegram\Conversations\DonateConversation;
use App\Telegram\Exceptions;
use App\Telegram\Handlers;
@@ -31,6 +32,7 @@
$bot->onPhoto(Handlers\PhotoHandler::class);
$bot->onPreCheckoutQuery(Handlers\PreCheckoutQueryHandler::class);
$bot->onSuccessfulPayment(Handlers\SuccessfulPaymentHandler::class);
+$bot->onCallbackQueryData('stats:{value}', [StatsCommand::class, 'updateStatsMessage']);
/*
|--------------------------------------------------------------------------