discord
discord copied to clipboard
usage with discord webhooks?
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.
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.
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.
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.
You can use bot for notify your channel as well.
For example:
In your config/services.php put and then set your .env file:
And you need follow this instruction to get BOT_API_TOKEN and CHANNEL_ID on Discord.
@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.
With node.js I don't have experience so I can't advise you with it.
@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.
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:
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 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.
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.
Create your own DiscordChannel class where just change this row https://github.com/laravel-notification-channels/discord/blob/master/src/DiscordChannel.php#L34
Like:
@michaloravec Thanks buddy! it worked!
Hey all!
We'd love to accept a PR if anyone is interested :)
@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
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.