I have a telegram client, implemented using the telethon library, I need to run a certain function twice a day (in it, my client writes a message to the recipient, then I process all the information using a message handler) To run it, I plan to use aioschedule Python version: 3.11. 9
import aioschedule as schedule
import asyncio
import config
import time
from telethon import TelegramClient, events, sync
from datetime import datetime
# Подготовка постов
async def preparation_posts():
print("Подготовка запросов пошла")
for auto in autos:
for i in range(1, len(auto)):
await ask_topic(auto[0], auto[i])
# Запрос темы для поста
async def ask_topic(nameAuto, model):
...
# Запрос информации для поста
async def ask_info(nameAuto, model):
...
# Отправка готового поста
async def send_post(nameAuto, model):
...
# Обработчик сообщений от @chatsgpts_bot
@client.on(events.NewMessage(chats=('@chatsgpts_bot')))
async def chatGPT_messages_handler(msg):
But when starting the program, an error appears due to the fact that I am calling an asynchronous function outside of any function, I need to fix it somehow) The problem is that I have a client running in parallel, I need to run both functions and for the bot to work)
When I try to run the program an error appears
Error text:
Traceback (most recent call last):
File "/Users/gutark/Documents/Проекты/telegram боты/GPT генератор/main.py", line 114, in <module>
schedule.every().day.at("13:34").do(preparation_posts())
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aioschedule/__init__.py", line 436, in do
self.job_func = functools.partial(job_func, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: the first argument must be callable
sys:1: RuntimeWarning: coroutine 'preparation_posts' was never awaited
gutark@MacBook-Air-Stepan GPT генератор % python3 main.py
File "/Users/gutark/Documents/Проекты/telegram боты/GPT генератор/main.py", line 119
await loop.run_until_complete(schedule.run_pending())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside function
I will be very grateful for your help 🙂
Stepan Khlon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.