I am utilizing the laravel-notification-channels/telegram package to enable my bot to send messages to a channel with admin privileges. The messages are being sent successfully. However, I require the message object returned by the sendMessage function. I must store this message ID in the database for future editing purposes. I have created an event and a listener, but as I am new to using them, I am still waiting for a response.
Notification class
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramMessage;
class TelegramMessageTest extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via($notifiable)
{
return ["telegram"];
}
public function toTelegram($notifiable)
{
$url = url('/announcements/');
return TelegramMessage::create()
->content("Hello there!\nYour invoice has been *PAID*")
->button('View Invoice', $url);
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
Listener class
namespace App\Listeners;
use App\Events\AnnouncementCreated;
use App\Models\User;
use App\Notifications\TelegramMessageTest;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
use NotificationChannels\Telegram\Telegram;
use NotificationChannels\Telegram\TelegramMessage;
class SendTelegramMessage
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*/
public function handle(AnnouncementCreated $event)
{
return Notification::route('telegram', env('TELEGRAM_CHAT_ID'))
->notify(new TelegramMessageTest());
}
}
Event class
namespace App\Events;
use App\Models\Announcement;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class AnnouncementCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*/
public $annoucement;
public function __construct(Announcement $annoucement)
{
$this->annoucement = $annoucement;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}