Comments (15)
Create your own DiscordChannel class where just change this row https://github.com/laravel-notification-channels/discord/blob/master/src/DiscordChannel.php#L34
from discord.
You can use bot for notify your channel as well.
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.
from discord.
@michaloravec Thanks buddy! it worked!
from discord.
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.
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.
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.
@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.
With node.js I don't have experience so I can't advise you with it.
from discord.
@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.
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.
from discord.
@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.
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.
Hey all!
We'd love to accept a PR if anyone is interested :)
from discord.
@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.
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)
- HTTP error: 403: Cannot send messages to this user HOT 1
- Discord responded with an HTTP error: 403: Missing Access
- Laravel 8 Support HOT 1
- Discord error response data discarded HOT 1
- PHP 8.0 support HOT 1
- Laravel doesn't identify the bot HOT 1
- DiscordPHP will not run on a webserver. Please use PHP CLI to run a DiscordPHP bot.
- README needs improving.
- Config not recognized ?!?!
- Laravel 9 Compatibility HOT 2
- Dynamic discord channel for each Notification class? HOT 4
- Driver [Discord] not supported HOT 1
- Discord webhook support per user
- Send embed message with image
- Retry sending a notification if you're being rate limited by Discord HOT 1
- Blank Fields Not working
- Discord Bot Always Offline HOT 1
- Getting `400 Bad Request` response: {\"embed\": [\"Expected an object/dictionary.\"]} HOT 8
- laravel 11 support
- Laravel 11.x support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from discord.