Results 1 to 13 of 13

Thread: Notification when your bot goes down (via discord)

  1. #1
    Join Date
    Oct 2012
    Posts
    1,258
    Mentioned
    40 Post(s)
    Quoted
    588 Post(s)

    Default Notification when your bot goes down (via discord)

    //Option 1 is easy to explain and so I've done the write up for it now to go with the video recorder 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/attachmen...1_01-47-28.mp4

    Option 1. Sending a discord notification on terminate

    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
    Simba Code:
    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.
    Simba Code:
    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/do...ources/webhook
    and here
    https://birdie0.github.io/discord-we...d_webhook.html


    Option 2. Sending a discord notification from the monitoring server once your script stops messaging the server

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

  2. #2
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    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)
    Python Code:
    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

  3. #3
    Join Date
    Feb 2013
    Location
    The Boonies
    Posts
    203
    Mentioned
    9 Post(s)
    Quoted
    70 Post(s)

  4. #4
    Join Date
    Dec 2011
    Posts
    2,147
    Mentioned
    221 Post(s)
    Quoted
    1068 Post(s)

  5. #5
    Join Date
    May 2019
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I use to push notification each time now this is super cool. Thanks for the information.

  6. #6
    Join Date
    Nov 2010
    Posts
    305
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    Hi and thank you @acow!

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

    Simba Code:
    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
    Simba Code:
    SendDiscordWebhook(+'Not mining'); //Notice the '+' in front

    Result
    s8TR2WB.png

  7. #7
    Join Date
    Jan 2012
    Location
    Sydney, Australia
    Posts
    877
    Mentioned
    12 Post(s)
    Quoted
    368 Post(s)

    Default

    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.

    Simba Code:
    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.


  8. #8
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,011
    Mentioned
    50 Post(s)
    Quoted
    505 Post(s)

    Default

    Quote Originally Posted by Dan the man View Post
    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?

  9. #9
    Join Date
    Jan 2012
    Location
    Sydney, Australia
    Posts
    877
    Mentioned
    12 Post(s)
    Quoted
    368 Post(s)

    Default

    Quote Originally Posted by Citrus View Post
    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?

  10. #10
    Join Date
    Jan 2012
    Location
    Sydney, Australia
    Posts
    877
    Mentioned
    12 Post(s)
    Quoted
    368 Post(s)

    Default

    Quote Originally Posted by Citrus View Post
    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.
    Last edited by Dan the man; 07-24-2019 at 01:14 AM.

  11. #11
    Join Date
    Oct 2012
    Posts
    1,258
    Mentioned
    40 Post(s)
    Quoted
    588 Post(s)

    Default

    Quote Originally Posted by Dan the man View Post
    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/r...-fixes.x86.exe

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

  12. #12
    Join Date
    Jan 2012
    Location
    Sydney, Australia
    Posts
    877
    Mentioned
    12 Post(s)
    Quoted
    368 Post(s)

    Default

    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

  13. #13
    Join Date
    Jul 2022
    Posts
    11
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Couldnt get this to work

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •