implementation de l'envoi de notification via telegram avec laravel

russeloken @russeloken 65 XP a posé dans

je suis entrain d'implémenter l'envoi de notification via Telegram avec Laravel, le bot et le channel ont été déjà configurer , J'ai récupérer leurs id , ajouter le bot au Channel avec les différentes autorisations.

Le contenu de mon .env

TELEGRAM_BOT_TOKEN=
TELEGRAM_CHANNEL=

contenu de mon services.php

'telegram-bot-api'=>[
'token'=> env('TELEGRAM_BOT_TOKEN'),
'channel'=> env('TELEGRAM_CHANNEL')
]

contenu de ma notification class

namespace App\Notifications;
 
use App\Models\Message;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramChannel;
use NotificationChannels\Telegram\TelegramMessage;
 
class ContactMessageHasBeenRepliedNotification extends Notification implements ShouldQueue
{
use Queueable;
 
/**
* Create a new notification instance.
*/
public function __construct(private readonly User $user, private readonly Message $message)
{
//
}
 
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(mixed $notifiable): array
{
 
if( ! empty(config('services.telegram-bot-api.token')) &&
! empty(config('services.telegram-bot-api.channel')) ){
return [TelegramChannel::class];
}
return [];
}
 
/**
* Get the mail representation of the notification.
*/
public function toTelegram(): TelegramMessage
{
$url=route('filament.admin.resources.messages.view',$this->message->id);
 
return TelegramMessage::create()
->to(config('services.telegram-bot-api.channel'))
->content($this->content())
->button('Voir les details du message',$url);
}
 
private function content(): string
{
$content = "*Message repondu!*\n\n";
$content .= 'Par: '.$this->user->fullName."\n";
$content .= 'Autheur: '.$this->message->fullName;
 
return $content;
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}

ma notification ne marche pas mais je rencontre cette erreur dans mes log

[2024-01-06 13:32:26] local.ERROR: Driver [NotificationChannels\Telegram\TelegramChannel] not supported. {"exception":"[object] (InvalidArgumentException(code: 0):

la commande php artisan queue:work est bien en cours d'exécution , mais ma notification echoue

  • Tu as installé la dependance? Parce que c'est pas une erreur de code ?

  • Il faut installer

    composer require laravel-notification-channels/telegram
  • Réponse acceptée

    j'ai pu resoudre mon probleme , en realité il s'agissait pas d'un probleme de dependance (Car celle ci etait installée) je me suis rendu compte que le probleme venait de mon .env

    En effet Telegram n'approuvait pas les requetes avec une url n'etant pas https ... j'ai remplacé ma variable d'environnement par

    afin de faire un test , car je suis en local et cela a marché

  • En local tu peux utiliser les solutions gratuites en ligne comme ngrok pour tuneliser ton application

Veuillez vous connecter ou créer un compte pour participer à cette conversation.