réponses
309 vues
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;23use 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;1112class ContactMessageHasBeenRepliedNotification extends Notification implements ShouldQueue13{14 use Queueable;1516 /**17 * Create a new notification instance.18 */19 public function __construct(private readonly User $user, private readonly Message $message)20 {21 //22 }2324 /**25 * Get the notification's delivery channels.26 *27 * @return array<int, string>28 */29 public function via(mixed $notifiable): array30 {3132 if( ! empty(config('services.telegram-bot-api.token')) &&34 return [TelegramChannel::class];35 }36 return [];37 }3839 /**40 * Get the mail representation of the notification.41 */42 public function toTelegram(): TelegramMessage43 {44 $url=route('filament.admin.resources.messages.view',$this->message->id);4546 return TelegramMessage::create()48 ->content($this->content())49 ->button('Voir les details du message',$url);50 }5152 private function content(): string53 {54 $content = "*Message repondu!*\n\n";55 $content .= 'Par: '.$this->user->fullName."\n";56 $content .= 'Autheur: '.$this->message->fullName;5758 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): array66 {67 return [68 //69 ];70 }71}1namespace App\Notifications;23use 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;1112class ContactMessageHasBeenRepliedNotification extends Notification implements ShouldQueue13{14 use Queueable;1516 /**17 * Create a new notification instance.18 */19 public function __construct(private readonly User $user, private readonly Message $message)20 {21 //22 }2324 /**25 * Get the notification's delivery channels.26 *27 * @return array<int, string>28 */29 public function via(mixed $notifiable): array30 {3132 if( ! empty(config('services.telegram-bot-api.token')) &&34 return [TelegramChannel::class];35 }36 return [];37 }3839 /**40 * Get the mail representation of the notification.41 */42 public function toTelegram(): TelegramMessage43 {44 $url=route('filament.admin.resources.messages.view',$this->message->id);4546 return TelegramMessage::create()48 ->content($this->content())49 ->button('Voir les details du message',$url);50 }5152 private function content(): string53 {54 $content = "*Message repondu!*\n\n";55 $content .= 'Par: '.$this->user->fullName."\n";56 $content .= 'Autheur: '.$this->message->fullName;5758 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): array66 {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
Tu as installé la dependance? Parce que c'est pas une erreur de code ?
Il faut installer
1composer require laravel-notification-channels/telegram1composer require laravel-notification-channels/telegram
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
1APP_URL=http://localhost1APP_URL=http://localhost
En effet Telegram n'approuvait pas les requetes avec une url n'etant pas https ... j'ai remplacé ma variable d'environnement par
1APP_URL=https://google.com1APP_URL=https://google.com
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
Il faut Se connecter ou Créer un compte pour participer à cette conversation.