2

I used the library aioschedule to send autoscheduled messages to concrete users in telegram bot. The logic is: if the column 'alert' in y_data is 1, the alert will be sent to users from the list GOOD_ID.

The error is:

ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-10' coro=<Job.run() done, defined at /opt/anaconda3/lib/python3.8/site-packages/aioschedule/__init__.py:455> exception=TypeError("job() missing 1 required positional argument: 'message'")>
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.8/site-packages/aioschedule/__init__.py", line 462, in run
    ret = await self.job_func()
TypeError: job() missing 1 required positional argument: 'message'

I don't get how to avoid this mistake, can you help me with that, please?

The code:

bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
chat = []

GOOD_ID = [1, 2, 3]

async def job(message: types.Message):
    if message.chat.id == chat[0] or message.chat.id in GOOD_ID:
        y_data = yesterday_data(df)
        if y_data['alert'].values == 1:
            await bot.send_message(
                message.chat.id,
                "Alert!",parse_mode='html', reply_markup=markup
            )
    else:
        await bot.send_message(message.chat.id, "You have no access")


async def scheduler():
    aioschedule.every().day.at("15:00").do(job)

    # loop = asyncio.get_event_loop()
    while True:
        await aioschedule.run_pending()
        await asyncio.sleep(2)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    task = loop.create_task(scheduler())
    executor.start_polling(dp, skip_updates=True)
  • You are missing the parameter when calling job function. Did you try adding function parameter in do call? For example: `aioschedule.every().day.at("15:00").do(job, 'scheduled call!')`. Another option is creating a function that takes no parameter and call that function from schedule _do_ call, and from that function call _job_ function with the parameters you need there. Let me know if that works. – agalluzzi Mar 14 '22 at 14:40
  • I tried the first case and it didn't help. I got the error: _File "bot\_.py", line 53, in job if message.chat.id == chat[0] or message.chat.id in GOOD_ID: AttributeError: 'str' object has no attribute 'chat'_ – Tanya Gaychenkova Mar 15 '22 at 05:34
  • But then I create job function that takes nothing as a parameter and create a loop with the users who I wanted to receive the alerts and it do help me. ```async def job(): for chat_id ... await bot.send_message(chat_id,)``` – Tanya Gaychenkova Mar 15 '22 at 05:38

0 Answers0