1

I have developed a Teams bot that runs an infinite loop at startup in order to send proactive messages to users.

async def job():
    i = 60
    await asyncio.sleep(i)
    await _create_file_of_the_day()
    await _send_question_of_the_day()
    await job()

if __name__ == "__main__":
    try:
        loop = asyncio.get_event_loop()
        t1 = loop.create_task(job())
        t2 = loop.create_task((web.run_app(APP, host="localhost", port=CONFIG.PORT)))
        asyncio.gather(t1,t2)
        loop.run_forever()
    except Exception as error:
        raise error

This work on local with python app.py but when I upload the bot to azure and test it online, the infinite loop is not started and so I find it impossible to proactively send messages to users. The two methods called work. The first creates a file on azure and the second creates two questions using the contents of the file, which should be sent proactively to all members of the channel. Does anyone know how to help me? I need to send messages to users based on a time delay not in response to their actions. This scheduling is not constant, for example I want to send messages only on working days and not on holidays.

Thanks to all

UPDATE

I have tried this second solution just on the comments, but the result is always the same. Locally the application behaves correctly, but on Azure cloud the routine that should loop seems not to be triggered.

async def job():
    i = 60
    await asyncio.sleep(i)
    await _create_file_of_the_day()
    await _send_question_of_the_day()
    await job()

async def main():

    runner = aiohttp.web.AppRunner(APP)
    await runner.setup()
    site = web.TCPSite(runner, host='localhost', port=CONFIG.PORT)
    await site.start()
    asyncio.create_task(job())
    while True:
        await asyncio.sleep(3600)


if __name__ == "__main__":
    try:
        asyncio.run(main())

    except Exception as error:
        raise error
Scaramel
  • 23
  • 4
  • Please update your post to fix the indentation of your code; it got messed up, looks like. – Random Davis Feb 19 '21 at 22:08
  • Yes, sorry. I fixed it. – Scaramel Feb 19 '21 at 22:42
  • `run_app` is a blocking (non-async) call, not something you can pass to `create_task`. See [this answer](https://stackoverflow.com/a/53465910/1600898) for ways to hook it into an existing loop. – user4815162342 Feb 20 '21 at 10:37
  • I looked at the question, but I think I don't understand. The argument is that this call initiated locally works and behaves as I want it to, but taken to the server it does not. However, now adding the steps in the linked answer locally no longer works. I'm probably doing something wrong. – Scaramel Feb 20 '21 at 12:09
  • @Scaramel - Welcome to Stack Overflow. This website is named after a common error type in several programming languages, caused by the sort of infinite recursion you have here. Why are you using infinite recursion instead of a normal loop? https://stackoverflow.com/questions/24899393/how-does-python-handle-infinite-recursion (Since there are multiple other people in this thread, you will need to @ mention me if you want me to see your reply.) – Kyle Delaney Feb 22 '21 at 21:45
  • @KyleDelaney - Hello. I needed to use an infinite loop to be able to schedule an action within my bot. Having tried for several days to follow this way without any success today I looked a little bit at what is offered by the Azure landscape and I found in azure function a possibility to do what I want avoiding to create the loop. – Scaramel Feb 22 '21 at 21:55
  • @Scaramel You misunderstand me. I wasn't asking why you need a loop at all. I was asking why you're using recursion to simulate an infinite loop instead of just using `while True` like you've done in another part of your code. Do you understand what recursion is, and that you're using it, and that the maximum recursion depth will be exceeded? You can see a discussion of the two ways to do an infinite loop here: https://stackoverflow.com/questions/50474189/infinite-loop-or-recursive-in-asyncio Also, have you resolved your problem with Azure functions already? Can you link to what you've found? – Kyle Delaney Feb 22 '21 at 23:22
  • @KyleDelaney Yes, I did not understand you. Probably the solution with the while loop is better, but it did not solve my problem. I will try to briefly explain the workflow of the program. I need to have a server with the bot active and every X seconds a question is asked to all the teams channel connected to the bot. This is done through the job() method. It seems that the job method is never triggered. I then thought of opening an endpoint and using a time-scheduled azure function to contact the endpoint and call the job() method.The problem is not yet solved, but I try in this way.Any tips? – Scaramel Feb 23 '21 at 08:19
  • @KyleDelaney Today, I took a closer look at the azure functions and simply with a get on an endpoint created in the bot app I was able to solve the problem. I left the job without the loop and the azure function of type time trigger calls the job method and every time the function is executed. – Scaramel Feb 23 '21 at 17:01
  • @Scaramel - Would you like to post that as an answer, or would you like someone else to post an answer for you to accept? – Kyle Delaney Feb 23 '21 at 23:25
  • @KyleDelaney I have posted the answer. :) – Scaramel Feb 24 '21 at 10:07

1 Answers1

0

Not being able to use the loop to be able to schedule messages, the problem was solved by using an Azure Function type timer trigger. This function calls an endpoint created inside the bot that each time it is called executes the job() method. These two links may be useful to understand how to create an endpoint and how to query it. In the code samples, the query is given by a click on the link which can easily be replaced by a GET request within the code.

Proactive message sample with endpoint creation

Article with explanation of proactive messages

The sample code is in python, but by browsing the git folders you can find code in other programming languages.

For the development of Azure Function I found this series of three videos on YouTube very useful.

Useful video on development of timer trigger function in python

Scaramel
  • 23
  • 4