DEV Community

Ujjwal Dubey
Ujjwal Dubey

Posted on

Sticky Agent Assignment for Shared WhatsApp Inboxes: A Small Python Router for Property Leads

When a property agency moves to one shared WhatsApp business number, the first engineering question is assignment. Buyers want the same agent every time. New buyers should be spread fairly. Agents on leave should not get new chats.

This router is a small piece of AI automation for real estate agencies running WhatsApp lead follow-up, and it is worth getting right before any AI is added.

Rules

  1. A returning buyer goes to the agent they spoke to last, if that agent is available.
  2. A new buyer goes to the next available agent in rotation for that area.
  3. If nobody for that area is available, the chat goes to a duty manager queue.

Code

import json, itertools
from pathlib import Path

STATE = Path("assignments.json")

AGENTS = {
    "north": ["asha", "ben"],
    "south": ["chen", "dina"],
}
AWAY = {"ben"}            # read from a calendar in real life
DUTY_QUEUE = "duty_manager"

def load():
    return json.loads(STATE.read_text()) if STATE.exists() else {"owner": {}, "cursor": {}}

def save(s):
    STATE.write_text(json.dumps(s, indent=1))

def assign(phone, area, state):
    owner = state["owner"].get(phone)
    if owner and owner not in AWAY:
        return owner, "returning"
    pool = [a for a in AGENTS.get(area, []) if a not in AWAY]
    if not pool:
        return DUTY_QUEUE, "no agent available"
    start = state["cursor"].get(area, 0)
    agent = pool[start % len(pool)]
    state["cursor"][area] = start + 1
    state["owner"][phone] = agent
    return agent, "new"

if __name__ == "__main__":
    s = load()
    for phone, area in [("+15550001", "north"), ("+15550002", "north"), ("+15550001", "north"), ("+15550003", "west")]:
        print(phone, area, *assign(phone, area, s))
    save(s)
Enter fullscreen mode Exit fullscreen mode

Notes

  • Phone numbers should be normalised first (E.164), or the same buyer looks new.
  • "Away" should come from a real calendar, not a hard-coded set.
  • Owner changes need a person. If a buyer asks for a different agent, a manager reassigns; the router should never decide that.
  • Log every assignment with a reason, so you can answer "why did this buyer get Chen?"

Where AI fits

Only after routing works. Then AI can draft the first reply or summarise the chat for the assigned agent. Drafts wait for the agent's approval on anything about price.

I work at NxFlowAI; we build routers like this after a 72-hour audit. If you are deciding between a packaged bot and custom work for the front of the chat, see our comparison of ready-made chatbots and custom builds.

Top comments (0)