Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/Http/Controllers/GithubWebHookController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Controllers;

use App\Services\TelegramBot;
use Illuminate\Http\Request;

class GithubWebHookController extends Controller
{
/**
* Handle incoming Telegram webhook requests.
*
* @param \Illuminate\Http\Request $request
* @param TelegramBot $telegramBot
*
* @throws \Throwable
*
* @return void
*/
public function release(Request $request, TelegramBot $telegramBot)
{
$payload = $request->all();

if ($payload['action'] === 'published') {
$release = $payload['release'];
$repo = $payload['repository'];

$message = view('telegram.github-release-notification', [
'repo' => $repo['full_name'],
'version' => $release['tag_name'],
'title' => $release['name'],
'body' => $release['body'],
'url' => $release['html_url'],
])->render();

collect(config('telegram.chats'))
->where('orchid_release', true)
->each(fn ($subscriber) => $telegramBot->sendMessageToChat($subscriber['id'], $message));
}
}
}
15 changes: 0 additions & 15 deletions app/Http/Controllers/WebHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,8 @@ class WebHookController extends Controller
*/
public function telegram(Request $request): void
{
$captcha = null;
if ($request->has('callback_query')) {
$data = $request->collect('callback_query');

$captcha = new CaptchaCallback(
checkId: $data->dot()->get('data'),
from: $data->dot()->get('from.id'),
messageId: $data->dot()->get('message.message_id'),
chatId: $data->dot()->get('message.chat.id'),
);
}

Log::channel('telegram')->info(json_encode($request->all()));

TelegramMessage::dispatch(
$request->collect('message'),
$captcha,
);

}
Expand Down
53 changes: 2 additions & 51 deletions app/Jobs/TelegramMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
namespace App\Jobs;

use App\Models\AntiSpamRecord;
use App\Services\Telegram\CaptchaCallback;
use App\Services\Telegram\TelegramBot;
use App\Services\TelegramBot;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class TelegramMessage implements ShouldQueue
{
Expand All @@ -26,69 +22,24 @@ class TelegramMessage implements ShouldQueue

public $firstName;

public $newChatMember;

public $locale;

/**
* Create a new job instance.
*/
public function __construct(public Collection $message, public ?CaptchaCallback $captcha)
public function __construct(public Collection $message)
{
$this->text = $this->message->only(['text', 'caption'])->first();
$this->messageId = $this->message->get('message_id');
$this->firstName = $this->message->dot()->get('from.first_name');
$this->chatId = $this->message->dot()->get('chat.id');
$this->from = $this->message->dot()->get('from.id');
$this->newChatMember = (bool) $this->message->get('new_chat_member');

Log::channel('telegram')->info('CHAT MEMBER CONSTRUCTOR:'.$this->newChatMember);

$chatConfig = collect(config('telegram.chats'))
->where('id', $this->chatId)
->first();

$this->locale = $chatConfig ? $chatConfig['locale'] : config('telegram.default_locale');

Log::channel('telegram')->info("TG Message Locale: $this->locale| TG Message chat:".json_encode($chatConfig));
}

/**
* Execute the job.
*/
public function handle(TelegramBot $telegramBot): void
{
// Ban new user without duration and send captcha button
/*
if ($this->newChatMember) {
Log::channel('telegram')->info('NEW CHAT MEMBER IF');
App::setLocale($this->locale);
$telegramBot->banUserInGroup($this->chatId, $this->from);
$telegramBot->sendWelcomeButton($this->chatId, $this->from, $this->firstName);

return;
}
*/

// Unmute user after click button
if ($this->captcha?->checkId && ($this->captcha->checkId === $this->captcha->from)) {

$telegramBot->unmuteUserInGroup($this->captcha->chatId, $this->captcha->from);

$messageId = Cache::get("tg_message_{$this->captcha->chatId}_{$this->captcha->from}");

if ($messageId) {
$telegramBot->deleteMessage($this->captcha->chatId, $messageId);
Cache::forget("tg_message_{$this->captcha->chatId}_{$this->captcha->from}");
}

return;
}

// Return when captcha clicked not muted users.
if ($this->captcha?->checkId && ($this->captcha->checkId !== $this->captcha->from)) {
return;
}

if (empty($this->from)) {
return;
Expand Down
13 changes: 0 additions & 13 deletions app/Services/Telegram/CaptchaCallback.php

This file was deleted.

161 changes: 0 additions & 161 deletions app/Services/Telegram/TelegramBot.php

This file was deleted.

11 changes: 11 additions & 0 deletions app/Services/TelegramBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,15 @@ public function isSpam(?string $message, $userId = null): bool

return (new SpamDetector($message))->isSpam();
}

public function sendMessageToChat(int $chatId, string $message): Response
{
$url = "https://api.telegram.org/bot{$this->token}/sendMessage";

return Http::post($url, [
'chat_id' => $chatId,
'text' => $message,
'parse_mode' => 'Markdown',
]);
}
}
23 changes: 13 additions & 10 deletions config/telegram.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<?php

return [
'chats' => [
'chats' => [
[
'id' => '-1002111700088',
'name' => 'Ахтунг laravel.su чат',
'locale' => 'en',
'id' => '-1002111700088',
'name' => 'Ахтунг laravel.su чат',
'locale' => 'en',
'orchid_release' => true,
],
[
'id' => '-1001300166722',
'name' => 'Laravel Orchid Russian Community',
'locale' => 'ru',
'id' => '-1001300166722',
'name' => 'Laravel Orchid Russian Community',
'locale' => 'ru',
'orchid_release' => true,
],
[
'id' => '-1001450006147',
'name' => 'Laravel Orchid',
'locale' => 'en',
'id' => '-1001450006147',
'name' => 'Laravel Orchid',
'locale' => 'en',
'orchid_release' => true,
],
[
'id' => '-1001104353296',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
🚀 *New release {{ $version }}* in {{ $repo }}

📝 *{{ $title }}*

{{ $body }}

[🔗 Link to release]({{ $url }})
Loading
Loading