russeloken
a posé

implementation de l'envoi de notification via telegram avec laravel

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

1TELEGRAM_BOT_TOKEN=
2TELEGRAM_CHANNEL=
1TELEGRAM_BOT_TOKEN=
2TELEGRAM_CHANNEL=

contenu de mon services.php

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

contenu de ma notification class

1namespace App\Notifications;
2 
3use App\Models\Message;
4use App\Models\User;
5use Illuminate\Bus\Queueable;
6use Illuminate\Contracts\Queue\ShouldQueue;
7use Illuminate\Notifications\Messages\MailMessage;
8use Illuminate\Notifications\Notification;
9use NotificationChannels\Telegram\TelegramChannel;
10use NotificationChannels\Telegram\TelegramMessage;
11 
12class ContactMessageHasBeenRepliedNotification extends Notification implements ShouldQueue
13{
14 use Queueable;
15 
16 /**
17 * Create a new notification instance.
18 */
19 public function __construct(private readonly User $user, private readonly Message $message)
20 {
21 //
22 }
23 
24 /**
25 * Get the notification's delivery channels.
26 *
27 * @return array<int, string>
28 */
29 public function via(mixed $notifiable): array
30 {
31 
32 if( ! empty(config('services.telegram-bot-api.token')) &&
33 ! empty(config('services.telegram-bot-api.channel')) ){
34 return [TelegramChannel::class];
35 }
36 return [];
37 }
38 
39 /**
40 * Get the mail representation of the notification.
41 */
42 public function toTelegram(): TelegramMessage
43 {
44 $url=route('filament.admin.resources.messages.view',$this->message->id);
45 
46 return TelegramMessage::create()
48 ->content($this->content())
49 ->button('Voir les details du message',$url);
50 }
51 
52 private function content(): string
53 {
54 $content = "*Message repondu!*\n\n";
55 $content .= 'Par: '.$this->user->fullName."\n";
56 $content .= 'Autheur: '.$this->message->fullName;
57 
58 return $content;
59 }
60 /**
61 * Get the array representation of the notification.
62 *
63 * @return array<string, mixed>
64 */
65 public function toArray(object $notifiable): array
66 {
67 return [
68 //
69 ];
70 }
71}
1namespace App\Notifications;
2 
3use App\Models\Message;
4use App\Models\User;
5use Illuminate\Bus\Queueable;
6use Illuminate\Contracts\Queue\ShouldQueue;
7use Illuminate\Notifications\Messages\MailMessage;
8use Illuminate\Notifications\Notification;
9use NotificationChannels\Telegram\TelegramChannel;
10use NotificationChannels\Telegram\TelegramMessage;
11 
12class ContactMessageHasBeenRepliedNotification extends Notification implements ShouldQueue
13{
14 use Queueable;
15 
16 /**
17 * Create a new notification instance.
18 */
19 public function __construct(private readonly User $user, private readonly Message $message)
20 {
21 //
22 }
23 
24 /**
25 * Get the notification's delivery channels.
26 *
27 * @return array<int, string>
28 */
29 public function via(mixed $notifiable): array
30 {
31 
32 if( ! empty(config('services.telegram-bot-api.token')) &&
33 ! empty(config('services.telegram-bot-api.channel')) ){
34 return [TelegramChannel::class];
35 }
36 return [];
37 }
38 
39 /**
40 * Get the mail representation of the notification.
41 */
42 public function toTelegram(): TelegramMessage
43 {
44 $url=route('filament.admin.resources.messages.view',$this->message->id);
45 
46 return TelegramMessage::create()
48 ->content($this->content())
49 ->button('Voir les details du message',$url);
50 }
51 
52 private function content(): string
53 {
54 $content = "*Message repondu!*\n\n";
55 $content .= 'Par: '.$this->user->fullName."\n";
56 $content .= 'Autheur: '.$this->message->fullName;
57 
58 return $content;
59 }
60 /**
61 * Get the array representation of the notification.
62 *
63 * @return array<string, mixed>
64 */
65 public function toArray(object $notifiable): array
66 {
67 return [
68 //
69 ];
70 }
71}

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

1[2024-01-06 13:32:26] local.ERROR: Driver [NotificationChannels\Telegram\TelegramChannel] not supported. {"exception":"[object] (InvalidArgumentException(code: 0):
1[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

mckenziearts
a répondu

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

Confirmer la suppression

Êtes-vous sûr de vouloir supprimer cette réponse ? Cette action est irréversible.

dy05
dy05 36 XP
a répondu

Il faut installer

1composer require laravel-notification-channels/telegram
1composer require laravel-notification-channels/telegram
Confirmer la suppression

Êtes-vous sûr de vouloir supprimer cette réponse ? Cette action est irréversible.

russeloken
a répondu
Meilleure réponse

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é

Confirmer la suppression

Êtes-vous sûr de vouloir supprimer cette réponse ? Cette action est irréversible.

lelouch_p
a répondu

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

Confirmer la suppression

Êtes-vous sûr de vouloir supprimer cette réponse ? Cette action est irréversible.

Il faut Se connecter ou Créer un compte pour participer à cette conversation.

Confirmer la suppression

Êtes-vous sûr de vouloir supprimer ce sujet ? Cette action est irréversible.