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. 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
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...
3
Answers
As the output says you need to await the response of the coroutine. The code may trigger exceptions which should be handled.
Telethon uses
asyncio
, butschedule
wasn’t designed withasyncio
in mind. You should consider using anasyncio
-based alternative toschedule
, or just use Python’s builtin functions in theasyncio
module to "schedule" things:You should also consider creating and
start()
ing the client insidemain
to avoid recreating it every time.This means that you do not give time for the operation to be done, try these changes :
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