Skip to content

Commit

Permalink
Merge pull request #42 from saber13812002/hadith_bot_search_all_books
Browse files Browse the repository at this point in the history
v6.18 nahj bot
  • Loading branch information
saber13812002 authored Oct 29, 2023
2 parents 82ecbd7 + d2d8ccd commit 6da1876
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 39 deletions.
16 changes: 16 additions & 0 deletions app/Helpers/BotHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 6 additions & 3 deletions app/Helpers/QuranHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 21 additions & 17 deletions app/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) .
"
را کلیک یا ارسال کنید.";

}


Expand Down Expand Up @@ -147,17 +150,19 @@ 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;

if ($id2) {
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) : '');
}


Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 ? "نامه" : "حکمت");
}
}
9 changes: 8 additions & 1 deletion app/Http/Controllers/NahjController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 3 additions & 18 deletions app/Services/NahjServiceImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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";
}
}

0 comments on commit 6da1876

Please sign in to comment.