diff --git a/app/Helpers/BotHelper.php b/app/Helpers/BotHelper.php index 307192f7..5333baac 100644 --- a/app/Helpers/BotHelper.php +++ b/app/Helpers/BotHelper.php @@ -172,6 +172,24 @@ public static function sendKeyboardMessage(Telegram $messenger, string $message, $messenger->sendMessage($content); } + /** + * @param Telegram $messenger + * @param string $message + * @param $keyboard + * @param $chat_id + * @return void + */ + public static function sendKeyboardMessageToChatId(Telegram $messenger, string $message, $keyboard, $chat_id): void + { + $content = [ + 'chat_id' => $chat_id, + 'text' => $message, + 'reply_markup' => $keyboard + ]; + + $messenger->sendMessage($content); + } + /** * @param $messenger @@ -407,7 +425,15 @@ public static function send1button(Telegram $messenger, $array): void ); $inlineKeyboard = $messenger->buildInlineKeyBoard($option); self::sendKeyboardMessage($messenger, $array[0][0], $inlineKeyboard); + } + public static function send1buttonToChatId(Telegram $messenger, $array, $chat_id): void + { + $option = array( + array($messenger->buildInlineKeyBoardButton($array[0][0], callback_data: $array[0][1])) + ); + $inlineKeyboard = $messenger->buildInlineKeyBoard($option); + self::sendKeyboardMessageToChatId($messenger, $array[0][0], $inlineKeyboard, $chat_id); } public static function send1buttonWithMessage(Telegram $messenger, $message, $array): void diff --git a/app/Helpers/QuranHelper.php b/app/Helpers/QuranHelper.php index ce3b6f61..46b9972e 100644 --- a/app/Helpers/QuranHelper.php +++ b/app/Helpers/QuranHelper.php @@ -2,6 +2,7 @@ namespace App\Helpers; +use App; use App\Models\BotUsers; use App\Models\QuranAyat; use App\Models\QuranSurah; @@ -756,6 +757,29 @@ public static function sendScanBaleButtons(int $pageNumber, mixed $token, Telegr BotHelper::messageWithKeyboard($token, $bot->ChatID(), $message, $inlineKeyboard); } + + public static function isContainSureAyahCommand($message): bool + { + $regex = '/ \/sure[0-9]+ayah[0-9]+ /'; + return StringHelper::isContainRegex($message, $regex); + } + + + public static function getCommandByRegex(string $message): array + { + $commandTemplateSure = '/sure'; + $commandTemplateAyah = 'ayah'; + + $regex = '/ \/sure[0-9]+ayah[0-9]+ /'; + [$sure, $aya] = StringHelper::getCommandByRegex($message, $regex); + + $command = $commandTemplateSure . $sure . $commandTemplateAyah . $aya; + if (App::runningUnitTests()) + $message = trans("bot.surah number:") . $sure . ":" . trans("bot.ayah") . " : " . $aya; + + return [$command, $message]; + } + /** * @param Telegram $bot * @param $token @@ -986,7 +1010,6 @@ private static function getCommandNextPage(string $searchPhrase, int $nextPage): //" . $searchPhrase . "page=" . $nextPage; } - } diff --git a/app/Helpers/StringHelper.php b/app/Helpers/StringHelper.php index f33f8ec5..6bea13a6 100644 --- a/app/Helpers/StringHelper.php +++ b/app/Helpers/StringHelper.php @@ -3,6 +3,7 @@ namespace App\Helpers; use Carbon\Carbon; +use Illuminate\Support\Str; class StringHelper { @@ -152,4 +153,26 @@ public static function get3digitNumber(int $pageNumber): string { return str_pad($pageNumber, 3, '0', STR_PAD_LEFT); } + + public static function isContainRegex(string $message, $regex): bool + { + $check = preg_match($regex, $message); + return $check; + } + + public static function getCommandByRegex(string $message, $regex): array + { + $commandTemplateAyah = 'ayah'; + if (preg_match('/sure(.*?)ayah/', substr($message, 1, Str::length($message)), $match) == 1) { + $sure = (integer)$match[1]; + if ($sure > 0) { + $aya = (integer)substr($message, strpos($message, $commandTemplateAyah) + Str::length($commandTemplateAyah)); + if ($aya > 0) { + return [$sure, $aya]; + } + } + } + + return [0, 0]; + } } diff --git a/app/Http/Controllers/QuranWordController.php b/app/Http/Controllers/QuranWordController.php index 95623754..826272bd 100644 --- a/app/Http/Controllers/QuranWordController.php +++ b/app/Http/Controllers/QuranWordController.php @@ -428,7 +428,6 @@ public function index(BotRequest $request) if ($isStartCommandShow) { - $array = [[trans("bot.return to command list"), "/start"]]; $message = $array[0][0]; if ($type == 'telegram') { @@ -460,6 +459,7 @@ public function index(BotRequest $request) /** * Show the form for creating a new resource. + * @throws GuzzleException */ public function messageToAll(BotRequest $request) @@ -492,13 +492,37 @@ function messageToAll(BotRequest $request) $botBale = new Telegram(env('QURAN_HEFZ_BOT_TOKEN_BALE'), 'bale'); $botTelegram = new Telegram(env('QURAN_HEFZ_BOT_TOKEN_TELEGRAM'), 'telegram'); - $logs = BotLog::where('created_at', '>=', Carbon::now()->subDay(500))->whereLanguage('fa')->select('chat_id', 'type')->distinct('chat_id')->get(); + + $logs = BotLog::where('created_at', '>=', Carbon::now()->subDay(500)) + ->whereLanguage('fa') + ->select('chat_id', 'type') + ->distinct('chat_id') + ->get(); + foreach ($logs as $log) { $count = $logs->count(); - if ($log['type'] == 'bale') - BotHelper::sendMessageByChatId($botBale, $log['chat_id'], $message); - else - BotHelper::sendMessageByChatId($botTelegram, $log['chat_id'], $message); + try { + + if ($log['type'] == 'bale') { + if (QuranHelper::isContainSureAyahCommand($message)) { + [$command, $messageButton] = QuranHelper::getCommandByRegex($message); + $array = [[$messageButton, $command]]; + $inlineKeyboard = BotHelper::makeBaleKeyboard1button($array); + BotHelper::messageWithKeyboard($token, $log['chat_id'], $message, $inlineKeyboard); + } else { + BotHelper::sendMessageByChatId($botBale, $log['chat_id'], $message); + } + } else { + BotHelper::sendMessageByChatId($botTelegram, $log['chat_id'], $message); + if (QuranHelper::isContainSureAyahCommand($message)) { + [$command, $messageButton] = QuranHelper::getCommandByRegex($message); + $array = [[$messageButton, $command]]; + BotHelper::send1buttonToChatId($botTelegram, $array, $log['chat_id']); + } + } + } catch (\Exception $exception) { + Log::info($exception->getMessage()); + } } } BotHelper::sendMessage($bot, trans("bot.sent it for :count person", ["count" => $count])); diff --git a/tests/Unit/QuranHelperTest.php b/tests/Unit/QuranHelperTest.php index 40b14d7f..c8d8da45 100644 --- a/tests/Unit/QuranHelperTest.php +++ b/tests/Unit/QuranHelperTest.php @@ -2,6 +2,7 @@ use App\Helpers\QuranHelper; +use App\Helpers\StringHelper; use PHPUnit\Framework\TestCase; use Saber13812002\Laravel\Fulltext\IndexedRecord; use function PHPUnit\Framework\assertEquals; @@ -56,4 +57,46 @@ public function test_pagination_command() assertEquals("الرحمان", $searchPhrase); } + + + public function test_is_command_exist_in_this_message() + { + $message = " adsfasdf /sure233ayah234 asdfasdfha"; + $isTrue = QuranHelper::isContainSureAyahCommand($message); + self::assertEquals($isTrue, true); + $regex = '/ \/sure[0-9]+ayah[0-9]+ /'; + [$sure, $aya] = StringHelper::getCommandByRegex($message, $regex); + self::assertEquals($sure, 233); + self::assertEquals($aya, 234); + + $message = " /sure233ayah234 "; + $isTrue = QuranHelper::isContainSureAyahCommand($message); + self::assertEquals($isTrue, true); + + $message = "asdfasdfas + sadf + + + sadf + + adsf + + /sure233ayah234 "; + $isTrue = QuranHelper::isContainSureAyahCommand($message); + self::assertEquals($isTrue, true); +// [$command, $messageButton] = QuranHelper::getCommandByRegex($message); +// self::assertEquals($command, "/sure233ayah234"); + + $message = " /sure233ayah"; + $isTrue = QuranHelper::isContainSureAyahCommand($message); + self::assertEquals($isTrue, false); + + $message = "sure233ayah234asdfasdfha"; + $isTrue = QuranHelper::isContainSureAyahCommand($message); + self::assertEquals($isTrue, false); + + $message = "sureayah234"; + $isTrue = QuranHelper::isContainSureAyahCommand($message); + self::assertEquals($isTrue, false); + } } diff --git a/tests/Unit/QuranTest.php b/tests/Unit/QuranTest.php new file mode 100644 index 00000000..2dab4fcc --- /dev/null +++ b/tests/Unit/QuranTest.php @@ -0,0 +1,6 @@ +