I’m building a bot that monitors my computer for file changes and sends notifications through Telegram when something happens.
I decided to use Telethon even though it’s async, but I’m trying to make it work synchronously. My code successfully connects and sends one message in the start() function:
bot.send_message('me', 'Test message works!')
I also have a FileWatcher class with a method that triggers when files change. Right now it just shows a test message:
print("File changed!")
However, when I try to uncomment this line in my file watcher:
#bot.send_message('me', 'File was modified!')
I get runtime errors and both the message sending and console output stop working completely.
Error message:
RuntimeWarning: coroutine 'MessageMethods.send_message' was never awaited
bot.send_message('me', 'Test message works!')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
My code:
from telethon import TelegramClient
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import time
api_id = ****
api_hash = '****'
bot = TelegramClient('Session', api_id, api_hash)
class FileWatcher(FileSystemEventHandler):
def on_modified(self, event):
#bot.send_message('me', 'File was modified!')
print("File changed!")
def start():
bot.send_message('me', 'Test message works!')
watcher = Observer()
watcher.schedule(FileWatcher(), path='/home/user/Documents/', recursive=True)
watcher.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
watcher.stop()
watcher.join()
with bot:
bot.loop.run_until_complete(start())
if __name__ == '__main__':
start()
What am I doing wrong here?