Giter VIP home page Giter VIP logo

Comments (15)

michaloravec avatar michaloravec commented on June 11, 2024 4

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

from discord.

michaloravec avatar michaloravec commented on June 11, 2024 2

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.

from discord.

HassanZahirnia avatar HassanZahirnia commented on June 11, 2024 1

@michaloravec Thanks buddy! it worked!

from discord.

codyphobe avatar codyphobe commented on June 11, 2024

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.

from discord.

HassanZahirnia avatar HassanZahirnia commented on June 11, 2024

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.

from discord.

hcyildirim avatar hcyildirim commented on June 11, 2024

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.

from discord.

HassanZahirnia avatar HassanZahirnia commented on June 11, 2024

@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.

from discord.

michaloravec avatar michaloravec commented on June 11, 2024

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

from discord.

HassanZahirnia avatar HassanZahirnia commented on June 11, 2024

@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.

from discord.

michaloravec avatar michaloravec commented on June 11, 2024

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.

from discord.

HassanZahirnia avatar HassanZahirnia commented on June 11, 2024

@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.

from discord.

HassanZahirnia avatar HassanZahirnia commented on June 11, 2024

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.

from discord.

atymic avatar atymic commented on June 11, 2024

Hey all!

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

from discord.

sts-ryan-holton avatar sts-ryan-holton commented on June 11, 2024

@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

from discord.

michaloravec avatar michaloravec commented on June 11, 2024

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.

from discord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.