Skip to content

Instantly share code, notes, and snippets.

View JonathanZWhite's full-sized avatar
🦁

Jonathan White JonathanZWhite

🦁
View GitHub Profile
var fallbackMessages = [
'Sorry, not sure what you just said.',
'Uh oh! Try again.',
'Hm... Sorry I\'m not that smart yet. Try again!'
];
var message = messages[Math.floor(Math.random() * messages.length)];
bot.say(message);
let store = {
...
getState: function(hash) {
if (!this._users[hash]) return this._initializeUser(hash)
return this._users[hash]
},
clearState: function(hash) {
let store = {
...
update: function(hash, data) {
if (!this._users[hash]) this._initializeUserState(hash)
console.log('🌑 => PREVIOUS STATE')
console.log(this.getState(hash))
this._users[hash] = _.merge(this._users[hash], data)
class Messenger {
...
handleText(msg) {
const message = new Message(Message.mapMessage(msg))
const text = message.text
if (inputParser.isAskingForGenreList(text))
return handlers.music.getGenreList(message, this.bot)
let store = {
_users: {},
// creates state tree
_initializeUserState: function(hash) {
this._users[hash] = {
command: '',
spotify: {
genre: '',
numberOfRecs: 0
(function(module) {
function getRecommendation(selectedGenre, limit) {
...
return Promise.resolve()
.then(getSpotifyAccessToken)
.then(getRecommendation)
function getRecommendation() {
class Music {
...
getRecommendation(message, bot) {
store.update(message.from, {
command: commands.GET_RECOMMENDATION,
spotify: {
numberOfRecs: message.text
}
})
class Music {
getGenreList(message, bot) {
// clears store for new command tree
store.clearState(message.from)
store.update(message.from, { command: commands.GET_GENRE_LIST })
bot.sendMessage(message.from, 'Choose a genre you would like a recommendation for 🎵', {
reply_markup: {
keyboard: genresJSON.genres,
one_time_keyboard: true
class InputParser {
isAskingForGenreList(text) {
const pattern = /music|recommendation/i
return text.match(pattern)
}
isAskingForNumberOfRec(text, prevCommand) {
return prevCommand === commands.GET_GENRE_LIST
}
'use strict'
const Messenger = require('./lib/messenger')
const messenger = new Messenger()
messenger.listen()
.then(() => {
console.log('🤖 Listening to incoming messages')
})