Skip to content

Instantly share code, notes, and snippets.

@richwednesday
Last active April 29, 2020 08:30
Show Gist options
  • Save richwednesday/e129ef181c0d43e5107ae888318e33d3 to your computer and use it in GitHub Desktop.
Save richwednesday/e129ef181c0d43e5107ae888318e33d3 to your computer and use it in GitHub Desktop.
sample code for a simple chat bot
function receiveMessage(id, text)
{
let state = redis.get(id);
if (state === "expecting default selection") processDefaultSelection(id, text)
else if (state == "expecting location") processLocationResponse(id, text)
else if (state === "expecting quantity") processQuantityResponse(id, text)
else sendDefaultMessage(id); // first message from user or user is in no state
}
function sendDefaultMessage(id)
{
twitter.sendTextMessage(id, "Welcome to our chatbot. Make a selection.\n1. Make Order\n2. Cancel Order")
redis.set(id, "expecting default selection")
}
function processDefaultSelection(id, text)
{
if (text === 1) startOrder(id, text)
else if (text === 2) cancelOrder(id, text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment