added test for channel
This commit is contained in:
@@ -64,6 +64,71 @@ class PriceChannelBroadcastService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ارسال دستی (عیبیابی) — cooldown و شرط تغییر قیمت را نادیده میگیرد.
|
||||
* وضعیت روشن/خاموش، انتخاب پیامرسان، توکن و مقصد همچنان لازم است.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function forceSend(): array
|
||||
{
|
||||
$message = $this->buildMessage();
|
||||
$targets = $this->resolveActiveTargets();
|
||||
|
||||
$results = [];
|
||||
foreach ($targets as $messenger => $target) {
|
||||
$results[$messenger] = $message === ''
|
||||
? ['skipped' => 'پیام خالی است (محصول واجد شرایط نیست)']
|
||||
: $this->dispatchToMessenger($messenger, $target['token'], $target['chat_ids'], $message);
|
||||
}
|
||||
|
||||
if ($message !== '' && $targets !== []) {
|
||||
$this->markSent();
|
||||
}
|
||||
|
||||
return [
|
||||
'sent' => $message !== '' && $targets !== [],
|
||||
'results' => $results,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* گزارش وضعیت — چرا ارسال میشود یا نمیشود.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function diagnose(): array
|
||||
{
|
||||
$selected = $this->selectedMessengers();
|
||||
$messengers = [];
|
||||
|
||||
foreach (self::SUPPORTED_MESSENGERS as $messenger) {
|
||||
$token = trim((string) $this->config->getString($this->tokenKey($messenger), ''));
|
||||
$chatIds = $this->parseList($this->config->getString($this->chatIdsKey($messenger), ''));
|
||||
$isSelected = in_array($messenger, $selected, true);
|
||||
|
||||
$messengers[$messenger] = [
|
||||
'selected' => $isSelected,
|
||||
'has_token' => $token !== '',
|
||||
'destinations' => $chatIds,
|
||||
'will_send' => $isSelected && $token !== '' && $chatIds !== [],
|
||||
];
|
||||
}
|
||||
|
||||
$message = $this->buildMessage();
|
||||
|
||||
return [
|
||||
'enabled' => $this->config->getBool('price_channel_enabled', false),
|
||||
'selected_messengers' => $selected,
|
||||
'min_seconds' => $this->config->getInt('price_channel_min_seconds', 120),
|
||||
'cooldown_elapsed' => $this->cooldownElapsed(),
|
||||
'eligible_products' => count($this->eligibleProducts()),
|
||||
'messengers' => $messengers,
|
||||
'active_targets' => array_keys($this->resolveActiveTargets()),
|
||||
'message_preview' => $message,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* پیامرسانهای قابل ارسال: انتخابشده + توکن معتبر + حداقل یک مقصد.
|
||||
*
|
||||
@@ -241,28 +306,48 @@ class PriceChannelBroadcastService
|
||||
return $lines;
|
||||
}
|
||||
|
||||
private function dispatchToMessenger(string $messenger, string $token, array $chatIds, string $message): void
|
||||
/**
|
||||
* @param array<int, string> $chatIds
|
||||
* @return array<string, string> chatId => نتیجه (ok / خطا)
|
||||
*/
|
||||
private function dispatchToMessenger(string $messenger, string $token, array $chatIds, string $message): array
|
||||
{
|
||||
$results = [];
|
||||
foreach ($chatIds as $chatId) {
|
||||
try {
|
||||
match ($messenger) {
|
||||
$response = match ($messenger) {
|
||||
'bale' => $this->sendBale($token, $chatId, $message),
|
||||
'telegram' => $this->sendTelegram($token, $chatId, $message),
|
||||
'rubika' => $this->sendRubika($token, $chatId, $message),
|
||||
default => null,
|
||||
};
|
||||
|
||||
$status = $response?->status() ?? 0;
|
||||
$ok = $response?->successful() ?? false;
|
||||
$results[$chatId] = $ok ? "ok ({$status})" : "failed ({$status}): " . trim((string) $response?->body());
|
||||
|
||||
if (! $ok) {
|
||||
Log::warning("PriceChannel {$messenger} send not ok", [
|
||||
'chat_id' => $chatId,
|
||||
'status' => $status,
|
||||
'body' => $response?->body(),
|
||||
]);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$results[$chatId] = 'exception: ' . $e->getMessage();
|
||||
Log::warning("PriceChannel {$messenger} send failed", [
|
||||
'chat_id' => $chatId,
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
private function sendBale(string $token, string $chatId, string $message): void
|
||||
private function sendBale(string $token, string $chatId, string $message): \Illuminate\Http\Client\Response
|
||||
{
|
||||
Http::withOptions(Controller::httpClientOptions())
|
||||
return Http::withOptions(Controller::httpClientOptions())
|
||||
->withoutVerifying()
|
||||
->timeout(6)
|
||||
->post("https://tapi.bale.ai/bot{$token}/sendMessage", [
|
||||
@@ -271,9 +356,9 @@ class PriceChannelBroadcastService
|
||||
]);
|
||||
}
|
||||
|
||||
private function sendTelegram(string $token, string $chatId, string $message): void
|
||||
private function sendTelegram(string $token, string $chatId, string $message): \Illuminate\Http\Client\Response
|
||||
{
|
||||
Http::withOptions(Controller::httpClientOptions())
|
||||
return Http::withOptions(Controller::httpClientOptions())
|
||||
->timeout(6)
|
||||
->post("https://api.telegram.org/bot{$token}/sendMessage", [
|
||||
'chat_id' => $chatId,
|
||||
@@ -281,9 +366,9 @@ class PriceChannelBroadcastService
|
||||
]);
|
||||
}
|
||||
|
||||
private function sendRubika(string $token, string $chatId, string $message): void
|
||||
private function sendRubika(string $token, string $chatId, string $message): \Illuminate\Http\Client\Response
|
||||
{
|
||||
Http::withOptions(Controller::httpClientOptions())
|
||||
return Http::withOptions(Controller::httpClientOptions())
|
||||
->withoutVerifying()
|
||||
->timeout(6)
|
||||
->post("https://botapi.rubika.ir/v3/{$token}/sendMessage", [
|
||||
|
||||
Reference in New Issue
Block a user