diff --git a/plugin/adapter.py b/plugin/adapter.py index b344b17..2fd5087 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -1070,47 +1070,6 @@ class RocketChatAdapter(BasePlatformAdapter): logger.error("[rocketchat] No real room for %s, cannot send", chat_id) return SendResult(success=False, error=f"No real room for virtual chat_id {chat_id}") - def _find_fallback_room_for_user(self, chat_id: str) -> Optional[str]: - """Fallback method to find a room for a user when direct mapping fails. - - Args: - chat_id: Virtual chat_id like "user:username" - - Returns: - Room ID if found, None otherwise - """ - if not chat_id.startswith("user:"): - return None - - target_username = chat_id.split(":", 1)[1].lower() - - # Try to find any DM room involving this user - for room_id, room_info in self._room_info.items(): - if room_info.get("type") == "d": - # Try to get room details with members - try: - room_detail = self.rest._request("GET", f"/api/v1/rooms.info?roomId={room_id}", headers={ - "X-Auth-Token": self.rest.auth_token, - "X-User-Id": self.rest.user_id, - }) - if room_detail and room_detail.get("success"): - room_data = room_detail.get("room", {}) - usernames = room_data.get("usernames", []) - if target_username in [u.lower() for u in usernames]: - return room_id - except Exception as e: - logger.debug("[rocketchat] Could not get room info for %s: %s", room_id, e) - - # Last resort: return first available room (better than nothing) - if self._subscribed_rooms: - first_room = next(iter(self._subscribed_rooms)) - logger.warning("[rocketchat] Using first available room %s as ultimate fallback for %s", - first_room, chat_id) - return first_room - - return None - - # ── Minimal-Fix: Parse [[BUTTONS:...]] markup from agent response text ── # This allows the agent to request buttons by embedding markup in its # response text, without requiring the gateway to pass metadata. @@ -1209,6 +1168,47 @@ class RocketChatAdapter(BasePlatformAdapter): return SendResult(success=True, message_id=str(int(time.time() * 1000))) return SendResult(success=False, error="chat.postMessage failed") + def _find_fallback_room_for_user(self, chat_id: str) -> Optional[str]: + """Fallback method to find a room for a user when direct mapping fails. + + Args: + chat_id: Virtual chat_id like "user:username" + + Returns: + Room ID if found, None otherwise + """ + if not chat_id.startswith("user:"): + return None + + target_username = chat_id.split(":", 1)[1].lower() + + # Try to find any DM room involving this user + for room_id, room_info in self._room_info.items(): + if room_info.get("type") == "d": + # Try to get room details with members + try: + room_detail = self.rest._request("GET", f"/api/v1/rooms.info?roomId={room_id}", headers={ + "X-Auth-Token": self.rest.auth_token, + "X-User-Id": self.rest.user_id, + }) + if room_detail and room_detail.get("success"): + room_data = room_detail.get("room", {}) + usernames = room_data.get("usernames", []) + if target_username in [u.lower() for u in usernames]: + return room_id + except Exception as e: + logger.debug("[rocketchat] Could not get room info for %s: %s", room_id, e) + + # Last resort: return first available room (better than nothing) + if self._subscribed_rooms: + first_room = next(iter(self._subscribed_rooms)) + logger.warning("[rocketchat] Using first available room %s as ultimate fallback for %s", + first_room, chat_id) + return first_room + + return None + + async def send_typing(self, chat_id: str, metadata=None) -> None: pass