PDA

View Full Version : Notification when your bot goes down (via discord)



acow
07-31-2018, 06:32 AM
//Option 1 is easy to explain and so I've done the write up for it now to go with the video recorder (https://villavu.com/forum/showthread.php?t=118373) thread. Option 2 I'll write up later.

When you're running bots it's generally quite nice if you can be notified whenever your bots stops running, rather than having to manually check up on them to see what's up.

There's 2 main options for getting a notification whenever your script stops:
1) simply sending a notification from the script itself, on terminatation
2) maintaining a heartbeat connection and firing the notification whenever the script fails to tell the monitoring server it's alive. See: https://en.wikipedia.org/wiki/Heartbeat_(computing)
Option 1 is super quick & easy to implement but not as reliable. Option 2 is very reliable but not as quick & easy to implement.

Discord makes it pretty simple to send notifications via their webhooks, so I will be using discord notifications as the examples for this guide. These ideas appy across other mediums such as email, text msg, skype, slack, phone call playing a prerecorded message saying "beep boop your bot is down", etc. The implementations would however ofcourse be different.

In order to send a notification to yourself on discord via simba, you will need a discord webhook url.
Here's a video demonstration on how to get a discord webhook url https://streamable.com/m6x4f
Alternative source of video: https://cdn.discordapp.com/attachments/233405586698403841/473728638744002560/2018-07-31_01-47-28.mp4


You will need to be able to send a post request to the webhook url with the webhook JSON.
Here's one possible way to write a simba procedure to do that

Procedure SendDiscordWebhook(WebhookURL, JSON: string);
var
HTTPClient: Integer;
begin
HTTPClient := InitializeHTTPClient(False);
PostHTTPPage(HTTPClient, WebhookURL, JSON);
FreeHTTPClient(HTTPClient);
end;


Here's how you might use such a procedure in a script.

const
DiscordWebhookURL = '';
DiscordWebhookJSON = '{"content" : "Text goes here"}';

Procedure SendDiscordWebhook(WebhookURL, JSON: string);
var
HTTPClient: Integer;
begin
HTTPClient := InitializeHTTPClient(False);
PostHTTPPage(HTTPClient, WebhookURL, JSON);
FreeHTTPClient(HTTPClient);
end;

Procedure Terminator();
begin
SendDiscordWebhook(DiscordWebhookURL, DiscordWebhookJSON);
end;

begin
AddOnTerminate('Terminator'); //whenever the script terminates [safely], the Terminator() procedure will be called

//rest of the script
end.


Try it out. Just put that into code simba, add your webhook url to "DiscordWebhookURL", and run the code. You should see "Text goes here" submitted into your text channel.

More info on discord webhook JSON parameters can be found here (incase you want anything fancier than just simple text messages)
https://discordapp.com/developers/docs/resources/webhook
and here
https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html



//todo, later

Implementation for this is already created and tested, just gotta do the write up explaining how to repeat my steps. Implementation for the monitoring server will be in python (using flask).

Olly
07-31-2018, 09:28 PM
Cool! Never thought about using webhooks. That's much easier than the route I went down. (made a python script which I packaged into a .exe to call from Simba)

import discord
import asyncio
import sys
import os.path

client = discord.Client()

@client.event
@asyncio.coroutine
def on_ready():
yield from client.wait_until_ready()

channel = discord.Object(id=sys.argv[1])
if os.path.exists(sys.argv[3]):
yield from client.send_file(channel, sys.argv[3])
else:
yield from client.send_message(channel, sys.argv[3])


yield from client.logout()

client.run(sys.argv[2])

# discord_msg.exe "CHANNEL" "TOKEN" "MESSAGE / IMAGE PATH"
# pyinstaller for exe

Lama
08-01-2018, 12:36 AM
Ayy super cool! Can't wait to see your write up on the heartbeat one ;)

Clarity
08-01-2018, 01:47 AM
Nice acow;

I personally used push notifications (Pushover app) but this ends up being better since we have Discord on both PC and phone!

vodalternatives
05-22-2019, 11:35 AM
I use to push notification each time now this is super cool. Thanks for the information.

Failure
07-19-2019, 06:59 PM
Hi and thank you acow!

I changed it a bit for personal use (and perhaps easier to use)


const DiscordWebhookURL := 'url';

Procedure SendDiscordWebhook(JSON: string);
var
HTTPClient: Integer;
begin
HTTPClient := InitializeHTTPClient(False);
PostHTTPPage(HTTPClient, DiscordWebhookURL, '{"content" : "['+ FormatDateTime('tt', Time) + ']\n' + JSON + '"}'); //Just added current time
FreeHTTPClient(HTTPClient);
end;


Usage

SendDiscordWebhook(+'Not mining'); //Notice the '+' in front


Result
28665

Dan the man
07-21-2019, 07:15 AM
Hey guys.

I have tried doing the above examples without any luck.

Anyone mind telling me what I am doing wrong?
Not getting any data coming through on the channel at all.

program new;
const
DiscordWebhookURL = 'http://discordapp.com/api/webhooks/602319574863708171/gVoLTLYGghVdhz_XX-6sabstnvOtRdGyOhkhUhpUW0yG0O9TH_74SFgmxTlA99qnyaj8 ';
DiscordWebhookJSON = '{"content" : "Test"}';

Procedure SendDiscordWebhook(WebhookURL, JSON: string);
var
HTTPClient: Integer;
begin
HTTPClient := InitializeHTTPClient(False);
writeln(HTTPClient);
PostHTTPPage(HTTPClient, WebhookURL, JSON);
writeln(GetHTTPPage(HTTPClient, DiscordWebhookURL));
FreeHTTPClient(HTTPClient);
end;

Procedure Terminator();
begin
SendDiscordWebhook(DiscordWebhookURL, DiscordWebhookJSON);
end;

begin
AddOnTerminate('Terminator'); //whenever the script terminates [safely], the Terminator() procedure will be called
SendDiscordWebhook(DiscordWebhookURL, DiscordWebhookJSON);
//rest of the script
end.

https://i.imgur.com/hSO00rR.png

Citrus
07-21-2019, 02:31 PM
Hey guys.

I have tried doing the above examples without any luck.

Anyone mind telling me what I am doing wrong?
Not getting any data coming through on the channel at all.

That's weird. I copy/pasted your code and just replaced the url with my own and it worked perfectly. Tried with your url and it failed. Maybe try to generate a new webhook url?

Dan the man
07-21-2019, 03:20 PM
That's weird. I copy/pasted your code and just replaced the url with my own and it worked perfectly. Tried with your url and it failed. Maybe try to generate a new webhook url?

Yeah I tried that on a few different servers I made with the same result. Very unusual. Any other Discord settings I am not thinking of?

Dan the man
07-22-2019, 03:51 AM
That's weird. I copy/pasted your code and just replaced the url with my own and it worked perfectly. Tried with your url and it failed. Maybe try to generate a new webhook url?

Would u mind sharing the url that worked for you?
Will allow me to eliminate my Simba client as an issue.

EDIT: If you are having the same issue as me, the problem was Simba 1.2. There is a bug that prevents it from sending the hook. Use Simba 1.3 or 1.1.

acow
07-31-2019, 10:17 PM
EDIT: If you are having the same issue as me, the problem was Simba 1.2. There is a bug that prevents it from sending the hook. Use Simba 1.3 or 1.1.

That's surprising since iirc only simba 1.2 was used for developing this and the video recorder. If you're using 1.2, the version by slacky is probably the one you should be using. https://github.com/slackydev/Simba/releases/download/Simba-1.2.0-rc6_fixes/Simba1206-fixes.x86.exe

if it works for 1.3, which it seems to, that's probably the way to go currently.

Dan the man
08-01-2019, 02:13 AM
Just tested that version and it didnt work.

After ainstakingly removing the reliance on aerolib from my include, i have managed to move it to 1.3 and it works flawlessly :D

the kappin
07-15-2022, 08:07 PM
Couldnt get this to work