0

I use this validation to send and receive messages between the client HTML chat and the rasa chatbot. I am using threads to wait for the socket to receive a message, but this is blocking the code from running, when for example 2 chats are coherently requesting to talk to an agent through the socket. Can you provide any better suggestion on how can I handle this, because I am not finding any good solution, based on the documentations of SocketIO and Rasa

class ValidateHandoffForm(FormValidationAction):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def name(self) -> Text:
        return "validate_handoff_form"

    def validate_set_message_bot(
            self,
            slot_value: Any,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: DomainDict,
    ) -> Dict[Text, Any]:
        self.random_id = tracker.sender_id
        if self.random_id not in initial_payload_sent:
            usertxt, bottxt = extract_user_bot_texts(tracker.events)
            print(usertxt)
            conversation = ""
            for index, txt in enumerate(usertxt):
                try:
                    if bottxt[index]:
                        conversation = "User: " + txt + "\nBot:" + bottxt[index]
                    else:
                        conversation = "User: " + txt + "\nBot: \n"
                except IndexError:
                    conversation += conversation + " \nUser:" + txt
            print('Inital message:' + self.random_id)
            send_to_socket_io('chatmessage', {"message": conversation, "sender": "admin-bot", "roomId":self.random_id}, dispatcher, self.random_id)
            initial_payload_sent[self.random_id] = True
        user_input = tracker.latest_message.get("text")
        print('Tracker message:' + self.random_id)
        sio.emit('chatmessage', {"message": user_input, "sender": "admin-bot", "roomId":self.random_id})
        def handle_message(message):
            if message['sender'] == 'admin' and message['roomId'] == tracker.sender_id:
                dispatcher.utter_message(text=message['message'])
                event_bot.set()
                return {"set_message_bot": message['message']}

        sio.on('message', handler=handle_message)
        event_bot.wait()
        return {"set_message_bot": 'Message'}

    def validate_set_message_client(
            self,
            slot_value: Any,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: DomainDict,
    ) -> Dict[Text, Any]:
        event_client.clear()
        user_input = tracker.latest_message.get("text")
        print(user_input)
        self.random_id = tracker.sender_id
        sio.emit('chatmessage', {"message": user_input, "sender": "admin-bot", "roomId":self.random_id})
        def handle_message(message):
            if message['sender'] == 'admin' and message['roomId'] == tracker.sender_id:
                dispatcher.utter_message(text=message['message'])
                if '/stop' in message['message']:
                #    SlotSet("set_message_client","Stop")
                #    SlotSet("set_message_bot", "Stop"),
                #    dispatcher.utter_message(template="utter_submit_handoff")
                #    FollowupAction(name="action_listen")
                event_client.set()
                return {"set_message_client": None}
        sio.on('message', handler=handle_message)
        event_client.wait()
        print("Here working")
        return {"set_message_client": None}

I was trying to communicate with the client chat html, which is like a real agent for human handoff, through 2 different chats and since the code is not running synchronously and the threading is not implemented correctly, this is blocking the replies.

0 Answers0