Skip to content

Instantly share code, notes, and snippets.

@1stevengrant
Created October 18, 2024 06:30
Show Gist options
  • Select an option

  • Save 1stevengrant/995b8736457091b70fc532f43c96963f to your computer and use it in GitHub Desktop.

Select an option

Save 1stevengrant/995b8736457091b70fc532f43c96963f to your computer and use it in GitHub Desktop.

Revisions

  1. 1stevengrant created this gist Oct 18, 2024.
    33 changes: 33 additions & 0 deletions TelegramService.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?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;
    }
    }
    }