Skip to content

Instantly share code, notes, and snippets.

@1stevengrant
Created October 18, 2024 06:30
Show Gist options
  • Save 1stevengrant/995b8736457091b70fc532f43c96963f to your computer and use it in GitHub Desktop.
Save 1stevengrant/995b8736457091b70fc532f43c96963f to your computer and use it in GitHub Desktop.
Telegram service to send a message to a Telegram bot
<?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