Simon icon Simon
Flexible server monitoring

Notifications to Slack or Discord Webhook

As Twitter Notifications stopped working since the update to Mac OS 10.13 (at least for me), I'm wondering if there is any possibility to send notifications to a Discord or Slack Webhook? Thanks in advance.

David Sinclair's picture

Re: Notifications to Slack or Discord Webhook

I haven't looked into it, but if they have a URL you can send to via GET or POST, you should be able to use the Web notifier kind to send that. See the Post to Simon Wall built-in notifier for an example.

If they require authentication, that might complicate things.

Re: Notifications to Slack or Discord Webhook

Thanks for the input, I did a little research/try and error and now its working. In case someone else has the same problem: I use this PHP script to send the message to the Discord Webhook: https://github.com/AuroraAri/Discord-Webhooks-PHP

David Sinclair's picture

Re: Notifications to Slack or Discord Webhook

Thanks for sharing your solution!

Re: Notifications to Slack or Discord Webhook

Uhm okay after 1 day of trying with the script, I got some serious problems with the Simon App. I don't know whats the problem, but the program is freezing. In the beginning it happened after a few hours, now right after the start. I'm not able to change or do anything. I re-installed the app, deleted all files, but its happening again :-(

Re: Notifications to Slack or Discord Webhook

This is the whole script (from the github link) I used btw - can you have a look if there is any potentially source of error?

<?php
   
namespace DiscordWebhooks;
    class
Client
{
   
/*
     * The URL generated by Discord to receive webhooks
     * @var string $url The URL generated by Discord to receive webhooks
     */
   
protected $url = null;
   
/*
     * (Optional) The name that should be shown as the user sending the message
     * @var string $name The name that should be shown as the user sending the message
     */
   
protected $name = null;
   
/*
     * (Optional) The image to be used as the avatar for the user sending the message
     * @var string $avatar The image to be used as the avatar for the user sending the message
     */
   
protected $avatar = null;
   
/*
     * The message to be sent to Discord
     * @var string $message The message to be sent to Discord
     */
   
protected $message = null;
   
/*
     * Set up the class
     * @param string $url The URL generated by Discord to receive webhooks
     */
   
public function __construct($url) {
       
$this->url = $url;
    }
   
/*
     * (Optional) Set the name of the user sending the message; if not set, uses the name set in Discord
     * @param string $name The name that should be shown as the user sending the message
     */
   
public function name($name) {
       
$this->name = $name;
    }
   
/*
     * (Optional) Set the avatar of the user sending the message; if not set, uses the avatar set in Discord
     * @param string $avatar The image to be used as the avatar for the user sending the message
     */
   
public function avatar($avatar) {
       
$this->avatar = $avatar;
    }
   
/*
     * (Optional) Set the message to be sent to Discord
     * @param string $message The message to be sent to Discord
     */
   
public function message($message) {
       
$this->message = $message;
    }
    
/*
     * Sends a message through the webhook
     * @param string $message The message to send through the webhook
     */
   
public function send($message = null) {
       
$message = isset($message) ? $message : $this->message;
       
$url = $this->url;
       
$data = array(
           
'content' => $message,
           
'name' => $this->name,
           
'avatar_url' => $this->avatar,
        );
       
$data_string = json_encode($data);
       
$curl = curl_init();
       
curl_setopt($curl, CURLOPT_URL, $url);
       
curl_setopt($curl, CURLOPT_POST, 1);
       
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
       
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
       
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
       
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
       
$output = curl_exec($curl);
       
$output = json_decode($output, true);
        if (
curl_getinfo($curl, CURLINFO_HTTP_CODE) != 204) {
            throw new
Exception($output['message']);
        }
       
curl_close($curl);
        return
true;
    }
}
    use \
DiscordWebhooks\Client as DiscordWebhook;
   
   
$discord = new DiscordWebhook('https://discordapp.com/api/webhooks/**********');
   
$discord->message('Hi, Discord!'); // Optionally, set the message to be sent here. If not set, uses the message in $this->send().
   
$discord->send('**{TestName}** {TestStatusPhrase} {TestChangeDifferenceWithHTML} - {TestURL} '); // If the message wasn't set in $this->message, it must be set here. This method sends the message.
?>

David Sinclair's picture

Re: Notifications to Slack or Discord Webhook

I tried it, though perhaps didn't have a third-party dependency set properly.

Anyway, I didn't notice any issues. If Simon freezes again, try launching Activity Monitor (from /Applications/Utilities) and do a Sample of Simon while frozen, and send me that. It might provide a clue.

Another option could be to put the PHP script in an external file, either on a local or remote web server, and call it as a web page with the Web (HTTP) service, passing the parameters and getting them via $_GET['testname'] etc in the script.