Issue
Update: After few uncessful attempts to explain the problem, completely rewrote the question:
How to execute a function on startup?
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler()
async def echo(message: types.Message):
await message.answer(message.text)
async def notify_message() # THIS FUNCTION
# await bot.sendMessage(chat.id, 'Bot Started')
await print('Hello World')
if __name__ == '__main__':
notifty_message() # doesn't work
executor.start_polling(dp, skip_updates=True)
Tried without success::
if __name__ == '__main__':
dp.loop.create_task(notify_message()) # Function to execute
executor.start_polling(dp, skip_updates=True)
AttributeError: 'NoneType' object has no attribute 'create_task'
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.create_task(notify_message()) # Function to execute
executor.start_polling(dp, skip_updates=True)
TypeError: object NoneType can't be used in 'await' expression
Solution
It was much simpler than expected. facepalm
Working Solution:
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler()
async def echo(message: types.Message):
await bot.send_message(message.chat.id, message.text)
def test_hi():
print("Hello World")
if __name__ == '__main__':
test_hi()
executor.start_polling(dp, skip_updates=True)
Answered By - Mike S.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.