Created
October 18, 2024 06:30
-
-
Save 1stevengrant/995b8736457091b70fc532f43c96963f to your computer and use it in GitHub Desktop.
Telegram service to send a message to a Telegram bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use Illuminate\Support\Facades\Http; | |
use Illuminate\Support\Facades\Log; | |
class TelegramService | |
{ | |
public function __construct( | |
private string $botToken = '', | |
private string $chatId = '' | |
) { | |
$this->botToken = $this->botToken ?: config('services.telegram.token'); | |
$this->chatId = $this->chatId ?: config('services.telegram.chat_id'); | |
} | |
public function sendMessage($message): bool | |
{ | |
$response = Http::post("https://api.telegram.org/bot{$this->botToken}/sendMessage", [ | |
'chat_id' => $this->chatId, | |
'text' => $message, | |
]); | |
if ($response->successful()) { | |
Log::info("Telegram message sent successfully"); | |
return true; | |
} else { | |
Log::error("Failed to send Telegram message: " . $response->body()); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment