[英]Python Telethon - Send messages at timed intervals
I'm trying to send a message to my group at defined time intervals, but I get a warning in the output the first time I try to send the message.我正在尝试按定义的时间间隔向我的群组发送消息,但我在第一次尝试发送消息时收到 output 中的警告。 Next times no warning, but nothing is posted in the group.下一次没有警告,但没有在组中发布任何内容。 I'm the owner of the group so in theory there shouldn't be any permissions issues.我是该组的所有者,因此理论上不应该有任何权限问题。
Code代码
from telethon import TelegramClient
import schedule
def sendImage():
apiId = 1111111
apiHash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
phone = "+111111111111"
client = TelegramClient(phone, apiId, apiHash)
toChat = 1641242898
client.start()
print("Sending...")
client.send_file(toChat, "./image.jpg", caption="Write text here")
client.disconnect()
return
def main():
schedule.every(10).seconds.do(sendImage)
while True:
schedule.run_pending()
if __name__ == "__main__":
main()
Output Output
Sending...
RuntimeWarning: coroutine 'UploadMethods.send_file' was never awaited
client.send_file(toChat, "./image.jpg", caption="Write text here")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Sending...
Sending...
Sending...
Telethon uses asyncio
, but schedule
wasn't designed with asyncio
in mind. Telethon 使用asyncio
,但schedule
在设计时并没有考虑到asyncio
。 You should consider using an asyncio
-based alternative to schedule
, or just use Python's builtin functions in the asyncio
module to "schedule" things:您应该考虑使用基于asyncio
的替代方案来替代schedule
,或者只使用asyncio
模块中的 Python 内置函数来“安排”事情:
import asyncio
from telethon import TelegramClient
def send_image():
...
client = TelegramClient(phone, apiId, apiHash)
await client.start()
await client.send_file(toChat, "./image.jpg", caption="Write text here")
await client.disconnect()
async def main():
while True: # forever
await send_image() # send image, then
await asyncio.sleep(10) # sleep 10 seconds
# this is essentially "every 10 seconds call send_image"
if __name__ == "__main__":
asyncio.run(main())
You should also consider creating and start()
ing the client inside main
to avoid recreating it every time.您还应该考虑在main
中创建和start()
ing 客户端,以避免每次都重新创建它。
This means that you do not give time for the operation to be done, try these changes:这意味着您没有时间完成操作,请尝试以下更改:
from telethon import TelegramClient
import schedule
async def sendImage(): # <- make it async
apiId = 1111111
apiHash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
phone = "+111111111111"
client = TelegramClient(phone, apiId, apiHash)
toChat = 1641242898
client.start()
print("Sending...")
await client.send_file(toChat, "./image.jpg", caption="Write text here") # <- here too add await
client.disconnect()
return
def main():
schedule.every(10).seconds.do(client.loop.run_until_complete(sendImage))
while True:
schedule.run_pending()
if __name__ == "__main__":
main()
other thing i don't think that you should keep connecting and disconnecting, in my opinion client.start() should be out of this function and client.disconnect too另一件事我认为你不应该继续连接和断开连接,在我看来 client.start() 应该不在这个 function 和 client.disconnect 中
As the output says you need to await the response of the coroutine.正如 output 所说,您需要等待协程的响应。 The code may trigger exceptions which should be handled.该代码可能会触发应处理的异常。
try:
client = TelegramClient(...)
client.start()
except Exception as e:
print(f"Exception while starting the client - {e}")
else:
try:
ret_value = await client.send_file(...)
except Exception as e:
print(f"Exception while sending the message - {e}")
else:
print(f"Message sent. Return Value {ret_value}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.