Skip to content

Commit

Permalink
Merge pull request #33 from saber13812002/send_button_bale_when_admin…
Browse files Browse the repository at this point in the history
…_has_command_message

Send button bale when admin has command message
  • Loading branch information
saber13812002 authored Sep 4, 2023
2 parents 30c1886 + 55ee5cf commit 6960a2f
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 7 deletions.
26 changes: 26 additions & 0 deletions app/Helpers/BotHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion app/Helpers/QuranHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Helpers;

use App;
use App\Models\BotUsers;
use App\Models\QuranAyat;
use App\Models\QuranSurah;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -986,7 +1010,6 @@ private static function getCommandNextPage(string $searchPhrase, int $nextPage):
//" . $searchPhrase . "page=" . $nextPage;

}

}


Expand Down
23 changes: 23 additions & 0 deletions app/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Helpers;

use Carbon\Carbon;
use Illuminate\Support\Str;

class StringHelper
{
Expand Down Expand Up @@ -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];
}
}
36 changes: 30 additions & 6 deletions app/Http/Controllers/QuranWordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -460,6 +459,7 @@ public function index(BotRequest $request)

/**
* Show the form for creating a new resource.
* @throws GuzzleException
*/
public
function messageToAll(BotRequest $request)
Expand Down Expand Up @@ -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]));
Expand Down
43 changes: 43 additions & 0 deletions tests/Unit/QuranHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions tests/Unit/QuranTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class QuranTest
{

}
8 changes: 8 additions & 0 deletions tests/Unit/StringHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use App\Helpers\QuranHelper;
use PHPUnit\Framework\TestCase;

class StringHelperTest extends TestCase
{
}

0 comments on commit 6960a2f

Please sign in to comment.