sclass Handover { sclass Form_DoYouWantAHuman > FormInFlight { *() { steps.add(nu FormStep( key := "answer", displayText := "Would you like to be connected to a sales representative?", buttons := ll("Yes", "No") )); } S complete() { if (isYes(getValue("answer"))) ret conversation.setForm(new ConnectToWorkerForm).initialMessage(); else ret getCannedAnswer("#dontConnectMe", conversation); } } sS handleHashtag(Conversation conversation, S tag) null { if (eqic(tag, "#ConnectMeToAHuman")) ret tryToConnect(conversation); if (eqic(tag, "#DoYouWantAHuman")) { conversation.setForm(new Form_DoYouWantAHuman); ret ""; // Text comes from Q&A admin } } sS tryToConnect(Conversation conversation) { print("Handover tryToConnect"); if (workerChat.anyWorkersAvailable()) ret conversation.setForm(new ConnectToWorkerForm).initialMessage(); else ret getCannedAnswer("#away", conversation); } sclass ConnectToWorkerForm > FormInFlight { runnable class TimeoutAction { onTimeout(); } void update(Runnable change) { super.update(change); if (conversation.worker != null) onWorkerAccepts(); } S initialMessage() { addTimeout(20.0, new TimeoutAction); ret getCannedAnswer("#connecting", conversation); } void onWorkerAccepts() { if (conversation == null) ret; // cancelled in the meantime print("Handover accept"); conversation.add(new Msg(getCannedAnswer("#connected", conversation), false)); conversation.cancelForm(); } void onTimeout() { if (conversation == null) ret; // cancelled in the meantime print("Handover timeout"); conversation.add(new Msg(getCannedAnswer("#away", conversation), false)); conversation.cancelForm(); } } }