Forums

Async with telegramwebhook

hi everyone i have a telegram bot do some thing with webhook in flaskApp in web section in free account (that can not run threading) i need a function that work parallel with this bot to send somthing in a telegram channel

this line is running in main body

asyncio.run(telegram_webhook(1))

and telegram webhook function is this:

        app = Flask(__name__)
        @app.route('/{}'.format(secret), methods=["POST"])
async def telegram_webhook(asy=0):
            if asy!=0:
                bot.sendMessage(ADMIN_ID,"before create_task")
                task1 = asyncio.create_task(run_forever())
                await task1
                bot.sendMessage(ADMIN_ID,"after create_task")
                return
    "do some jobs here to communicate with tlegram user....."

and run_forever() function

async def run_forever():
    while 1:
        bot.sendMessage(ADMIN_ID ,"run_forever .................")
        await asyncio.sleep(2)

i can not control run_forever() function .. it sometimes running .. sometimes not running . when running i can not stop that (reload-clear codes-clear import asyncio and many ways i tried)

i need help to learn work with asyncio please guid me and if possible be my teacher

thanks lot

As Glenn mentioned in the other thread, asyncio won't work in the web app on PythonAnywhere at a moment as we do not support ASGI yet.

oooh .. i thought that is working ... ok thank you