How to get the next telegram messages from specific users

Viewed 59

I'm implementing a telegram bot that will serve users. Initially, it used to get any new message sequentially, even in the middle of an ongoing session with another user. Because of that, anytime 2 or more users tried to use the bot, it used to get all jumbled up. To solve this I implemented a queue system that put users on hold until the ongoing conversation was finished. But this queue system is turning out to be a big hassle. I think my problems would be solved with just a method to get the new messages from a specific chat_id or user. This is the code that I'm using to get any new messages:

    def get_next_message_result(self, update_id: int, chat_id: str):
        """
        get the next message the of a given chat.
        In case of the next message being from another user, put it on the queue, and wait again for
        expected one.
        """
        update_id += 1
        link_requisicao = f'{self.url_base}getUpdates?timeout={message_timeout}&offset={update_id}'
        result = json.loads(requests.get(link_requisicao).content)["result"]
        if len(result) == 0:
            return result, update_id # timeout

        if "text" not in result[0]["message"]:
            self.responder(speeches.no_text_speech, message_chat_id)
            return [], update_id # message without text

        message_chat_id = result[0]["message"]["chat"]["id"]

        while message_chat_id != chat_id:
            self.responder(speeches.wait_speech, message_chat_id)
            if message_chat_id not in self.current_user_queue:
                self.current_user_queue.append(message_chat_id)
                print("Queuing user with the following chat_id:", message_chat_id)


            update_id += 1
            link_requisicao = f'{self.url_base}getUpdates?timeout={message_timeout}&offset={update_id}'
            result = json.loads(requests.get(link_requisicao).content)["result"]
            if len(result) == 0:
                return result, update_id # timeout
            
            if "text" not in result[0]["message"]:
                self.responder(speeches.no_text_speech, message_chat_id)
                return [], update_id # message without text

            message_chat_id = result[0]["message"]["chat"]["id"]
            
        return result, update_id

On another note: I use the queue so that the moment the current conversation ends, it calls the next user in line. Should I just drop the queue feature and tell the concurrent users to wait a few minutes? While ignoring any messages not from the current chat_id?

0 Answers
Related