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