diff --git a/app/Helpers/BotHelper.php b/app/Helpers/BotHelper.php index f647bbb9..03bd51f8 100644 --- a/app/Helpers/BotHelper.php +++ b/app/Helpers/BotHelper.php @@ -714,4 +714,20 @@ public static function sendPhotoGap(mixed $chat_id, string $photoUrl, $messenger { return $messenger->sendImage($chat_id, $photoUrl, $caption); } + + + public static function linkArticle($itemId): string + { + return "/_id" . $itemId; + } + + public static function generateLink(string $command, $botType): string + { + return ($botType == 'bale') ? "[" . $command . "](send:" . $command . ")" : $command; + } + + public static function generateTextLink(string $text, string $command, $botType): string + { + return ($botType == 'bale') ? "[" . $text . "](send:" . $command . ")" : $text . " -> " . $command; + } } diff --git a/app/Helpers/QuranHelper.php b/app/Helpers/QuranHelper.php index f7481ed1..b1c17415 100644 --- a/app/Helpers/QuranHelper.php +++ b/app/Helpers/QuranHelper.php @@ -716,11 +716,14 @@ public static function getResultMessage(int $count, mixed $item, array|string|nu */ public static function getPageNumberFromPhrase(string $searchPhrase): array { - $pageNumberPosition = strpos($searchPhrase, "page="); + $page = "page"; + $pageNumberPosition = strpos($searchPhrase, $page); + + $offset = strlen($page); if ($pageNumberPosition > 1) { - if (strlen($searchPhrase) > $pageNumberPosition + 5) { - $pageNumber = substr($searchPhrase, $pageNumberPosition + 5, strlen($searchPhrase)); + if (strlen($searchPhrase) > $pageNumberPosition + $offset) { + $pageNumber = substr($searchPhrase, $pageNumberPosition + $offset, strlen($searchPhrase)); $searchPhrase = substr($searchPhrase, 0, $pageNumberPosition); return [$searchPhrase, $pageNumber]; } else { diff --git a/app/Helpers/StringHelper.php b/app/Helpers/StringHelper.php index 05b31f75..90703a8b 100644 --- a/app/Helpers/StringHelper.php +++ b/app/Helpers/StringHelper.php @@ -65,14 +65,17 @@ public static function getHadithCommandsAsPostfixForMessages(): string */ public static function getNahjCommandsAsPostfixForMessages(): string { + $botType = Config::get('config.bot.type', 'bale'); // TODO: random implementation return self::getStringMessageDivider() . " برای جستجو -در کل متن نهج البلاغه دستور /search -و برای نمایش جستجوهای دیگران /history -و برای ارسال یک حکمت یا خطبه یا نامه تصادفی /random -و برای نمایش فهرست /fehrest +در کل متن نهج البلاغه دستور / search +و برای نمایش جستجوهای دیگران / history +و برای ارسال یک حکمت یا خطبه یا نامه تصادفی " . " " . BotHelper::generateLink("/random", $botType) . " + و برای نمایش فهرست. " . BotHelper::generateLink("/fehrest", $botType) . + " را کلیک یا ارسال کنید."; + } @@ -147,7 +150,7 @@ public static function generateDetailHadithMessage($academyOfIslamDataItem): str // dd(isset($academyOfIslamDataItem['book'])); list($book, $number, $part, $chapter, $arabic, $english, $id2) = self::getItemFields($academyOfIslamDataItem); - $botType = Config::get('config.bot.type', 'bale'); + $botType = Config::get('config.bot.type', 'bale'); //bottype $isLong = Str::length(strip_tags($arabic)) > 1000; @@ -155,9 +158,11 @@ public static function generateDetailHadithMessage($academyOfIslamDataItem): str self::saveLongTextToDB($academyOfIslamDataItem); } + $cmd = "/_id" . $id2; + return self::getStringHadith($book, $number, $part, $chapter, $arabic, $english, $id2, $isLong) . ' ' . ' - ' . ($isLong ? self::generateLink($id2, $botType) : ''); + ' . ($isLong ? BotHelper::generateTextLink($cmd, $cmd, $botType) : ''); } @@ -240,14 +245,6 @@ public static function getSureAyeByRegex(string $message): array return [0, 0]; } - private static function generateLink(mixed $id2, mixed $botType): string - { - $cmd = "/_id=" . $id2; - if ($botType == 'bale') { - return "[" . $cmd . "](send:" . $cmd . ")"; - } - return $cmd; - } private static function saveLongTextToDB(mixed $academyOfIslamDataItem) { @@ -324,15 +321,22 @@ public static function normalizer($phrase): array|string return str_replace(' ', '%20', $phrase); } - public static function getStringNahj(mixed $category, mixed $number, mixed $title, mixed $persian=null, mixed $arabic=null, mixed $english=null, mixed $dashti=null, mixed $id, bool $isLong): string + public static function getStringNahj(mixed $category, mixed $number, mixed $title, mixed $persian = null, mixed $arabic = null, mixed $english = null, mixed $dashti = null, mixed $id, bool $isLong): string { + $botType = Config::get('config.bot.type', 'bale'); return ' +' . trans("nahj.result.category: ") . self::getCategory($category) . ' ' . trans("nahj.result.number: ") . $number . ' -' . trans("nahj.result.category: ") . strip_tags($category) . ' ' . trans("nahj.result.title: ") . strip_tags($title) . (App::getLocale() == 'fa' ? ' ' . trans("nahj.result.translate: ") . strip_tags($persian) : "") . ' ' . trans("nahj.result.arabic text: ") . ($isLong ? (substr($arabic, 0, 1000) . "...") : strip_tags($arabic)) . (App::getLocale() != 'fa' ? ' ' . trans("nahj.result.english text: ") . substr($english, 0, 100) . '...' : "") . ' -' . trans("nahj.result.id: ") . $id; +' . trans("nahj.result.id: ") . $id . " -> " . BotHelper::generateTextLink("/_id" . $id, "/_id" . $id, $botType) . " +" . trans("bot.next") . " -> " . BotHelper::generateTextLink("/_id" . $id + 1, "/_id" . $id + 1, $botType); + } + + private static function getCategory(mixed $category) + { + return $category == 1 ? "خطبه" : ($category == 2 ? "نامه" : "حکمت"); } } diff --git a/app/Http/Controllers/NahjController.php b/app/Http/Controllers/NahjController.php index 57d51d6f..8ee62fd4 100644 --- a/app/Http/Controllers/NahjController.php +++ b/app/Http/Controllers/NahjController.php @@ -66,18 +66,25 @@ public function index(BotRequest $request) $message = trans("nahj.in the name of God you can use /help command to start."); } else if ($command == "search") { $message = trans("nahj.Please send your phrase to search in all Nahj ul balagha texts."); + } else if ($command == "random") { + $page = 1; + $limit = 1; + $id = rand(1, 815); + $message = $this->nahjService->item($bot, $id, $page, $limit); + return 1; } else if ($command == "help") { $message = $this->nahjService->help($bot); return 1; } else if ($command == "fehrest") { [$phrase, $page, $limit] = $this->getPhraseAndPage($bot); +// dd($page); $this->nahjService->list($bot, "", $page, $limit); return 1; } else if ($isItemRequested) { // /_id3 $id2 = substr($bot->Text(), 4); [$phrase, $page, $limit] = $this->getPhraseAndPage($bot); +// dd($page); $this->nahjService->item($bot, $id2, $page, $limit); - } else { $message = $this->nahjService->help($bot); } diff --git a/app/Services/NahjServiceImpl.php b/app/Services/NahjServiceImpl.php index 5c6490fd..62833cdb 100644 --- a/app/Services/NahjServiceImpl.php +++ b/app/Services/NahjServiceImpl.php @@ -100,8 +100,8 @@ public function list($bot, string $phrase, string $currentPage, string $pageSize $message .= $this->getFehrestItems($item, $bot->BotType()); } $message .= " -" . ((int)$currentPage != 163 ? $this->generateLink($commandNext, $bot->BotType()) : $this->generateLink("/fehrest", $bot->BotType())) . " -" . ((int)$currentPage > 1 ? $this->generateLink($commandBack, $bot->BotType()) : ""); +" . ((int)$currentPage != 163 ? BotHelper::generateLink($commandNext, $bot->BotType()) : BotHelper::generateLink("/fehrest", $bot->BotType())) . " +" . ((int)$currentPage > 1 ? BotHelper::generateLink($commandBack, $bot->BotType()) : ""); $this->sendFehrest($message, $bot); } @@ -190,21 +190,6 @@ public function sendFehrest($message, $bot): void private function getFehrestItems(mixed $item, string $botType): string { $text = $item->category . "-" . $item->number . "-" . $item->title; - return $this->generateTextLink($text, $this->linkArticle($item->id), $botType) . "\n"; - } - - private function linkArticle($itemId): string - { - return "/_id" . $itemId; - } - - private function generateLink(string $command, $botType): string - { - return ($botType == 'bale') ? "[" . $command . "](send:" . $command . ")" : $command; - } - - private function generateTextLink(string $text, string $command, $botType): string - { - return ($botType == 'bale') ? "[" . $text . "](send:" . $command . ")" : $text . "->" . $command; + return BotHelper::generateTextLink($text, BotHelper::linkArticle($item->id), $botType) . "\n"; } }