Keep C# Telegram bot active with periodic requests every quarter hour

I’m working on a C# Telegram bot that becomes inactive when there’s no user activity for a while. The bot seems to fall asleep or become unresponsive if it doesn’t receive any messages or commands for a certain period.

I want to implement an automatic ping or keep-alive feature that activates every 15 minutes to stop this issue. What would be the best way to do this? Should I use a timer, a background service, or another approach?

Here’s a simple example of what I have in mind:

public class BotKeepAlive
{
    private Timer keepAliveTimer;
    private TelegramBotClient botClient;
    
    public void StartKeepAlive()
    {
        keepAliveTimer = new Timer(SendPingRequest, null, TimeSpan.Zero, TimeSpan.FromMinutes(15));
    }
    
    private void SendPingRequest(object state)
    {
        // What code should I add here to keep the bot awake?
        botClient.GetMeAsync();
    }
}

I’d appreciate any advice on the most effective way to address this.

Been running telegram bots in production for years and your timer solution works great. Just don’t fire and forget that GetMeAsync call. Store the timer reference and handle cancellation tokens if your app needs clean shutdowns. Also, some hosting providers like Azure App Service have idle timeouts that a simple API ping won’t fix. On shared hosting, you’ll need always-on settings or a dedicated instance. The 15-minute interval works, but I use 10 minutes - more reliable for avoiding edge cases where the connection drops right after your ping.

your approach looks solid, but i’d use ihostedservice instead of a raw timer. better disposal handling and plays nicer with di. getmeasync() is perfect for keepalive - it’s lightweight and won’t spam users.

Your Timer approach looks good and should work fine here. I’ve done similar projects before - GetMeAsync() is ideal since it’s lightweight and won’t bug users. Just ensure to wrap your SendPingRequest method in a try-catch block around the GetMeAsync() call to handle any potential network issues. Remember to dispose of the timer when your application shuts down to avoid resource leaks. Additionally, consider logging the keepalive pings for debugging purposes later.

Why mess with timers and hosting when you can just automate it?

I had the same issue - bot kept going dormant. Instead of adding keepalive stuff to my C# code, I just moved everything to Latenode. Set up a workflow that pings the Telegram API every 15 minutes automatically.

Best part? No worrying about timers, disposal, or hosting headaches. Latenode handles the scheduling and keeps running even if your main app crashes. You can watch the pings from their dashboard too.

Just make a workflow with a 15-minute scheduled trigger, add a Telegram node that calls getMe, done. Takes 5 minutes to set up and never breaks.

Keeps your bot alive without cluttering your main app. Way cleaner.