Skip to content

Commit 9dc0bfe

Browse files
committed
feat(job): add send telegram to job
1 parent 11b0dfb commit 9dc0bfe

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

app/Actions/Log.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Actions;
44

5+
use App\Jobs\SendTelegram;
56
use GuzzleHttp\Client;
67
use GuzzleHttp\Exception\GuzzleException;
78

@@ -31,28 +32,6 @@ public static function error($log): void
3132
self::handle("error", $log);
3233
}
3334

34-
/**
35-
* @throws GuzzleException
36-
*/
37-
private static function send($message): void
38-
{
39-
$token = config("telegram.token");
40-
$channelUsername = config("telegram.id");
41-
42-
$client = new Client();
43-
$url = "https://api.telegram.org/bot{$token}/sendMessage";
44-
45-
$client->post($url, [
46-
'form_params' => [
47-
'chat_id' => $channelUsername,
48-
'text' => $message,
49-
],
50-
]);
51-
}
52-
53-
/**
54-
* @throws GuzzleException
55-
*/
5635
private static function handle($type, $log): void
5736
{
5837
$template = "";
@@ -64,6 +43,6 @@ private static function handle($type, $log): void
6443

6544
$template .= $log;
6645

67-
self::send($template);
46+
SendTelegram::dispatch($template);
6847
}
6948
}

app/Jobs/SendTelegram.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use GuzzleHttp\Client;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Queue\Queueable;
8+
9+
class SendTelegram implements ShouldQueue
10+
{
11+
use Queueable;
12+
13+
public $message;
14+
15+
16+
public function __construct($message)
17+
{
18+
$this->message = $message;
19+
}
20+
21+
/**
22+
* Execute the job.
23+
*/
24+
public function handle(): void
25+
{
26+
$token = config("telegram.token");
27+
$channelUsername = config("telegram.id");
28+
29+
$client = new Client();
30+
$url = "https://api.telegram.org/bot{$token}/sendMessage";
31+
32+
$client->post($url, ['form_params' => ['chat_id' => $channelUsername, 'text' => $this->message]]);
33+
}
34+
}

0 commit comments

Comments
 (0)