Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

111
LINES

< > BotCompany Repo | #1028517 // Handover [have web chat bot switch to sales rep, Include]

JavaX fragment (include)

concept HandoverConfig {
  int handoverTimeout = 20;
}

sclass Handover {
  sclass Form_DoYouWantAHuman > FormInFlight {
    private *() {}
    *(Conversation conversation) {
      steps.add(nu FormStep(
        key := "answer",
        displayText := getCannedAnswer("#DoYouWantAHuman", conversation),
        placeholder := "",
        buttons := ll("Yes", "No")
      ));
    }
    
    S complete() {
      if (isYes(getValue("answer")))
        ret conversation.setForm(new ConnectToWorkerForm).initialMessage();
      else
        ret getCannedAnswer("#dontConnectMe", conversation);
    }
    
    bool allowGeneralOverride() { true; }
  }
  
  sS handleHashtag(Conversation conv, S tag) null {
    if (eqic(tag, "#ConnectMeToAHuman"))
      ret tryToConnect(conv);

    if (eqic(tag, "#DoYouWantAHuman")) {
      if (conv.form instanceof ConnectToWorkerForm)
        ret getCannedAnswer("#stillConnecting", conv);

      conv.setForm(new Form_DoYouWantAHuman(conv));
      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((double) uniq(HandoverConfig).handoverTimeout, new TimeoutAction);
      workerChat.lastWorkerRequested = now(); // play special notification sound
      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();
    }
  }
  
  sO html(S uri, SS params, AuthedDialogID auth) null {
    S uri2 = appendSlash(uri);
    bool requestAuthed = auth != null;
  
    if (startsWith(uri2, "/handover-admin/")) {
      if (!requestAuthed) ret serveAuthForm(params.get('uri));
      ret serveHandoverAdmin(uri, params);
    }
  }
    
  sS serveHandoverAdmin(S uri, SS params) {
    uniq(HandoverConfig);
    
    S _nav = p(ahref(rawBotLink(dbBotID), "Main admin") + " | " + ahref(baseLink + "/handover-admin", "Handover admin"));
    
    HCRUD_Concepts<HandoverConfig> data = new HCRUD_Concepts<>(HandoverConfig);
    data.fieldHelp = litmap(
      handoverTimeout := "How long bot waits for an available worker (in seconds)",
    );

    HCRUD crud = new(rawLink("handover-admin"), data) {
      S frame(S title, S contents) {
        ret hhtml(hhead_title_htmldecode(title) + hbody(
          hsansserif() + hcss_responstable()
          + _nav + h1(title)
          + contents));
      }
    };
    crud.singleton = true;
    crud.cmdsLeft = true;
    crud.tableClass = "responstable";
    ret crud.renderPage(params);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028517
Snippet name: Handover [have web chat bot switch to sales rep, Include]
Eternal ID of this version: #1028517/37
Text MD5: 2936d80e8c35df876df8a501485a817b
Author: stefan
Category: javax / web chat bot
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-10-24 12:07:40
Source code size: 3501 bytes / 111 lines
Pitched / IR pitched: No / No
Views / Downloads: 265 / 524
Version history: 36 change(s)
Referenced in: [show references]