snoeren-development / laravel-discord-webhook-channel

v1.10.0 2024-03-25 07:39 UTC

README

Latest version on Packagist Software License Build status Downloads

安装

您可以使用Composer安装此包

composer require snoeren-development/laravel-discord-webhook-channel

要求

此包需要至少PHP 8.2和Laravel 10。

使用

在您希望通过Discord通知的每个可通知模型中,您需要添加routeNotificationForDiscord方法;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model
{
    use Notifiable;

    /**
     * Route the notification for Discord.
     *
     * @return string
     */
    public function routeNotificationForDiscord(): string
    {
        return $this->discord_webhook;
    }
}

webhook URL可以通过Discord频道服务器的Webhooks设置创建和检索。通知需要完整的URL,其外观如下

https://discordapp.com/api/webhooks/1234567890123456789/1Px6cK9-9346g0CbOYArYjr1jj6X9rvRcCpRi3s7HePN0POeCSvuF1Iagb-Wjiq78BnT

您现在可以通过Laravel的via方法向Discord webhook发送通知。

use SnoerenDevelopment\DiscordWebhook\DiscordMessage;
use SnoerenDevelopment\DiscordWebhook\DiscordWebhookChannel;

class DiscordNotification extends Notification
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed $notifiable The notifiable model.
     * @return array
     */
    public function via($notifiable)
    {
        return [DiscordWebhookChannel::class];
    }

    /**
     * Get the Discord representation of the notification.
     *
     * @param  mixed $notifiable The notifiable model.
     * @return \SnoerenDevelopment\DiscordWebhook\DiscordMessage
     */
    public function toDiscord($notifiable): DiscordMessage
    {
        return DiscordMessage::create()
            ->username('My Laravel App')
            ->content('The message body.')
            ->avatar('https://domain.com/avatar.jpg')
            ->tts(false);
    }
}

测试

$ composer test

致谢

许可协议

MIT许可协议。有关更多信息,请参阅LICENSE