🤖

Q&A + Keyword Handler

TagsAutomationBot
DescriptionKeyword "bot" type functionality for channel
FeaturesChartJavaScriptState Machine
TypeChannel

This app is a combination of a state machine as well as a keyword bot. On first message, the bot will ask three questions (name, company, email) and then offer a menu of selections. A graph of the count of menu choices is displayed.

var m = message.toLowerCase().trim();


if (m == "!reset") {
   // can be useful for debugging complex use cases
   chatbox.sendMessage("reset complete");
   return;
}

// if agent is present they are in a conversation
if (store.conversation.teamMembers && store.conversation.teamMembers.length > 0 && activeAgentPresent() == true) {
   return; 
}


// ignore opt outs
var optOuts = [ "STOP", "STOPALL", "UNSUBSCRIBE", "CANCEL", "END", "QUIT" ]; // copied from server cod
if (optOuts.indexOf(m.toUpperCase().trim()) >= 0) {
	return;
}


if (store.org.paused == "true") {
   console.log("Keyword Handler Paused -- Sending to Inbox");
   chatbox.sendToInbox();
   return;
}


var state = store.customer.conversationState;

if (state == undefined) {
   state = "initial";
}


var menuMsg = "1. Tell me the date\n2. Tag me as a VIP\n3. Show me my tags\n4. Summon a human";

switch(state) {   
      
   case "initial":
      chatbox.sendMessage("What's your name?");
      store.save("customer", "conversationState", "getName");
      break;
      
   case "getName":
      store.save("customer", "name", message);
      customer.setDisplayName(message);
      store.save("customer", "conversationState", "getCompany");
      chatbox.sendMessage("What company are you with?");
      break;
      
   case "getCompany":
      store.save("customer", "organization", message);
      store.save("customer", "conversationState", "getEmail");
      chatbox.sendMessage("What is your email address?");   
      break;
      
   case "getEmail":
      if (emailIsValid(message) == false && m != "no") {
         chatbox.sendMessage("Sorry, that doesn't look like a valid email address. Please enter an email address to continue or NO if you prefer to not provide one.");
         return;
      }

      store.save("customer", "conversationState", "demo");
      chatbox.sendMessage("Thank you.\n\n" + menuMsg);
      break;
      
   case "demo":
      doDemo();
      break;
      
   default:
      chatbox.sendMessage(menuMsg);
}

function emailIsValid (email) {
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
}


function doDemo() {
   switch(m) {
      case "1": 
         chatbox.sendMessage("GMT " + new Date());
         incrementStat("1");
         break;
         
      case "2": 
         chatbox.sendMessage("You are now a VIP!");
         customer.addTag("VIP", "#00ff00");
         incrementStat("2");
         break;
         
      case "3":
         chatbox.sendMessage("Your tags: " + customer.tags.toString());
         incrementStat("3");
         break;
 
      case "4":
         // places this item into all inboxes for team members with permissions for this channel
         incrementStat("4");
         chatbox.api.conversations.updateMembership(
	         { conversationId: store.conversation.id },
	         { list: [{ dest:"INBOX", targetGroup:"*" }], includeSystemMessage: false }
	      );         
         chatbox.sendMessage("We'll summon a member of the team to text you. Thank you.");
         break; 

      default:
         chatbox.sendMessage(menuMsg);

   }
}

function incrementStat(id) {   
   var stats = json(store.schema.stats, 
      {data: [
         {x: "1", y: 0}, 
         {x: "2", y: 0},
         {x: "3", y: 0},
         {x: "4", y: 0}
      ]});

   for(var i = 0; i < stats.data.length; i++) {
      var s = stats.data[i];
      if (s.x == id) {
         s.y = s.y + 1;
         store.save("schema", "stats", JSON.stringify(stats));
         app.setElementProperties("stats", stats);
         return;
      }
   }
   console.warn("Stat with id " + id + " no found");
}

// this is a helper function that could be used to see if the agent is actually active
// not used in this example but included for convenience
function activeAgentPresent() {
   var iterator = chatbox.agentContext.values().iterator();

   // loop throught he agentContext for our the peer-to-peer agent and see if they are online
   while (iterator.hasNext()) {
		var agentContext = iterator.next();
      if (agentContext.isActive) {
         return true;
      }
	}
	return false;
}