PHP code example of arthurpar06 / laravel-discord-notifier

1. Go to this page and download the library: Download arthurpar06/laravel-discord-notifier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arthurpar06 / laravel-discord-notifier example snippets


use Arthurpar06\DiscordNotifier\Messages\DiscordMessage;
use Arthurpar06\DiscordNotifier\Embeds\DiscordEmbed;
use Arthurpar06\DiscordNotifier\Enums\DiscordColor;

DiscordMessage::make()
    ->content('Deployment finished')
    ->embed(
        DiscordEmbed::make()
            ->title('Bonjour')
            ->description('Everything is green.')
            ->color(DiscordColor::Green)
            ->field('Environment', 'production', inline: true)
    );

// config/discord-notifier.php
return [
    'bot' => [
        'token' => env('DISCORD_BOT_TOKEN'),
        'api_base' => 'https://discord.com/api/v10',
    ],
];

use Arthurpar06\DiscordNotifier\Messages\DiscordMessage;
use Arthurpar06\DiscordNotifier\Messages\AllowedMentions;
use Arthurpar06\DiscordNotifier\Embeds\DiscordEmbed;
use Arthurpar06\DiscordNotifier\Enums\AllowedMentionType;
use Arthurpar06\DiscordNotifier\Enums\MessageFlag;

DiscordMessage::make()
    ->content('Heads up <@123>')
    ->embeds([
        DiscordEmbed::make()
            ->title('Report')
            ->footer('generated automatically')
            ->author('CI bot'),
    ])
    ->allowedMentions(AllowedMentions::make()->parse([AllowedMentionType::Users]))
    ->flags(MessageFlag::SuppressNotifications);

use Arthurpar06\DiscordNotifier\Components\Button;
use Arthurpar06\DiscordNotifier\Embeds\DiscordEmbed;
use Arthurpar06\DiscordNotifier\Messages\DiscordMessage;

DiscordMessage::make()
    ->embed(DiscordEmbed::make()->title('Deploy finished'))
    ->actionRow(
        Button::link('https://ci.example.com/builds/42', 'View build'),
        Button::primary('redeploy', 'Redeploy'),
    );

use Arthurpar06\DiscordNotifier\Messages\DiscordMessage;
use Illuminate\Notifications\Notification;

class DeploymentFinished extends Notification
{
    public function via($notifiable): array
    {
        return ['discord'];
    }

    public function toDiscord($notifiable): DiscordMessage
    {
        return DiscordMessage::make()->content('Deployment finished ✅');
    }
}

use Illuminate\Support\Facades\Notification;
use Arthurpar06\DiscordNotifier\Routing\DiscordRoute;

// bare channel id → delivered by the bot
Notification::route('discord', config('services.discord.admin_channel_id'))
    ->notify(new DeploymentFinished);

// explicit webhook
Notification::route('discord', DiscordRoute::webhook(config('services.discord.webhook')))
    ->notify(new DeploymentFinished);

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForDiscord(): ?string
    {
        return $this->discord_private_channel_id ?: null;
    }
}

$user->notify(new DeploymentFinished);

public function via($notifiable): array
{
    return ['mail', 'discord']; // users without Discord just get the mail
}
bash
php artisan vendor:publish --tag="discord-notifier-config"