Skip to content

Commit 3c248ac

Browse files
authored
Added release info messages to chats (#280)
* Added release info messages to chats * delete bot captcha * remove extra test route * change chat list
1 parent 0b01789 commit 3c248ac

File tree

10 files changed

+78
-250
lines changed

10 files changed

+78
-250
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Services\TelegramBot;
6+
use Illuminate\Http\Request;
7+
8+
class GithubWebHookController extends Controller
9+
{
10+
/**
11+
* Handle incoming Telegram webhook requests.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param TelegramBot $telegramBot
15+
*
16+
* @throws \Throwable
17+
*
18+
* @return void
19+
*/
20+
public function release(Request $request, TelegramBot $telegramBot)
21+
{
22+
$payload = $request->all();
23+
24+
if ($payload['action'] === 'published') {
25+
$release = $payload['release'];
26+
$repo = $payload['repository'];
27+
28+
$message = view('telegram.github-release-notification', [
29+
'repo' => $repo['full_name'],
30+
'version' => $release['tag_name'],
31+
'title' => $release['name'],
32+
'body' => $release['body'],
33+
'url' => $release['html_url'],
34+
])->render();
35+
36+
collect(config('telegram.chats'))
37+
->where('orchid_release', true)
38+
->each(fn ($subscriber) => $telegramBot->sendMessageToChat($subscriber['id'], $message));
39+
}
40+
}
41+
}

app/Http/Controllers/WebHookController.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,8 @@ class WebHookController extends Controller
1919
*/
2020
public function telegram(Request $request): void
2121
{
22-
$captcha = null;
23-
if ($request->has('callback_query')) {
24-
$data = $request->collect('callback_query');
25-
26-
$captcha = new CaptchaCallback(
27-
checkId: $data->dot()->get('data'),
28-
from: $data->dot()->get('from.id'),
29-
messageId: $data->dot()->get('message.message_id'),
30-
chatId: $data->dot()->get('message.chat.id'),
31-
);
32-
}
33-
34-
Log::channel('telegram')->info(json_encode($request->all()));
35-
3622
TelegramMessage::dispatch(
3723
$request->collect('message'),
38-
$captcha,
3924
);
4025

4126
}

app/Jobs/TelegramMessage.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
namespace App\Jobs;
44

55
use App\Models\AntiSpamRecord;
6-
use App\Services\Telegram\CaptchaCallback;
7-
use App\Services\Telegram\TelegramBot;
6+
use App\Services\TelegramBot;
87
use Illuminate\Bus\Queueable;
98
use Illuminate\Contracts\Queue\ShouldQueue;
109
use Illuminate\Foundation\Bus\Dispatchable;
1110
use Illuminate\Queue\InteractsWithQueue;
1211
use Illuminate\Queue\SerializesModels;
1312
use Illuminate\Support\Collection;
14-
use Illuminate\Support\Facades\App;
15-
use Illuminate\Support\Facades\Cache;
16-
use Illuminate\Support\Facades\Log;
1713

1814
class TelegramMessage implements ShouldQueue
1915
{
@@ -26,69 +22,24 @@ class TelegramMessage implements ShouldQueue
2622

2723
public $firstName;
2824

29-
public $newChatMember;
30-
31-
public $locale;
3225

3326
/**
3427
* Create a new job instance.
3528
*/
36-
public function __construct(public Collection $message, public ?CaptchaCallback $captcha)
29+
public function __construct(public Collection $message)
3730
{
3831
$this->text = $this->message->only(['text', 'caption'])->first();
3932
$this->messageId = $this->message->get('message_id');
4033
$this->firstName = $this->message->dot()->get('from.first_name');
4134
$this->chatId = $this->message->dot()->get('chat.id');
4235
$this->from = $this->message->dot()->get('from.id');
43-
$this->newChatMember = (bool) $this->message->get('new_chat_member');
44-
45-
Log::channel('telegram')->info('CHAT MEMBER CONSTRUCTOR:'.$this->newChatMember);
46-
47-
$chatConfig = collect(config('telegram.chats'))
48-
->where('id', $this->chatId)
49-
->first();
50-
51-
$this->locale = $chatConfig ? $chatConfig['locale'] : config('telegram.default_locale');
52-
53-
Log::channel('telegram')->info("TG Message Locale: $this->locale| TG Message chat:".json_encode($chatConfig));
5436
}
5537

5638
/**
5739
* Execute the job.
5840
*/
5941
public function handle(TelegramBot $telegramBot): void
6042
{
61-
// Ban new user without duration and send captcha button
62-
/*
63-
if ($this->newChatMember) {
64-
Log::channel('telegram')->info('NEW CHAT MEMBER IF');
65-
App::setLocale($this->locale);
66-
$telegramBot->banUserInGroup($this->chatId, $this->from);
67-
$telegramBot->sendWelcomeButton($this->chatId, $this->from, $this->firstName);
68-
69-
return;
70-
}
71-
*/
72-
73-
// Unmute user after click button
74-
if ($this->captcha?->checkId && ($this->captcha->checkId === $this->captcha->from)) {
75-
76-
$telegramBot->unmuteUserInGroup($this->captcha->chatId, $this->captcha->from);
77-
78-
$messageId = Cache::get("tg_message_{$this->captcha->chatId}_{$this->captcha->from}");
79-
80-
if ($messageId) {
81-
$telegramBot->deleteMessage($this->captcha->chatId, $messageId);
82-
Cache::forget("tg_message_{$this->captcha->chatId}_{$this->captcha->from}");
83-
}
84-
85-
return;
86-
}
87-
88-
// Return when captcha clicked not muted users.
89-
if ($this->captcha?->checkId && ($this->captcha->checkId !== $this->captcha->from)) {
90-
return;
91-
}
9243

9344
if (empty($this->from)) {
9445
return;

app/Services/Telegram/CaptchaCallback.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/Services/Telegram/TelegramBot.php

Lines changed: 0 additions & 161 deletions
This file was deleted.

app/Services/TelegramBot.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,15 @@ public function isSpam(?string $message, $userId = null): bool
9898

9999
return (new SpamDetector($message))->isSpam();
100100
}
101+
102+
public function sendMessageToChat(int $chatId, string $message): Response
103+
{
104+
$url = "https://api.telegram.org/bot{$this->token}/sendMessage";
105+
106+
return Http::post($url, [
107+
'chat_id' => $chatId,
108+
'text' => $message,
109+
'parse_mode' => 'Markdown',
110+
]);
111+
}
101112
}

config/telegram.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<?php
22

33
return [
4-
'chats' => [
4+
'chats' => [
55
[
6-
'id' => '-1002111700088',
7-
'name' => 'Ахтунг laravel.su чат',
8-
'locale' => 'en',
6+
'id' => '-1002111700088',
7+
'name' => 'Ахтунг laravel.su чат',
8+
'locale' => 'en',
9+
'orchid_release' => true,
910
],
1011
[
11-
'id' => '-1001300166722',
12-
'name' => 'Laravel Orchid Russian Community',
13-
'locale' => 'ru',
12+
'id' => '-1001300166722',
13+
'name' => 'Laravel Orchid Russian Community',
14+
'locale' => 'ru',
15+
'orchid_release' => true,
1416
],
1517
[
16-
'id' => '-1001450006147',
17-
'name' => 'Laravel Orchid',
18-
'locale' => 'en',
18+
'id' => '-1001450006147',
19+
'name' => 'Laravel Orchid',
20+
'locale' => 'en',
21+
'orchid_release' => true,
1922
],
2023
[
2124
'id' => '-1001104353296',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
🚀 *New release {{ $version }}* in {{ $repo }}
2+
3+
📝 *{{ $title }}*
4+
5+
{{ $body }}
6+
7+
[🔗 Link to release]({{ $url }})

0 commit comments

Comments
 (0)