Telegram bot sending automatic notifications

Hey guys, I need some help with my Telegram bot. I’m trying to get it to send notifications to users based on their ID. However, when it starts checking for new data and sending out alerts, everything freezes, stopping users from making further requests.

I attempted to resolve this by running the update function in a separate thread, but now I keep encountering a ‘NoneType’ error. Here’s a revised snippet of my code:

import telebot
import threading
from aiogram import Bot, Dispatcher, types

bot = Bot(token='your_token_here')
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def notify_user(message: types.Message):
    user_id = message.from_user.id
    await message.reply(f'Your ID is: {user_id}')
    threading.Thread(target=send_updates, args=(user_id,), daemon=True).start()


def send_updates(user_id):
    # Placeholder for code to fetch and send updates
    pass

if __name__ == '__main__':
    from aiogram import executor
    executor.start_polling(dp, skip_updates=True)

When I run it, the console confirms that the thread has started, but no notifications reach the user. I’m stuck with a ‘NoneType’ error. Any advice on how to resolve this would be greatly appreciated. Thanks!

yo, i had a similar problem with my bot. try using asyncio.gather() to run multiple coroutines concurrently. it helped me avoid freezing issues. something like:

async def main():
    await asyncio.gather(
        dp.start_polling(),
        send_updates(user_id)
    )

if __name__ == '__main__':
    asyncio.run(main())

this way both functions run simultaneously without blockin eachother. hope it helps!

I’d suggest considering a different approach altogether. Instead of sending notifications directly from your bot, you could implement a message queue system like RabbitMQ or Redis. This would allow you to decouple the notification sending process from your main bot logic.

Here’s how it might work:

  1. Your bot receives a command and adds a notification task to the queue.
  2. A separate worker process continuously checks the queue for new tasks.
  3. The worker handles sending notifications, leaving your bot free to process other commands.

This architecture scales well and prevents your bot from getting bogged down with notification tasks. It also provides better error handling and retry mechanisms for failed notifications.

If you’re not familiar with message queues, there are simpler alternatives like using a database to store pending notifications and a separate script to process them. The key is separating the notification logic from your main bot process.

I’ve faced similar issues with Telegram bots before, and I think I see what’s going on here. The problem likely stems from mixing the telebot and aiogram libraries, which can cause conflicts.

Instead of using threading, I’d recommend sticking with aiogram’s built-in asynchronous capabilities. Here’s a modified version that should work better:

from aiogram import Bot, Dispatcher, types
import asyncio

bot = Bot(token='your_token_here')
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_command(message: types.Message):
    user_id = message.from_user.id
    await message.reply(f'Your ID is: {user_id}')
    asyncio.create_task(send_updates(user_id))

async def send_updates(user_id):
    while True:
        # Your update logic here
        await bot.send_message(user_id, 'This is a test update')
        await asyncio.sleep(60)  # Wait for 60 seconds before next update

if __name__ == '__main__':
    from aiogram import executor
    executor.start_polling(dp, skip_updates=True)

This approach uses asyncio to handle the updates without blocking the main thread. It should resolve your NoneType error and keep your bot responsive. Remember to adjust the update logic and timing to suit your needs.