Skip to content

Commit

Permalink
Merge pull request #41 from saber13812002/hadith_bot_search_all_books
Browse files Browse the repository at this point in the history
Hadith bot search all books
  • Loading branch information
saber13812002 authored Oct 29, 2023
2 parents 9e5c976 + b2a5115 commit 82ecbd7
Show file tree
Hide file tree
Showing 66 changed files with 2,134 additions and 278 deletions.
4 changes: 2 additions & 2 deletions app/Helpers/BlogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function getBlogInfo(string $type, string $chatId): array
->whereChatId($chatId)
->get()
->first();
return $blogUser->count() > 0 ? [$blogUser['blog_user_id'], $blogUser['blog_token']] : [null, null];
return ($blogUser && $blogUser->count() > 0) ? [$blogUser['blog_user_id'], $blogUser['blog_token']] : [null, null];
}


Expand All @@ -86,6 +86,6 @@ public static function getBlogInfoByMobileNumber(string $mobile): array
->whereMobileNumber($mobile)
->get()
->first();
return $blogUser->count() > 0 ? [$blogUser['blog_user_id'], $blogUser['blog_token']] : [null, null];
return ($blogUser && $blogUser->count() > 0) ? [$blogUser['blog_user_id'], $blogUser['blog_token']] : [null, null];
}
}
122 changes: 89 additions & 33 deletions app/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,28 @@ public static function getWeatherBotCommandsAsPostfixForMessages(): string
*/
public static function getHadithCommandsAsPostfixForMessages(): string
{
// TODO: random implementation
return self::getStringMessageDivider() . "
برای جستجو
در کل احادیث کتب شیعه دستور /search
و برای نمایش جستجوهای دیگران در کتب شیعی /history
و برای ارسال یک حدیث تصادفی /random
را کلیک یا ارسال کنید.";
}


/**
* @return string
*/
public static function getNahjCommandsAsPostfixForMessages(): string
{
// TODO: random implementation
return self::getStringMessageDivider() . "
برای جستجو
در کل متن نهج البلاغه دستور /search
و برای نمایش جستجوهای دیگران /history
و برای ارسال یک حکمت یا خطبه یا نامه تصادفی /random
و برای نمایش فهرست /fehrest
را کلیک یا ارسال کنید.";
}

Expand Down Expand Up @@ -127,26 +145,19 @@ public static function generateDetailHadithMessage($academyOfIslamDataItem): str
// dd($academyOfIslamDataItem);
// dd(array_key_exists('book', $academyOfIslamDataItem));
// dd(isset($academyOfIslamDataItem['book']));
list($book, $number, $part, $chapter, $arabic, $english, $_id) = self::getItemFields($academyOfIslamDataItem);
list($book, $number, $part, $chapter, $arabic, $english, $id2) = self::getItemFields($academyOfIslamDataItem);

$botType = Config::get('config.bot.type', 'bale');

$isLong = Str::length(strip_tags($arabic)) > 1000;

if ($isLong && $_id) {
if ($id2) {
self::saveLongTextToDB($academyOfIslamDataItem);
}

return '
شماره:' . $number . '
کتاب:' . strip_tags($book) . '
بخش:' . strip_tags($part) . '
فصل:' . strip_tags($chapter) . '
متن عربی:' . ($isLong ? (substr($arabic, 0, 1000) . "...") : strip_tags($arabic)) . (App::getLocale() != 'fa' ? '
متن انگلیسی:' . substr($english, 0, 100) . '...' : "") . '
شناسه:' . $_id . '
return self::getStringHadith($book, $number, $part, $chapter, $arabic, $english, $id2, $isLong) . '
' . '
' . ($isLong ? self::generateLink($_id, $botType) : '');
' . ($isLong ? self::generateLink($id2, $botType) : '');
}


Expand Down Expand Up @@ -229,34 +240,20 @@ public static function getSureAyeByRegex(string $message): array
return [0, 0];
}

private static function generateLink(mixed $_id, mixed $botType): string
private static function generateLink(mixed $id2, mixed $botType): string
{
$cmd = "/_id=" . $_id;
$cmd = "/_id=" . $id2;
if ($botType == 'bale') {
return "[" . $cmd . "](send:" . $cmd . ")";
}
return $cmd;
}

private static function saveLongTextToDB(mixed $academyOfIslamDataItem): void
private static function saveLongTextToDB(mixed $academyOfIslamDataItem)
{
list($book, $number, $part, $chapter, $arabic, $english, $_id) = self::getItemFields($academyOfIslamDataItem);
// dd($_id);
$botHadithItem = BotHadithItem::firstOrNew([
'_id' => $_id
], [
'book' => $book,
'number' => $number,
'part' => $part,
'chapter' => $chapter,
'arabic' => $arabic,
'english' => $english
// 'arabic' => $arabic,
// 'arabic' => $arabic,
// 'arabic' => $arabic
]);


list($book, $number, $part, $chapter, $arabic, $english, $id2) = self::getItemFields($academyOfIslamDataItem);
// dd($id2);
return self::firstOrCreate($id2, $book, $number, $part, $chapter, $arabic, $english);
}

/**
Expand All @@ -276,7 +273,66 @@ public static function getItemFields(mixed $academyOfIslamDataItem): array
// $gradings = $academyOfIslamDataItem["gradings"][0];
// $related = $academyOfIslamDataItem["related"][0];
// $history = $academyOfIslamDataItem["history"][0];
$_id = isset($academyOfIslamDataItem['_id']) ? $academyOfIslamDataItem["_id"] : "";
return array($book, $number, $part, $chapter, $arabic, $english, $_id);
$id2 = isset($academyOfIslamDataItem['_id']) ? $academyOfIslamDataItem["_id"] : "";
return array($book, $number, $part, $chapter, $arabic, $english, $id2);
}

/**
* @param string $id2
* @param string $book
* @param string $number
* @param string $part
* @param string $chapter
* @param string $arabic
* @param string $english
* @return void
*/
public static function firstOrCreate(string $id2, string $book, string $number, string $part, string $chapter, string $arabic, string $english)
{
$botHadithItem = BotHadithItem::firstOrCreate([
'id2' => $id2
], [
// 'id2' => $id2,
'book' => $book,
'number' => $number,
'part' => $part,
'chapter' => $chapter,
'arabic' => $arabic,
'english' => $english
// 'arabic' => $arabic,
// 'arabic' => $arabic,
// 'arabic' => $arabic
]);
// $botHadithItem->save();
return $botHadithItem;
}

public static function getStringHadith($book, $number, $part, $chapter, $arabic, $english, $id2, $isLong): string
{
return '
' . trans("hadith.result.number: ") . $number . '
' . trans("hadith.result.book: ") . strip_tags($book) . '
' . trans("hadith.result.part: ") . strip_tags($part) . '
' . trans("hadith.result.chapter: ") . strip_tags($chapter) . '
' . trans("hadith.result.arabic text: ") . ($isLong ? (substr($arabic, 0, 1000) . "...") : strip_tags($arabic)) . (App::getLocale() != 'fa' ? '
' . trans("hadith.result.english text: ") . substr($english, 0, 100) . '...' : "") . '
' . trans("hadith.result.id: ") . $id2;
}

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
{
return '
' . 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;
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/BotMotherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function childrenMessageBroadcasterWebhook(Request $request)
->firstOrFail();
} catch (Exception $e) {
BotHelper::sendMessageToSuperAdmin('وب هوک ارسالی به سرور برای روبات بله قادر به تشخیص توکن و یوزرنیم روبات نیست', $type);
Log::warning($e->getMessage());
Log::error($e->getMessage());
// throw $e;
}
// TODO: count check
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/BotUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function approve(Request $request)
->firstOrFail();
} catch (Exception $e) {
BotHelper::sendMessageToSuperAdmin(trans("bot.An error occurred when admin want to approve your request"), $type);
Log::warning($e->getMessage());
Log::error($e->getMessage());
// throw $e;
}

Expand All @@ -52,7 +52,7 @@ public function approve(Request $request)
} catch (Exception $e) {
$message = 'یا این روبات قبلا تایید شده است و الان دارد دوباره تایید میشود';
BotHelper::sendMessageToBotAdmin($bot, $message);
Log::warning($e->getMessage());
Log::error($e->getMessage());
// throw $e;
}

Expand Down
29 changes: 21 additions & 8 deletions app/Http/Controllers/HadithSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Interfaces\Services\HadithApiService;
use App\Models\BotHadithItem;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -70,11 +72,11 @@ public function index(BotRequest $request)
// TODO : saveLastCommandInDb($bot); performance said we need to have lastStatus log for any bot mother
$message = trans("hadith.Please send your phrase to search in all shia hadith books.");
} else if ($isHadithIdRequested) {
// echo '_id';
$_id = substr($bot->Text(), 5);
$hadith = BotHadithItem::query()->where("_id", $_id)->first();
if ($hadith && count($hadith) > 0)
BotHelper::sendMessage($bot, $hadith);
// echo 'id2';
$id2 = substr($bot->Text(), 5);
$hadith = BotHadithItem::query()->where("id2", $id2)->first();
if ($hadith)
BotHelper::sendMessage($bot, $this->getHadith($hadith));
else
BotHelper::sendMessage($bot, trans("bot.not found"));
} else {
Expand All @@ -84,11 +86,11 @@ public function index(BotRequest $request)
[$phrase, $page, $limit] = $this->getPhraseAndPage($bot);
BotHelper::sendMessageToSuperAdmin("hadith:
" . $phrase, $bot->BotType());
BotHelper::sendMessage($bot, trans("bot.please wait"));
BotHelper::sendMessage($bot, trans("bot.please wait") . $this->getSearchWebUrl($phrase) . "
" . trans("bot.if there is no results please try again with non long query with less words. thank you"));
$message = $this->hadithApiService->search($phrase, $page, $limit);
$message .= trans("hadith.for more result click this link:") .
"
https://hadith.academyofislam.com/?q=" . str_replace(' ', '%20', $phrase);
$this->getSearchWebUrl($phrase);
}

BotHelper::sendMessageToUserAndAdmins($bot, $message . $commands, $type);
Expand Down Expand Up @@ -128,4 +130,15 @@ private static function ifBotTextIsTooLong($bot, string $botText): bool
return false;
}

private function getSearchWebUrl($phrase)
{
return "
https://hadith.academyofislam.com/?q=" . StringHelper::normalizer($phrase);
}

private function getHadith(Model|Builder|null $hadith)
{
return StringHelper::getStringHadith($hadith->book, $hadith->number, $hadith->part, $hadith->chapter, $hadith->arabic, $hadith->english, $hadith->id2, false);
}

}
Loading

0 comments on commit 82ecbd7

Please sign in to comment.