discord icon indicating copy to clipboard operation
discord copied to clipboard

usage with discord webhooks?

Open hcyildirim opened this issue 4 years ago • 15 comments

title is self-explanatory but i'm gonna ask it again. can i use this package with discord webhooks? (seems like i can't) i want to able it for my users to write down a discord url and receive messages.

hcyildirim avatar Aug 17 '19 12:08 hcyildirim

Unfortunately this package doesn't currently have support for sending messages via webhooks. I've been meaning to add that functionality, I just haven't had the time.

codyphobe avatar Aug 18 '19 19:08 codyphobe

I'm also interested in using webhooks. I want to start using this package but I never used or created a discord bot. It seems you're supposed to host a bot but I don't know how to and I can't find any tutorials how to do that and link it to this package.

Just a quick question for clarification, are webhooks and bots different in terms of sending notifications? or they're the same?

Because today I was setting up a monitoring tool called netdata and for discord notifications, it only needed a webhook url, that's all. It didn't ask for bots or anything like that.

HassanZahirnia avatar Aug 28 '19 17:08 HassanZahirnia

I don't know the difference between bot and webhook notification but I needed a simple discord webhook so I forked a repository and created this, you can check it out and see if it meets your requirements.

hcyildirim avatar Aug 28 '19 19:08 hcyildirim

You can use bot for notify your channel as well.

For example: image

In your config/services.php put and then set your .env file: image

And you need follow this instruction to get BOT_API_TOKEN and CHANNEL_ID on Discord.

michaloravec avatar Aug 28 '19 19:08 michaloravec

@ccoeder I will have a look, thanks for the link! @michaloravec The problem is I don't know how to run a discord bot on my server. I did some googling and discord.js came up ( a node js application ). but I don't know what kind of code am i supposed to put in the node file. and how it integrates/communicates with this package.

HassanZahirnia avatar Aug 28 '19 20:08 HassanZahirnia

With node.js I don't have experience so I can't advise you with it.

michaloravec avatar Aug 28 '19 20:08 michaloravec

@michaloravec Ok I did a big mistake i think. There is a part in the readme where it says "Add the bot to your server" I thought I'm supposed to run some app on my server to host the bot. I didn't realize it's talking about adding the bot to my discord server.

I just created the bot, added it to my discord server, and installed this package. I also ran the php artisan discord:setup and the output looks ok ( the bot is identified and can send api requests ).

Now I'm struggling on how to make laravel-backup use this channel to send notifications to discord.

HassanZahirnia avatar Aug 28 '19 21:08 HassanZahirnia

Ok great, I think you can use second parameter of DiscordMessage::create($body, $embed), which is embed. And you can use it something like that:

image

There is documentation for this embed part https://discordapp.com/developers/docs/resources/channel#embed-object

In this section is mentioned about content, embed and file, but this package use only content and embed.

So the only possibility is use url to your file, like on picture above.

michaloravec avatar Aug 28 '19 21:08 michaloravec

@michaloravec Thanks a lot for the pointers and the help! I'm gonna look stupid here but actually I'm few steps behind that. This is because I'm not familiar with the concept of notifications in Laravel.

I used the code you posted in here and it worked. The bot sent a message on the channel successfully! So the purpose of the package here is fulfilled.

From here on it's about getting it to work with laravel-backup which I know is not in the context of this repo/issue. But I will write it down in case someone is kind enough to help further more.

How do I actually connect the discord notification channel to the laravel-backup ? I followed this tutorial to add a custom notification channel but it's not working.

HassanZahirnia avatar Aug 28 '19 22:08 HassanZahirnia

No luck here so far. I realized there are 2 ways of sending notification. One is per model/user so to speak, and the other is on-demand which is what I'm looking for just like the one michaloravec showed here.

I don't want to send discord message to any specific user or model. I want to send messages to a specific discord channel.

But I keep hooking the laravel-backup's notification to discordchannel like so:

// config/backup.php

use \NotificationChannels\Discord\DiscordChannel as DiscordChannel;

'notifications' => [
            \App\Notifications\CleanupWasSuccessful::class => [DiscordChannel::class],
            \App\Notifications\CleanupHasFailed::class => [DiscordChannel::class],

and I implement my notification classes like this:

// app/Notifications/CleanupHasFailed.php

namespace App\Notifications;

use Spatie\Backup\Notifications\Notifications\CleanupHasFailed as BaseNotification;
use NotificationChannels\Discord\DiscordMessage;

class CleanupHasFailed extends BaseNotification
{
public function toDiscord($notifiable)
    {
        return DiscordMessage::create('message test');
    }
}

I know this is wrong but don't know how to fix it. this is not on-demand and it doesn't know the channel i want the message to be sent.

HassanZahirnia avatar Aug 29 '19 11:08 HassanZahirnia

Create your own DiscordChannel class where just change this row https://github.com/laravel-notification-channels/discord/blob/master/src/DiscordChannel.php#L34

Like: image

michaloravec avatar Aug 29 '19 11:08 michaloravec

@michaloravec Thanks buddy! it worked!

HassanZahirnia avatar Aug 29 '19 12:08 HassanZahirnia

Hey all!

We'd love to accept a PR if anyone is interested :)

atymic avatar Sep 27 '19 23:09 atymic

@michaloravec What was the full code of what you've written there? I'm facing a similar issue, I've got the Discord package installed and each user in my project can have a Discord webhook URL on their profile, this is what I've created: App\Notifications\DiscordChannel.php as per your recommendations:

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;

class DiscordChannel
{
    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return void
     */
    public function send($notifiable, Notification $notification)
    {
        // test
        $channel = 'https://discord.com/channels/1005783872686018620/1005783872686018623';

        $message = $notification->toDiscord($notifiable);

        return $this->discord->send($channel, [
            'content' => $message->body,
            'embed' => $message->embed,
        ]);
    }
}

But with this, an error is thrown:

ErrorException: Undefined property: App\Notifications\DiscordChannel::$discord in C:\Users\Ryan\Desktop\web-projects\domain-monitor\domain-monitor-api\app\Notifications\DiscordChannel.php:22

sts-ryan-holton avatar Aug 09 '22 17:08 sts-ryan-holton

You have to extend the original class NotificationChannels\Discord\DiscordChannel

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\Discord\DiscordChannel as Channel;

class DiscordChannel extends Channel
{
    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return void
     */
    public function send($notifiable, Notification $notification)
    {
        // test
        $channel = 'https://discord.com/channels/1005783872686018620/1005783872686018623';

        $message = $notification->toDiscord($notifiable);

        return $this->discord->send($channel, [
            'content' => $message->body,
            'embed' => $message->embed,
        ]);
    }
}

By the way, I think that $channel has to be only a number (id) and not a full url.

michaloravec avatar Aug 09 '22 22:08 michaloravec