【发布时间】:2020-10-28 00:31:20
【问题描述】:
我正在做一个 Telegram 机器人,如果桌面上的文件发生更改,它会向 Telegram 发送消息。
Telethon 是一个异步库,但是我排除了异步库,正在等待。程序连接电报服务器并向main()函数发送一次性测试消息。
client.send_message('me', 'Hello to myself!')
以及MyEventHandler 类中的一个函数,该函数在文件更改时运行并在控制台上显示测试消息
print("Tyu")
但是如果你取消注释该行
#client.send_message('me', 'myself!')
然后显示错误,client.send_message (“I”, “I!”) 和打印 (“Ty”) 失败。
谁能帮助理解这是怎么回事?
控制台输出:
RuntimeWarning: coroutine 'MessageMethods.send_message' was never awaited
client.send_message('me', 'Hello to myself!')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
程序代码:
from telethon import TelegramClient
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import time
api_id = ****
api_hash = '****'
client = TelegramClient('New', api_id, api_hash)
class MyEventHandler(FileSystemEventHandler):
def on_modified(self, event):
#client.send_message('me', 'myself!')
print("Tyu")
def main():
# Now you can use all client methods listed below, like for example...
client.send_message('me', 'Hello to myself!')
observer = Observer()
observer.schedule(MyEventHandler(), path='/Users/Apple/Desktop/', recursive=True)
# Start the observer
observer.start()
try:
while True:
# Set the thread sleep time
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
with client:
client.loop.run_until_complete(main())
if __name__ == '__main__':
main()
【问题讨论】:
标签: python telegram-bot telethon