Telegram bot for pinging ip address

hello! I need a script that will ping my ip address and send if it's enable via telegram. I'vew already made a script using c++.
so what i need to do to run it, i'm a beginner and don't know. So the main reason why i need to ping my ip is to check if i have the light by checking the enable of router, but i don't know how to run that script on my router and even will it work or no(if there is no electricity then router is turned off).What am i supposed to do, maybe use some cloud resources?

Run a loop, post telegram message if there's no reply for X minutes, sleep X minutes retry, perhaps post another message.

yeap, i already made a script, how can i run it on my router. Just if I have no electricity at home, than both router and PC are turned off. The bot still working?

you said you wanted to ping your Ip, that would indicate doing it from outside.

reverse it ?
have the router message you every X minutes as long as there's power, if there's no message within the regular time slot, you either have no power, or your ISP is having issues.

but there should be a service online, providing this kind of basic monitoring for free.
perhaps not reporting via telegram though.

register a DDNS and use https://www.downnotifier.com/ or perhaps https://uptimerobot.com/
could also imagine there's a free app for doing this kind of stuff too.

1 Like

i have the script for windows, so now i need to run it on linux? and i can`t find any answers how i need to run the program on router, how i can add it to router system, maybe you can recommend some resources where people explain how to do it

something like this

#!/bin/sh

while( true )
do
ping -q -w 5 -c 1 8.8.8.8
if [ "$?" -eq 0 ]; then
  curl -s -o /dev/null -X POST -H "Content-Type: application/json" -d "{\"chat_id\": \"-100XXXXXXXXXX\", \"text\": \"test_message\", \"disable_notification\": false}" https://api.telegram.org/bot{API_TOKEN}/sendMessage
else                                              
  sleep 600
fi
done

thanks for ther script, but i have one, i need to make it work on router.Maybe i`m a bad explainer but i have my application on wondows, what i need to do to run it on router?

#include<iostream>
#include <stdio.h>
#include <tgbot/tgbot.h>
using namespace std;
using namespace TgBot;
int main() {
    Bot bot("6010182175:AAHQy3qPN-Erg0W0wvLKEX1Ook2fWbbhhjo");
    bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
        bot.getApi().sendMessage(message->chat->id, "Hi!");
        });
    bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
        printf("User wrote %s\n", message->text.c_str());

       
     cout << "lf" << endl;
    bool check = false;
    while (true) {
        int x = system("ping 91.196.148.96");
        if (x == 0) {
            if (check == false) {
                check = true;
                bot.getApi().sendMessage(message->chat->id, "light");
            }
        }
        else {
            if (check == true) {
                check = false;
                bot.getApi().sendMessage(message->chat->id, "no light");
            }
        }
        Sleep(5000);
    }
        });

    try {
        printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
        TgLongPoll longPoll(bot);
        while (true) {
            printf("Long poll started\n");
            longPoll.start();
        }
    }
    catch (TgException& e) {
        printf("error: %s\n", e.what());
    }
    return 0;
}

(probably want to remove the bot name from your last post, unless it's bogus.)

then you need to set up a dev environment, and cross compile it, it'l take a lot longer,
unless your router runs x86.

knock yourself out

1 Like

BTW, that's not a "script", it's source code.

2 Likes

and you're really overthinking this, @Misha.

1 Like

why, should you explain? I just install openwrt and linux for that purpose without any additional knowledge of it, if it`s not hard for you?

or just use that script?

it's not a question whether it's hard for me, but if it is for you ...

have you already studied the links provided ?

would that be your C code, or my shell script ?

1 Like

Can you explain the issues you're having with OpenWrt?

Also, feel free to ask questions. You posted source code, so it's difficult to determine your level of expertise.

yeah, i read them before asking, but some errors appeared whick i can`t solve. For me it will be better to run my C code. So i tried to undesrand what i need to make it work. I tried to follow that https://openwrt.org/docs/guide-developer/helloworld/start instructions, but in the very beginning some packages give error(i can provide screens if you need?).

I am a complete begginer in linux, but have some experience with c++, so write my code using it. The main problem is to understand what am i supposed to do next. I just need to ping the ip address every n seconds, but i don`t know work it or not if i have no electricity at home, because earlier i just run all the code in sandbox(just in a visualstudio compiler)

If you don’t have power then there’s no way for your computer or router to be operating?

1 Like

problem isn't the alerts, it's the code you're trying to run on the router,
if you want to have it run elsewhere, your problem isn't with openwrt.

you've already have been provided with alternative solutions.

and we still don't know what device you've got.

1 Like

The same as on any system, compile it.

???

Perhaps this is a language/translation anamoly, as I incorrectly understood you want to run software on a device that has no power.

OK, I'm not 'trippin.

You'd compile it. See above.

exactly, every n hours a day i have electricity restrictions. That`s why i need to check if i have light at home or no. And the main problem that nothing working at that time, the same result if you pull your pc and router from the socket