Telegram Bot File Monitoring with Telethon Library Issues

I’m building a bot for Telegram that monitors file changes in a folder and sends notifications. The bot uses Telethon library and I’m having trouble with async operations.

When I test the bot, it connects fine and sends a welcome message in the start_bot() function:

telegram_client.send_message('me', 'Bot is running!')

I also have a file watcher class called FileWatcher that detects changes and shows a debug message:

print("File changed detected")

The problem happens when I try to send a telegram message from inside the file watcher. If I enable this line:

# telegram_client.send_message('me', 'File updated!')

I get runtime errors and both the message sending and console output stop working.

Here’s my complete code:

from telethon import TelegramClient
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import time

app_id = ****
app_secret = '****'

telegram_client = TelegramClient('session', app_id, app_secret)

class FileWatcher(FileSystemEventHandler):
    def on_modified(self, event):
        # telegram_client.send_message('me', 'File updated!')
        print("File changed detected")

def start_bot():
    telegram_client.send_message('me', 'Bot is running!')
    watcher = Observer()
    watcher.schedule(FileWatcher(), path='/home/user/Documents/', recursive=True)
    watcher.start()
    try:
        while True:
            time.sleep(2)
    except KeyboardInterrupt:
        watcher.stop()
        watcher.join()

with telegram_client:
    telegram_client.loop.run_until_complete(start_bot())

if __name__ == '__main__':
    start_bot()

The error message shows:

RuntimeWarning: coroutine 'MessageMethods.send_message' was never awaited

What am I doing wrong with the async handling?

you’re mixing sync and async contexts. watchdog runs sync, but telethon needs async. use asyncio.create_task() or better yet, try telegram_client.loop.create_task(telegram_client.send_message('me', 'File updated!')) inside ur file watcher method. thatll fix the coroutine warning.

The problem is send_message() returns a coroutine that needs awaiting, but your FileWatcher runs synchronously. I hit this same issue building a monitoring system last year.

Don’t try running async code from the sync handler. Use asyncio.run_coroutine_threadsafe() to bridge them:

import asyncio

class FileWatcher(FileSystemEventHandler):
    def on_modified(self, event):
        future = asyncio.run_coroutine_threadsafe(
            telegram_client.send_message('me', 'File updated!'),
            telegram_client.loop
        )
        future.result()  # Wait for completion
        print("File changed detected")

This runs the coroutine in the right event loop without blocking your file monitoring thread. Threading handles the sync between your file watcher and async telegram client.

Had the same setup and ran into this exact problem. Watchdog runs on its own thread but telethon needs the main event loop. Don’t try forcing sync/async communication - restructure instead.

Ditch watchdog and move your file monitoring into an async function. Use asyncio filesystem polling - just check file modification times in a loop with await asyncio.sleep(). Everything stays in the same async context as your telegram client.

Or use a queue system. Your sync file watcher pushes events to a queue, then your main async loop checks and processes them periodically. Much cleaner and avoids the threading mess that’s causing your coroutine warnings.