Slack bot api requests and my server requests are not running at a time

I created a slack bot app using Django. In this app, the bot will ask some questions to the users within a given schedule(periodically) from the database. The bot will wait for the reply of the users.

This is how I am calling the slack API to ask the questions concurrently, at a time. I used async and await in the function.

async def post_async_message(channel, message):
   """ broadcast message in the given channel asynchronously """
   try:
       response = await Async_Client.chat_postMessage(
           channel=channel,
           text=message
       )

       if response['ok']:
           await asyncio.sleep(Wait_For_Answer)
   except SlackApiError as e:
       raise CustomError(f"Error occurred: {e}", e.response)

This is the function from where the post_async_message function has been called.

async def post_standup_message(standup_id):

    participants = await models.get_participants(standup_id)
    questions = await models.get_standup_questions(standup_id)

    async def ask_question(user_id):
        # send standup question to the user

        async for question in questions:
            try:
                await views.post_async_message(user_id, question.Question)
            except CustomError as e:
                print(e)


    tasks = [ask_question(participant.Slack_User_Id) async for participant in participants]
    for completed_task in asyncio.as_completed(tasks):
        await completed_task


asyncio.run(post_standup_message(49))

Everything is doing good. But one thing that I notice is that during the asking of the questions by the bot if I call any API at the same time, the server is unable to execute the API. But when the execution of the post_standup_message function is completed the API works perfectly. Which means the API and the bot are not running at a time. What is the reason behind that? Note that my bot and APIs are running on the same server. Also, I have some questions.

Am I doing it right to execute the bot and APIs on the same server? Is it good to have an external background scheduling tool to create a slack bot?

Back to Top