From cd90d4f21cb5554be5473a90a9f021caaf046465 Mon Sep 17 00:00:00 2001 From: Luca Patera Date: Mon, 15 Jul 2024 19:55:31 +0200 Subject: [PATCH] Add XTR support to /donate command --- .../Conversations/DonateConversation.php | 130 ++++-------------- config/donation.php | 12 -- lang/en/donate.php | 3 +- lang/it/donate.php | 3 +- lang/localization.csv | 3 +- lang/pl/donate.php | 3 +- .../views/messages/donate/invalid.blade.php | 4 + .../donate/{menu.blade.php => main.blade.php} | 3 + .../views/messages/donate/telegram.blade.php | 1 - .../views/messages/donate/third.blade.php | 3 - 10 files changed, 39 insertions(+), 126 deletions(-) create mode 100644 resources/views/messages/donate/invalid.blade.php rename resources/views/messages/donate/{menu.blade.php => main.blade.php} (71%) delete mode 100644 resources/views/messages/donate/telegram.blade.php delete mode 100644 resources/views/messages/donate/third.blade.php diff --git a/app/Telegram/Conversations/DonateConversation.php b/app/Telegram/Conversations/DonateConversation.php index 8d3f122..3bbcdbc 100644 --- a/app/Telegram/Conversations/DonateConversation.php +++ b/app/Telegram/Conversations/DonateConversation.php @@ -2,134 +2,52 @@ namespace App\Telegram\Conversations; -use App\Facades\ImageUtils; -use JsonException; -use Psr\SimpleCache\InvalidArgumentException; -use SergiX44\Nutgram\Conversations\InlineMenu; +use SergiX44\Nutgram\Conversations\Conversation; use SergiX44\Nutgram\Nutgram; use SergiX44\Nutgram\Telegram\Properties\ParseMode; -use SergiX44\Nutgram\Telegram\Types\Internal\InputFile; -use SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardButton; +use SergiX44\Nutgram\Telegram\Types\Payment\LabeledPrice; -class DonateConversation extends InlineMenu +class DonateConversation extends Conversation { - /** - * Open donation menu - * @param Nutgram $bot - * @throws InvalidArgumentException - */ public function start(Nutgram $bot): void { - $this->menuText(message('donate.menu'), [ - 'parse_mode' => ParseMode::HTML, - 'disable_web_page_preview' => true, - ]); - - $this->clearButtons(); - $this->addButtonRow(InlineKeyboardButton::make('Telegram Payment', - callback_data: 'donate.telegram@menuTelegram')); - - foreach (config('donation.third_party_providers.url') as $service => $value) { - $this->addButtonRow(InlineKeyboardButton::make($service, url: $value)); - } - - foreach (config('donation.third_party_providers.text') as $service => $value) { - $this->addButtonRow(InlineKeyboardButton::make($service, - callback_data: "$service@menuThirdParty")); - } - - $this->addButtonRow(InlineKeyboardButton::make('❌ '.trans('common.close'), - callback_data: 'donate.cancel@end')); + $bot->sendMessage( + text: message('donate.main'), + parse_mode: ParseMode::HTML, + disable_web_page_preview: true, + ); - $this->showMenu(); + $this->next('getAmount'); stats('command.donate'); } - /** - * Telegram payment actions - * @throws InvalidArgumentException - */ - public function menuTelegram(): void + public function getAmount(Nutgram $bot): void { - $this->menuText(message('donate.telegram'), [ - 'parse_mode' => ParseMode::HTML, - 'disable_web_page_preview' => true, - ]); + //get the amount + $amount = (int)$bot->message()?->text; - $this->clearButtons(); - $this->addButtonRow( - InlineKeyboardButton::make('1$', callback_data: '1@donationInvoice'), - InlineKeyboardButton::make('5$', callback_data: '5@donationInvoice'), - InlineKeyboardButton::make('10$', callback_data: '10@donationInvoice'), - InlineKeyboardButton::make('25$', callback_data: '25@donationInvoice'), - InlineKeyboardButton::make('50$', callback_data: '50@donationInvoice'), - InlineKeyboardButton::make('100$', callback_data: '100@donationInvoice') - ); - - $this->addButtonRow(InlineKeyboardButton::make('🔙 '.trans('common.back'), - callback_data: 'donate.telegram.back@start')); + if ($amount < 1) { + $bot->sendMessage( + text: message('donate.invalid'), + parse_mode: ParseMode::HTML, + ); + $this->next('getAmount'); - $this->showMenu(); - - stats('donate.telegram'); - } - - /** - * Telegram invoice actions - * @param Nutgram $bot - * @param string $data - * @throws InvalidArgumentException - * @throws JsonException - */ - public function donationInvoice(Nutgram $bot, string $data): void - { - $value = (int)$data; + return; + } $this->bot->sendInvoice( title: trans('donate.donation'), description: trans('donate.support_by_donating'), payload: 'donation', - provider_token: config('donation.provider_token'), - currency: 'USD', - prices: [['label' => "{$value}$", 'amount' => $value * 100]] + provider_token: '', + currency: 'XTR', + prices: [LabeledPrice::make("$amount XTR", $amount)] ); $this->end(); - stats('donate.invoice', ['value' => $value]); - } - - /** - * Third-party providers actions - * @param Nutgram $bot - * @param string $service - * @throws InvalidArgumentException - */ - public function menuThirdParty(Nutgram $bot, string $service): void - { - $text = config("donation.third_party_providers.text.$service"); - - $photo = ImageUtils::qrcode($text, $service, true); - - $this->bot->sendPhoto( - photo: InputFile::make($photo), - caption: message('donate.third', [ - 'service' => $service, - 'text' => $text, - ]), - parse_mode: ParseMode::HTML, - ); - - $this->end(); - - stats('donate.third', ['service' => $service]); - } - - protected function closing(Nutgram $bot): void - { - parent::closing($bot); - - stats('donate.cancel'); + stats('donate.invoice', ['value' => $amount]); } } diff --git a/config/donation.php b/config/donation.php index 315a154..67c1d50 100644 --- a/config/donation.php +++ b/config/donation.php @@ -3,17 +3,5 @@ return [ 'enabled' => (int)env('DONATION_ENABLED', false), 'terms' => env('DONATION_TERMS'), - 'provider_token' => env('DONATION_PROVIDER_TOKEN'), - 'third_party_providers' => [ - 'url' => [ - 'Github Sponsor' => env('DONATION_THIRD_PARTY_PROVIDERS_URL_GITHUB'), - 'PayPal' => env('DONATION_THIRD_PARTY_PROVIDERS_URL_PAYPAL'), - ], - 'text' => [ - 'BTC' => env('DONATION_THIRD_PARTY_PROVIDERS_TEXT_BTC'), - 'ETH' => env('DONATION_THIRD_PARTY_PROVIDERS_TEXT_ETH'), - 'DOGE' => env('DONATION_THIRD_PARTY_PROVIDERS_TEXT_DOGE'), - ], - ], 'message_in_caption' => (bool)env('DONATION_MESSAGE_IN_CAPTION', false), ]; diff --git a/lang/en/donate.php b/lang/en/donate.php index 9225eea..a661d9d 100644 --- a/lang/en/donate.php +++ b/lang/en/donate.php @@ -4,11 +4,12 @@ 'donation' => 'Donation', 'support_by_donating' => 'Support this bot by donating', - 'select_amount' => 'Select the amount you wish to donate:', + 'select_amount' => 'Enter the number of Telegram Stars you would like to donate:', 'consider_donation' => 'If you want to support my work, please consider making a donation.', 'appreciated' => 'That\'ll be very much appreciated 😊', 'terms' => 'Terms and conditions for donation payments', 'thanks' => 'Thank you for your donation!', 'with' => 'Donate with :service', + 'invalid' => 'Please enter a number greater than 0.', ]; diff --git a/lang/it/donate.php b/lang/it/donate.php index 57bcbaa..c675ed3 100644 --- a/lang/it/donate.php +++ b/lang/it/donate.php @@ -4,11 +4,12 @@ 'donation' => 'Donazione', 'support_by_donating' => 'Supporta questo bot donando', - 'select_amount' => 'Seleziona la quantità che desideri donare:', + 'select_amount' => 'Inserisci il numero di Telegram Stars che desideri donare:', 'consider_donation' => 'Se vuoi supportare il mio lavoro, per favore considera una donazione.', 'appreciated' => 'Verrà molto apprezzato 😊', 'terms' => 'Termini e condizioni per le donazioni', 'thanks' => 'Grazie per la tua donazione!', 'with' => 'Dona con :service', + 'invalid' => 'Inserire un numero maggiore di 0.', ]; diff --git a/lang/localization.csv b/lang/localization.csv index 2d7f159..c2fbe30 100644 --- a/lang/localization.csv +++ b/lang/localization.csv @@ -40,7 +40,7 @@ feedback,thanks,Thank you for your feedback!,Grazie per il tuo feedback!,Dzięku feedback,wrong,Invalid content. Please send only text.,Contenuto invalido. Per favore inserisci solo del testo.,Nieprawidłowa zawartość. Proszę wprowadzić tylko tekst. donate,donation,Donation,Donazione,Darowizna donate,support_by_donating,Support this bot by donating,Supporta questo bot donando,Wspomóż tego bota poprzez dotację -donate,select_amount,Select the amount you wish to donate:,Seleziona la quantità che desideri donare:,"Wybierz kwotę, jaką chcesz przekazać:" +donate,select_amount,Enter the number of Telegram Stars you would like to donate:,Inserisci il numero di Telegram Stars che desideri donare:,"Wprowadź liczbę gwiazdek Telegram, które chcesz przekazać:" donate,consider_donation,"If you want to support my work, please consider making a donation.","Se vuoi supportare il mio lavoro, per favore considera una donazione.","Jeśli chcesz wesprzeć moją pracę, rozważ przekazanie darowizny." donate,appreciated,That'll be very much appreciated 😊,Verrà molto apprezzato 😊,To będzie bardzo miłe 😊 donate,terms,Terms and conditions for donation payments,Termini e condizioni per le donazioni,Zasady i warunki płatności darowizn @@ -93,3 +93,4 @@ stats,range.year,This year: :value,Quest'anno: :value, stats,range.yesterday,Yesterday: :value,Ieri: :value, settings,trim.enable,✂️ Trim transparent areas,✂️ Ritaglia aree trasparenti, settings,trim.disable,✂️ Do not trim transparent areas,✂️ Non ritagliare aree trasparenti, +donate,invalid,Please enter a number greater than 0.,Inserire un numero maggiore di 0.,Wprowadź liczbę większą niż 0. diff --git a/lang/pl/donate.php b/lang/pl/donate.php index 47703fa..bf11bc3 100644 --- a/lang/pl/donate.php +++ b/lang/pl/donate.php @@ -4,11 +4,12 @@ 'donation' => 'Darowizna', 'support_by_donating' => 'Wspomóż tego bota poprzez dotację', - 'select_amount' => 'Wybierz kwotę, jaką chcesz przekazać:', + 'select_amount' => 'Wprowadź liczbę gwiazdek Telegram, które chcesz przekazać:', 'consider_donation' => 'Jeśli chcesz wesprzeć moją pracę, rozważ przekazanie darowizny.', 'appreciated' => 'To będzie bardzo miłe 😊', 'terms' => 'Zasady i warunki płatności darowizn', 'thanks' => 'Dziękujemy za wsparcie!', 'with' => 'Wesprzyj za pomocą :service', + 'invalid' => 'Wprowadź liczbę większą niż 0.', ]; diff --git a/resources/views/messages/donate/invalid.blade.php b/resources/views/messages/donate/invalid.blade.php new file mode 100644 index 0000000..da368bf --- /dev/null +++ b/resources/views/messages/donate/invalid.blade.php @@ -0,0 +1,4 @@ +@lang('donate.invalid')
+
+@lang('donate.select_amount') + diff --git a/resources/views/messages/donate/menu.blade.php b/resources/views/messages/donate/main.blade.php similarity index 71% rename from resources/views/messages/donate/menu.blade.php rename to resources/views/messages/donate/main.blade.php index 08be48b..452b2f5 100644 --- a/resources/views/messages/donate/menu.blade.php +++ b/resources/views/messages/donate/main.blade.php @@ -1,7 +1,10 @@ +@lang('donate.donation')
@lang('donate.consider_donation')
@lang('donate.appreciated')
@if(!empty(config('donation.terms')))
@lang('donate.terms') @endif +

+@lang('donate.select_amount') diff --git a/resources/views/messages/donate/telegram.blade.php b/resources/views/messages/donate/telegram.blade.php deleted file mode 100644 index af16b9a..0000000 --- a/resources/views/messages/donate/telegram.blade.php +++ /dev/null @@ -1 +0,0 @@ -@lang('donate.select_amount') diff --git a/resources/views/messages/donate/third.blade.php b/resources/views/messages/donate/third.blade.php deleted file mode 100644 index ae0f6a1..0000000 --- a/resources/views/messages/donate/third.blade.php +++ /dev/null @@ -1,3 +0,0 @@ -@lang('donate.with',['service'=>$service]) -
-
{{$text}}