Last active
October 3, 2017 18:49
-
-
Save web20opensource/87160e9e239d54da81f7fb3feab3fbe3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RtmClient = require('@slack/client').RtmClient; | |
const MemoryDataStore = require('@slack/client').MemoryDataStore; | |
const RTM_EVENTS = require('@slack/client').RTM_EVENTS; | |
const token = process.env.SLACK_TOKEN || ''; | |
const rtm = new RtmClient(token, { | |
logLevel: 'error', | |
// logLevel: 'debug', | |
// Initialise a data store for our client, this will load additional helper functions for the storing and retrieval of data | |
dataStore: new MemoryDataStore(), | |
// Boolean indicating whether Slack should automatically reconnect after an error response | |
autoReconnect: true, | |
// Boolean indicating whether each message should be marked as read or not after it is processed | |
autoMark: true, | |
}); | |
let state = null; | |
let user = { | |
name: "", | |
phone: '', | |
address: '' | |
}; | |
const setState = (newState) => { | |
state = newState; | |
} | |
const handlers = {}; | |
const router = (message) => { | |
if (!state) { | |
handlers.DEFAULT(message); | |
} else { | |
handlers[state](message) | |
} | |
} | |
handlers.DEFAULT = (message) => { | |
rtm.sendMessage("Welcome! What's your name?", message.channel); | |
setState("GET_NAME"); | |
} | |
handlers.GET_NAME = (message) => { | |
console.log('get name'); | |
let name = message.text; | |
if ( /^[a-zA-Z]+$/.test(message.text) ){ | |
rtm.sendMessage("Ok, what's your phone number?", message.channel); | |
user.name = message.text; | |
setState("PHONE_NUMBER"); | |
}else{ | |
rtm.sendMessage("Please provide a valid name", message.channel); | |
setState("GET_NAME"); | |
} | |
} | |
handlers.PHONE_NUMBER = (message) => { | |
console.log('get phone'); | |
if ( /^[0-9- ]+$/.test(message.text) ){ | |
rtm.sendMessage("Ok, what's your address?", message.channel); | |
user.phone = message.text; | |
setState("COLLECT_ADDRESS"); | |
}else{ | |
rtm.sendMessage("Please provide a valid phone number, a.k.a", message.channel); | |
setState("PHONE_NUMBER"); | |
} | |
} | |
handlers.COLLECT_ADDRESS = (message) => { | |
console.log('get address'); | |
if ( /^[a-zA-Z0-9 .#-]+$/.test(message.text) ){ | |
user.address = message.text; | |
rtm.sendMessage("is this correct: " + JSON.stringify (user), message.channel); | |
setState("CONFIRM"); | |
}else{ | |
rtm.sendMessage("Ok,. let's to start again", message.channel); | |
setState("COLLECT_ADDRESS"); | |
} | |
} | |
handlers.CONFIRM = (message) =>{ | |
console.log('confirm? ' + message.text.toLowerCase() ); | |
if (message.text.toLowerCase() == 'yes'){ | |
rtm.sendMessage("Thanks. See ya!", message.channel); | |
}else{ | |
rtm.sendMessage("Let's try again...", message.channel); | |
} | |
setState("DEFAULT"); | |
} | |
/* | |
1. add a new handler called GET_NAME | |
2. have it collect and store the name of the user | |
3. have it set the state to phone number | |
4. collect and store the phone number | |
5. do the same for collecting an address | |
6. confirm with the user the input that you've stored | |
7a. if yes, say thanks, and loop back to the beginning, | |
7b. otherwise loop back with error | |
*/ | |
// Listens to all `message` events from the team | |
rtm.on(RTM_EVENTS.MESSAGE, (message) => { | |
if (message.channel === "G79CLHEA3") { | |
console.log('Bot sees a message in general and chooses not to respond.') | |
} else { | |
router(message); | |
} | |
}); | |
rtm.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment