Skip to content

Commit ec69211

Browse files
committed
feat(job) Run log in a queueable job
1 parent 5e7e039 commit ec69211

File tree

3 files changed

+76
-35
lines changed

3 files changed

+76
-35
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ composer require thecoder/laravel-monolog-telegram
1717
# Usage
1818
Open config/logging.php and change the file
1919

20+
### Run log in a Queue
21+
22+
If a queue name is set, the job will run in the specified queue; otherwise, it will run synchronously.
23+
2024
```php
2125

2226
use TheCoder\MonologTelegram\Attributes\EmergencyAttribute;
@@ -47,6 +51,7 @@ use TheCoder\MonologTelegram\Attributes\LowPriorityAttribute;
4751
'topic_id' => env('LOG_TELEGRAM_TOPIC_ID',null),
4852
'bot_api' => env('LOG_TELEGRAM_BOT_API', 'https://api.telegram.org/bot'),
4953
'proxy' => env('LOG_TELEGRAM_BOT_PROXY', null),
54+
'queue' => env('LOG_TELEGRAM_QUEUE', null),
5055
'topics_level' => [
5156
EmergencyAttribute::class => env('LOG_TELEGRAM_EMERGENCY_ATTRIBUTE_TOPIC_ID', null),
5257
CriticalAttribute::class => env('LOG_TELEGRAM_CRITICAL_ATTRIBUTE_TOPIC_ID', null),

src/SendJob.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace TheCoder\MonologTelegram;
4+
5+
use GuzzleHttp\Client;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
13+
class SendJob implements ShouldQueue
14+
{
15+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16+
17+
public function __construct(
18+
private string $url,
19+
private string $message,
20+
private string $chatId,
21+
private string|null $topicId = null,
22+
private string|null $proxy = null,
23+
)
24+
{
25+
}
26+
27+
public function handle(): void
28+
{
29+
$httpClientOption = [];
30+
$httpClientOption['verify'] = false;
31+
32+
if (!is_null($this->proxy)) {
33+
$httpClientOption['proxy'] = $this->proxy;
34+
}
35+
36+
$params = [
37+
'text' => $this->message,
38+
'chat_id' => $this->chatId,
39+
'parse_mode' => 'html',
40+
'disable_web_page_preview' => true,
41+
];
42+
43+
$requestOptions = [
44+
'form_params' => $this->topicId !== null ? $params + ['message_thread_id' => $this->topicId] : $params
45+
];
46+
47+
$httpClient = new Client($httpClientOption);
48+
49+
try {
50+
$response = $httpClient->post($this->url, $requestOptions);
51+
} catch (\Throwable $exception) {
52+
$this->fail($exception);
53+
}
54+
}
55+
}

src/TelegramBotHandler.php

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class TelegramBotHandler extends AbstractProcessingHandler
5656
*/
5757
protected $topicId;
5858

59+
protected $queue = null;
60+
5961
protected $topicsLevel;
6062

6163
/**
@@ -68,6 +70,7 @@ public function __construct(
6870
string $token,
6971
string $chat_id,
7072
?string $topic_id = null,
73+
?string $queue = null,
7174
$topics_level = [],
7275
$level = Logger::DEBUG,
7376
bool $bubble = true,
@@ -81,6 +84,7 @@ public function __construct(
8184
$this->botApi = $bot_api;
8285
$this->chatId = $chat_id;
8386
$this->topicId = $topic_id;
87+
$this->queue = $queue;
8488
$this->topicsLevel = $topics_level;
8589
$this->level = $level;
8690
$this->bubble = $bubble;
@@ -116,47 +120,24 @@ private function truncateTextToTelegramLimit(string $textMessage): string
116120
* @param null $token
117121
* @param null $chatId
118122
* @param null $topicId
119-
* @param array $option
120123
* @throws GuzzleException
121124
*/
122-
protected function send(string $message, $token = null, $chatId = null, $topicId = null, array $option = []): void
125+
protected function send(string $message, $token = null, $chatId = null, $topicId = null): void
123126
{
124-
try {
125-
126-
$token = $token ?? $this->token;
127-
$chatId = $chatId ?? $this->chatId;
128-
$topicId = $topicId ?? $this->topicId;
129-
130-
if (!isset($option['verify'])) {
131-
$option['verify'] = false;
132-
}
133-
134-
if (!is_null($this->proxy)) {
135-
$option['proxy'] = $this->proxy;
136-
}
137-
138-
$httpClient = new Client($option);
139-
140-
$url = !str_contains($this->botApi, 'https://api.telegram.org')
141-
? $this->botApi
142-
: $this->botApi . $token . '/SendMessage';
143-
144-
$message = $this->truncateTextToTelegramLimit($message);
127+
$token = $token ?? $this->token;
128+
$chatId = $chatId ?? $this->chatId;
129+
$topicId = $topicId ?? $this->topicId;
145130

146-
$params = [
147-
'text' => $message,
148-
'chat_id' => $chatId,
149-
'parse_mode' => 'html',
150-
'disable_web_page_preview' => true,
151-
];
131+
$url = !str_contains($this->botApi, 'https://api.telegram.org')
132+
? $this->botApi
133+
: $this->botApi . $token . '/SendMessage';
152134

153-
$options = [
154-
'form_params' => $topicId !== null ? $params + ['message_thread_id' => $topicId] : $params
155-
];
135+
$message = $this->truncateTextToTelegramLimit($message);
156136

157-
$response = $httpClient->post($url, $options);
158-
} catch (\Exception $e) {
159-
$a = 1;
137+
if ($this->queue === null) {
138+
dispatch_sync(new SendJob($url, $message, $chatId, $topicId, $this->proxy));
139+
} else {
140+
dispatch(new SendJob($url, $message, $chatId, $topicId, $this->proxy))->onQueue($this->queue);
160141
}
161142
}
162143

0 commit comments

Comments
 (0)