const BootBot = require('bootbot'); | |
const ngrok = require('ngrok'); | |
const request = require('request'); | |
const low = require('lowdb'); | |
const FileSync = require('lowdb/adapters/FileSync'); | |
const adapter = new FileSync('user_db.json'); | |
const user_db = low(adapter); | |
user_db.defaults({users: {}, suggestions: []}).write(); | |
accessToken = '<add your own>' | |
const bot = new BootBot({ | |
accessToken: accessToken, | |
verifyToken: 'verifyme', | |
appSecret: '9407dc59dcba5173fb9a315d275a8026' | |
}); | |
var ngrok_url = "http://fdce1ea9.ngrok.io" | |
var typing = { typing: true } | |
function get_user_info(user_id) { | |
return new Promise((resolve, reject) => { | |
request("https://graph.facebook.com/v2.6/"+user_id+"?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token="+accessToken, | |
(err, res, body) => { | |
if(err) { | |
reject(err) | |
} else { | |
body = JSON.parse(body) | |
body['wp_state'] = "welcome" | |
resolve(body); | |
} | |
}); | |
}); | |
} | |
function getGIF(topic) { | |
return new Promise((resolve, reject) => { | |
var giphy_url = "https://api.giphy.com/v1/gifs/search?api_key=aVVttBYnUSEHPXpebRlntiETRGXXrzx6&limit=25&offset=0&rating=G&lang=en&q=" | |
request(giphy_url+topic, (err, res, body) => { | |
if(err) { | |
reject(err) | |
} else { | |
try { | |
body = JSON.parse(body).data | |
if(body.length == 0) { | |
reject(err) | |
} else { | |
resolve(body[Math.floor(Math.random() * body.length)]['images']['original']['url']) | |
} | |
} catch(e) { | |
reject(e) | |
} | |
} | |
}) | |
}) | |
} | |
function sendGIF(user_id, reason) { | |
return new Promise((resolve, reject) => { | |
getGIF(reason).then((GIF_url) => { | |
console.log("Got gif url - ",GIF_url); | |
bot.say(user_id, { | |
attachment: 'image', | |
url: GIF_url | |
}).then(() => resolve()) | |
}).catch((err) => {console.log("GIF getting error - ",err); resolve()}) | |
}) | |
} | |
function newUser(payload, chat) { | |
console.log("User doesn't exist. Adding..."); | |
get_user_info(payload.sender.id).then(user_info => { | |
user_db.set('users.'+payload.sender.id, user_info).write() | |
console.log("Welcoming user..."); | |
chat.conversation((convo) => { | |
user = user_db.get('users').get(payload.sender.id).value() | |
welcomeUser(convo,user).then(() => { | |
convo.say({ | |
text: 'So, Would you like to select a partner or wait for them to add you?', | |
quickReplies: ['Add Partner', 'Wait'] | |
}).then(() => convo.end()) | |
}); | |
}) | |
}).catch(err => { | |
console.log("Error writing user info - ",err); | |
}) | |
} | |
function SPEAKZ(text) { | |
return new Promise((resolve, reject) => { | |
request("http://speaklolcat.com/?from="+(text.split(" ").join("+")), (err, res, body) => { | |
if(err) { | |
reject(err) | |
} | |
else { | |
catspeak = body.match(/\<input type=\"hidden\" name=\"to\" value=\"([\d\D]+?)\" \/>/i) | |
if(catspeak) | |
resolve(catspeak[1]) | |
else | |
reject("Cat regex failed to parse") | |
} | |
}) | |
}) | |
} | |
function getPointLimit(user, convo) { | |
return new Promise((resolve, reject) => { | |
convo.ask("Where do you want to set the point limit for each level? (eg. 50)", (payload, convo) => { | |
if(isNaN(parseFloat(payload.message.text))) { | |
convo.say("Couldn't parse that.").then(() => getPointLimit(user, convo)) | |
} | |
else { | |
user.set('pointLimit',parseFloat(payload.message.text)).write() | |
convo.say("Thanks! Point limit set to "+parseFloat(payload.message.text)+".").then(() => convo.end()) | |
} | |
}) | |
}) | |
} | |
function addPoints(points, reason, from_id) { | |
return new Promise((resolve, reject) => { | |
var sender = undefined, recipient = undefined; | |
try { | |
sender = user_db.get('users').get(from_id); | |
recipient = user_db.get('users').get(sender.get('partner_id').value()); | |
} catch(e) { | |
reject("User or participant doesn't exist"); | |
return; | |
} | |
console.log("Need to send (",points,") for (",reason,") from ",sender.get('first_name').value(), " to ",recipient.get('first_name').value()) | |
recipient.get('points').push({ | |
time: (new Date).getTime(), | |
points: points, | |
reason: reason | |
}).write(); | |
recipient.update('pointCount', count => count+points).write(); | |
if((recipient.has('silent').value() && !recipient.get('silent').value()) || !recipient.has('silent').value()) { | |
if(recipient.has('gifs').value() && recipient.get('gifs').value()) { | |
console.log("sending GIFs...") | |
sendGIF(recipient.get('id').value(), reason).then( | |
() => bot.say(recipient.get('id').value(), sender.get('nickname').value()+" sent you points!").then( | |
() => levelNotification(recipient, points))) | |
} | |
else { | |
console.log("Not sending GIFs..."); | |
bot.say(recipient.get('id').value(), sender.get('nickname').value()+" sent you points!").then( | |
() => levelNotification(recipient, points)); | |
} | |
} | |
if(recipient.get('points').size().value() <= 1) { | |
bot.conversation(sender.get('id').value(), convo => { | |
convo.say('This looks like your first time giving points. Please set a point limit for leveling up.', typing).then(() => { | |
convo.say("For example, if you set a point limit of 10, level 1 will be at 10 points, level 2 will be at 20, etc.", typing).then(() => { | |
convo.ask({ | |
text: "You can only do this once, so set something not too high but reasonable. Would you like to know more about the levels?", | |
quickReplies: [{content_type: 'text', title: 'Yes', payload: 'YES'},{content_type: 'text', title: 'Nah', payload: 'NO'}] | |
}, (payload, convo) => { | |
if(payload.message.quick_reply && payload.message.quick_reply.payload =="YES") | |
levelsExplanation(convo).then(() => getPointLimit(recipient, convo)) | |
else | |
getPointLimit(recipient,convo) | |
}) | |
}) | |
}) | |
}) | |
bot.conversation(recipient.get('id').value(), convo => { | |
convo.ask({ | |
text: "Looks like this is your first time getting points. Would you like to know more?", | |
quickReplies: [ | |
{ | |
content_type: 'text', title:'Save it for later', payload: 'LATER' | |
},{ | |
content_type: 'text', title:'Tell me', payload: 'NOW' | |
} | |
]}, (payload, convo) => { | |
if(payload.message.quick_reply && payload.message.quick_reply.payload =="NOW") { | |
levelsExplanation(convo).then(() => convo.end()) | |
} | |
else { | |
convo.say("No problem! If you'd like to know at any point, type 'points?' in.").then(() => convo.end()) | |
} | |
}) | |
}) | |
} | |
resolve(); | |
}) | |
} | |
function levelNotification(user, pointsAdded) { | |
if(getLevel(user.get('id').value()) > getLevel(user.get('id').value(),user.get('pointCount').value()-pointsAdded)) | |
bot.say(user.get('id').value(), "Awesome! You've leveled up.", typing).then(() => { | |
bot.say(user.get('id').value(), "You're now at "+levelDescs[Math.min(levelDescs.length-1,getLevel(user.get('id').value()))], typing); | |
}) | |
} | |
function getLevel(user_id, points) { | |
if(!user_db.get('users').has(user_id).value()) | |
return 0; | |
user = user_db.get('users').get(user_id) | |
if(points == undefined) | |
points = user.get('pointCount').value() | |
pointLimit = 0 | |
if(!user.has('pointLimit').value()) | |
pointLimit = 1000 | |
else | |
pointLimit = user.get('pointLimit').value() | |
return Math.floor(points/pointLimit) | |
} | |
function levelsExplanation(chat) { | |
return chat.say("Ok here goes - points get you levels. These are what the levels are and what they let you do: ").then( | |
() => chat.say(levelDescs[0], typing)).then( | |
() => chat.say(levelDescs[1], typing)).then( | |
() => chat.say(levelDescs[2], typing)).then( | |
() => chat.say(levelDescs[3], typing)).then( | |
() => chat.say(levelDescs[4], typing)).then( | |
() => chat.say(levelDescs[5], typing)).then( | |
() => chat.say(levelDescs[6], typing)).then( | |
() => chat.say(levelDescs[7], typing)).then( | |
() => chat.say(levelDescs[8], typing)).then( | |
() => chat.say(levelDescs[9], typing)) | |
} | |
function getPartner(convo, user) { | |
return new Promise((resolve, reject) => { | |
convo.ask("What's your SO's nickname?", (payload, convo) => { | |
console.log("Got nickname - ",payload) | |
partner_nickname = payload.message.text | |
user_db.get('users').get(user['id']).assign({partner_nickname: partner_nickname}).write() | |
partners = [] | |
users = user_db.get('users').value() | |
for(var key in users) { | |
if(users[key]['nickname'] === partner_nickname) //TODO: Check for yourself | |
partners.push(users[key]) | |
} | |
convo.ask("What's their first name?", (payload, convo) => { | |
partner_firstname = payload.message.text | |
partners = partners.filter(partner => partner['first_name'] === partner_firstname) | |
user_db.get('users').get(user['id']).assign({partner_firstname: partner_firstname}).write() | |
console.log("Found ",partners.length," partners."); | |
if(partners.length == 0) { | |
convo.ask({ | |
text: "I can't seem to find your partner. You can either try again or save this nickname. I'll text you when they join!", | |
quickReplies: [ | |
{ | |
content_type: 'text', title:'Try Again', payload: 'TRY_AGAIN' | |
},{ | |
content_type: 'text', title:'Save', payload: 'SAVE' | |
} | |
] | |
}, (payload, conversation) => { | |
console.log("User said ",payload) | |
if(payload.message.quick_reply != undefined && payload.message.quick_reply.payload ==='SAVE') | |
convo.say('Saved! Have fun waiting...').then(resolve()) | |
else | |
getPartner(convo,user).then(() => resolve()).catch(reject) | |
}) | |
} else if(partners.length > 1) { | |
convo.ask({ | |
text: "I found too many people of that description. Guess we're not all snowflakes, huh? You can either enter their last name or restart the search.", | |
quickReplies: [ | |
{ content_type: 'text', title:'Try Again', payload: 'TRY_AGAIN'}, | |
{ content_type: 'text', title:'End', payload: 'END'} | |
] | |
}, (payload, convo) => { | |
if(payload.message.quick_reply && payload.message.quick_reply.payload =="TRY_AGAIN") { | |
getPartner(convo, user).then(() => resolve()).catch(reject) | |
} else if (payload.message.quick_reply && payload.message.quick_reply.payload =="END") { | |
convo.say("Okay, I guess life can have a little wait.", typing).then(() => resolve()); | |
} else { | |
partner_lastname = payload.message.text | |
partners = partners.filter(partner => partner['last_name'] === partner_lastname) | |
console.log("Found ",partners.length, " after last name was entered."); | |
if(partners.length == 0) { | |
convo.ask({ | |
text: "I can't seem to find your partner. You can either try again or save this nickname. I'll text you when they join!", | |
quickReplies: [ | |
{ | |
content_type: 'text', title:'Try Again', payload: 'TRY_AGAIN' | |
},{ | |
content_type: 'text', title:'Save', payload: 'SAVE' | |
} | |
] | |
}, (payload, conversation) => { | |
console.log("User said ",payload) | |
if(payload.message.quick_reply != undefined && payload.message.quick_reply.payload ==='SAVE') | |
convo.say('Saved! Have fun waiting...').then(resolve()) | |
else | |
getPartner(convo,user).then(() => resolve()).catch(reject) | |
}) | |
} else { | |
addPartner(convo, user, partners[0]).then(() => resolve()) | |
} | |
} | |
}) | |
} else { | |
addPartner(convo, user, partners[0]).then(() => resolve()) | |
} | |
}) | |
}) | |
}) | |
} | |
function addPartner(convo, user, partner) { | |
return new Promise((resolve, reject) => { | |
console.log("Adding new partner..") | |
partner = user_db.get('users').get(partner['id']) | |
me = user_db.get('users').get(user['id']) | |
if(partner.value().partner_status && partner.value().partner_status == "confirmed") { | |
convo.ask({ | |
text: "Unfortunately life is tough. They seem to already have a partner. You can ask them to add you, or redo the search.", | |
quickReplies: [ | |
{ | |
content_type: 'text', title:'Try Again', payload: 'TRY_AGAIN' | |
} | |
] | |
}, (payload, conversation) => { | |
console.log("User said ",payload) | |
if(payload.message.quick_reply != undefined && payload.message.quick_reply.payload ==='TRY_AGAIN') | |
convo.say('Saved! Have fun waiting...').then(() => {convo.end();resolve()}) | |
else | |
addPartner(convo,user,partner).then(() => resolve()).catch(reject) | |
}) | |
} else { | |
partner.assign({partner_status: 'unconfirmed', partner_nickname: me.value().nickname, partner_id: me.value().id}).write() | |
me.assign({partner_status: 'unconfirmed', partner_id: partner.value().id}).write() | |
console.log("Partner info written to database."); | |
//Ask the other user to confirm | |
bot.conversation(partner.value().id, convo => { | |
convo.ask({ | |
text: me.value().first_name+" ("+me.value().nickname+") would like to connect. Accept?", | |
quickReplies: [{ | |
content_type:"text", title: "Yes", payload: "YES" | |
}, { | |
content_type:"text", title: "No", payload: "NO" | |
}] | |
}, (payload, convo) => { | |
console.log("User replied ",payload.message.text) | |
if(payload.message.text === "yes" || payload.message.text === "Yes" || (payload.message.quick_reply != undefined && payload.message.quick_reply.payload == "YES")) { | |
me.assign({wp_state: 'partnered', partner_status: 'confirmed'}).write() | |
partner.assign({wp_state: 'partnered', partner_status: 'confirmed'}).write() | |
me.set('points', []).write(); | |
partner.set('points', []).write(); | |
me.set('pointCount', 0).write(); | |
partner.set('pointCount', 0).write(); | |
convo.say('All good! You now have a partner. Type help to see what you can do!', typing).then(() => {convo.end(); resolve()}).catch(reject); | |
bot.conversation(me.value().id, convo => { | |
convo.say(partner.value().nickname+" has accepted you as a partner. Have fun! Type help at any point to see what you can do!", typing).then(() => { | |
convo.end(); | |
}) | |
}) | |
} else { | |
bot.conversation(me.value().id, convo => { | |
convo.say(partner.value().nickname+" has denied your request to be a partner.", typing); //TODO: Add option to restart process | |
}) | |
convo.say("No problems, we'll let them know.", typing).then(() => { convo.end(); resolve()}).catch(reject) | |
} | |
}) | |
}) | |
convo.say("All good, we're just waiting to hear back from your "+me.value().partner_nickname, typing).then(() => { | |
convo.end(); resolve() | |
}) | |
} | |
}) | |
} | |
//********************************BOT FUNCTIONS****************************************** | |
bot.on('message', (payload, chat) => { | |
console.log("Got message "+payload.message.text) | |
if(user_db.get('users').has(payload.sender.id).value() && !user_db.get('users').get(payload.sender.id).has('gifs').value()) { | |
user_db.get('users').get(payload.sender.id).set('gifs',true).write() | |
chat.say("New feature - you can now get relevant gifs with each point and try to find out why you got them. They're on by default, to turn off type 'gifs off'!", typing); | |
} | |
if(user_db.get('users').has(payload.sender.id).value() && user_db.get('users').get(payload.sender.id).get('wp_state').value === 'partnered') { | |
// All partnered, let's look for stuff | |
} else if(payload.message.text != 'Add Partner') { | |
if(!user_db.get('users').has(payload.sender.id).value()) { | |
newUser(payload, chat) | |
} | |
else if(user_db.get('users').has(payload.sender.id).value() && user_db.get('users').get(payload.sender.id).get('wp_state').value === 'welcomed') { | |
chat.say({ | |
text: 'So, Would you like to select a partner or wait for them to add you?', | |
quickReplies: ['Add Partner'] | |
}) | |
} | |
} | |
}) | |
bot.hear(['help'], (payload, chat) => { | |
chat.say("Hello there! I'm the wifepoints bot. Here's what you can do with me:", typing).then(() => { | |
chat.say("First, at any point you can say 'X points for (reason)' to award points.", typing).then(() => { | |
chat.say("There's a leveling system that will activate once you do.", typing).then(() => { | |
chat.say("There's a few more things you can do, try saying 'talk', 'me', suggestion', 'gifs on', 'gifs off', silent on', 'silent off', 'reasons', 'levels', 'new pointlimit' or 'points' to find out more!", typing) | |
}) | |
}) | |
}) | |
}) | |
bot.hear(['talk','send message'], (payload, chat) => { | |
if(!user_db.get('users').has(payload.sender.id).value()) { | |
chat.say("I don't have you registered yet. Easy fix...").then(() => newUser(payload, chat)); | |
return; | |
} | |
user = user_db.get('users').get(payload.sender.id) | |
var intro = undefined | |
if(!user.has('last_chat_time').value()) { | |
user.set('last_chat_time', (new Date).getTime()).write() | |
intro = chat.say("Looks like this is your first time. You can now send messages to your partner! YAY!", typing).then( | |
() => chat.say("One catch though.", typing)).then( | |
() => chat.say("Actually two. First, you can only send one message every two hours. Why two? No reason. Maybe we'll add that to the levels.", typing)).then( | |
() => chat.say("Second, everything you say is translated to cat.", typing)) | |
} | |
if(intro == undefined && ((new Date).getTime() - user.get('last_chat_time').value()) < (1000*60*60*2)) { | |
chat.say("Speak too soon, and forever hold your peace. Just kidding, you've got "+((1000*60*60*2) - ((new Date).getTime() - user.get('last_chat_time').value()))+" milliseconds to go before you can talk again. Time to spend thinking about people...", typing) | |
} else { | |
if(!intro) | |
intro = Promise.resolve() | |
intro.then(() => { | |
chat.conversation((convo) => { | |
convo.ask("What would you like to say?", (payload, convo) => { | |
SPEAKZ(payload.message.text).then(catsponse => { | |
bot.say(user.get('partner_id').value(), catsponse); | |
convo.say("On it's way. Carry on!", typing).then(() => convo.end()) | |
user.set('last_chat_time', (new Date).getTime()).write() | |
}).catch(err => { | |
convo.say("Couldn't say that. Too much cat.", typing).then(() => convo.end()); | |
}) | |
}) | |
}) | |
}) | |
} | |
}) | |
bot.hear(['levels'], (payload, chat) => { | |
levelsExplanation(chat); | |
}) | |
var points_all_regex = /([\.\d]+?)[\s]?(pts|points|point)?([\s])? (cause|to|for|because) (.+)/i | |
bot.hear([points_all_regex], (payload, chat) => { | |
data = payload.message.text.match(points_all_regex) | |
addPoints(parseFloat(data[1]),data[5],payload.sender.id).then(() => { | |
points_responses = [ | |
"Points! Yay!", | |
"I love the smell of points in the current colloquial time of day you're on!", | |
"The points DO matter. Drew Carrey really didn't know shizz.", | |
"Thanks for not being an overzealous point guard!" | |
] | |
chat.say({ | |
text: points_responses[Math.floor(Math.random()*points_responses.length)], | |
quickReplies: ['Help', 'Talk', 'Me', 'Reason', 'Points', 'Suggestion', 'Levels'] | |
}, typing) | |
}).catch(err => { | |
chat.say("Couldn't add points. We're possibly down for service. Do something nice for them instead?"); | |
}) | |
}) | |
bot.hear(['new pointlimit'], (payload, chat) => { | |
if(user_db.get('users').has(payload.sender.id).value() && user_db.get('users').has(user_db.get('users').get(payload.sender.id).get('partner_id').value()).value()) { | |
user = user_db.get('users').get(payload.sender.id) | |
partner = user_db.get('users').get(user.get('partner_id').value()) | |
chat.conversation((convo) => { | |
convo.ask({ | |
text: "Your current point limit is "+partner.get('pointLimit').value()+". Please enter a new pointLimit: ", | |
quickReplies: [{content_type: 'text', title: 'Nevermind', payload:'LATER'}] | |
}, (payload, convo) => { | |
if(payload.message.quick_reply && payload.message.quick_reply.payload =="LATER") { | |
convo.say("No problem, toodles!").then(() => convo.end()) | |
} else { | |
pointLimit = parseFloat(payload.message.text); | |
if(isNaN(pointLimit)) { | |
convo.say("Need a number, sorry. Try again with 'new pointlimit'...").then(() => convo.end()) | |
} else { | |
partner.set('pointLimit', pointLimit).write(); | |
convo.say('New pointlimit set!').then(() => convo.end()) | |
} | |
} | |
}); | |
}) | |
} else { | |
chat.say("I don't have you registered yet. Easy fix...").then(() => newUser(payload, chat)); | |
} | |
}) | |
bot.hear(['me'], (payload, chat) => { | |
if(user_db.get('users').has(payload.sender.id).value()) { | |
user = user_db.get('users').get(payload.sender.id) | |
chat.say("Hey there! Your nickname is "+user.get('nickname').value(), typing).then(() => { | |
if(user_db.get('users').has(user.get('partner_id').value())) { | |
partner = user_db.get('users').get(user.get('partner_id').value()) | |
chat.say("You're partnered (for the moment) with "+partner.get('nickname').value(), typing).then(() => { | |
chat.say("You've given out a total of "+partner.get('pointCount')+" for a point limit of "+partner.get('pointLimit')+".", typing).then(() => { | |
chat.say("They're currently at level - "+levelDescs[Math.min(levelDescs.length-1,getLevel(partner.get('id').value()))], typing) | |
}); | |
}) | |
} | |
}) | |
} else { | |
chat.say("I don't have you registered yet. Easy fix...").then(() => newUser(payload, chat)); | |
} | |
}) | |
/* | |
Levels | |
0 - get notifications for points | |
1 - check point balance with 25% variability | |
2 - 1/10 of the time get the reason for points | |
3 - check point balance with 5% variability | |
4 - query the last reason for points with 1/2 chance of succeeding | |
5 - get accurate point balance | |
6 - get accurate reasons for last 5 point balance updates | |
7 - send messages as a response to points | |
8 - point balances become redeemable (work in progress) | |
9 - to be added (suggestions welcome) | |
*/ | |
var levelDescs = [ | |
"Level 0 (default) - Get notified of points", | |
"Level 1 - Type 'points' to check point balance with 50% margin of error", | |
"Level 2 - Type 'reasons' to get reason for points with 1/10 chance of success", | |
"Level 3 - Type 'points' to check point balance with 10% margin of error", | |
"Level 4 - Type 'reasons' check the last reason for points with 1/2 chance of succeeding", | |
"Level 5 - Type 'points' to get accurate point balance", | |
"Level 6 - Type 'reasons' to get accurate reasons for last five point updates", | |
"Level 7 - Reply to points given with messages", | |
"Level 8 - Point balances are redeemable and transferable (work in progress, hopefully you're not here too soon)", | |
"Level 9 and on - WIP!!" | |
] | |
bot.hear(["points"], (payload, chat) => { | |
level = getLevel(payload.sender.id) | |
if(level < 1) { | |
chat.say("Unfortunately, you haven't leveled up enough to see your balance. Keep earning those points!", typing) | |
} else { | |
points = user_db.get('users').get(payload.sender.id).get('pointCount').value() | |
if(level < 3) { | |
chat.say("You currently have "+(points*((100-((Math.random()*50)-25))/100))+" points (plus or minus 25%). Sorry for the decimals, you can get rid of it higher up the levels!", typing) | |
} else if(level <5) { | |
chat.say("You currently have "+Math.floor(points*((100-((Math.random()*10)-5))/100))+" points (plus or minus 5%). Keep leveling!", typing) | |
} else { | |
chat.say("Congrats on earning that accurate point balance! You have "+points+" points.", typing); | |
} | |
} | |
}) | |
bot.hear(['reason', 'reasons'], (payload, chat) => { | |
level = getLevel(payload.sender.id) | |
if(level < 2) { | |
chat.say("Sorry about that. You need to level up more before you can see reasons. Right now, yours is not to reason why I guess...", typing); | |
} else { | |
user = user_db.get('users').get(payload.sender.id) | |
if(user.get('points').size().value() == 0) { | |
chat.say("You don't have any points yet. Not to worry, work will set you free...", typing); | |
} | |
else if(!user.has('checkedReason').value() || user.get('checkedReason').value() != user.get('pointCount').value()) | |
{ | |
if(level < 4) { | |
if(Math.random() < 0.1) { | |
chat.say("Yay! Here's the last reason you got points - "+user.get('points').orderBy('time','desc').take(1).value()[0]['reason'], typing); | |
} else { | |
chat.say("Sorry! Not this time. Maybe next time?", typing) | |
} | |
} else if(level < 5) { | |
if(Math.random() < 0.5) { | |
chat.say("Yay! Here's the last reason you got points - "+user.get('points').orderBy('time','desc').take(1).value()[0]['reason'], typing); | |
} else { | |
chat.say("Sorry! Not this time. Maybe next time?", typing) | |
} | |
} else { | |
reasons = user.get('points').orderBy('time','desc').take(5).value().map((item) => item['reason']) | |
chat.say("Whoa there power user! Here are the last five reasons for your points - ").then(() => { | |
if(reasons.length > 0) | |
chat.say("First, you got points for "+reasons[0]).then(() => { | |
if(reasons.length > 1) | |
chat.say("Second, you got points for "+reasons[1]).then(() => { | |
if(reasons.length > 2) | |
chat.say("3 - "+reasons[2]).then(() => { | |
if(reasons.length > 3) | |
chat.say("4 - "+reasons[3]).then(() => { | |
if(reasons.length > 4) | |
chat.say("5 - "+reasons[4]).then(() => { | |
}) | |
}) | |
}) | |
}) | |
}) | |
}) | |
} | |
user.set("checkedReason", user.get('pointCount').value()).write(); | |
} else { | |
chat.say("Oooh sorry - you've already checked your reasons once. Wait till you get more points. What have you done today?", typing); | |
} | |
} | |
}) | |
bot.hear(['Suggestion'], (payload, chat) => { | |
chat.conversation(convo => { | |
convo.ask("Lay it on me. What's your suggestion?", (payload, chat) => { | |
user_db.get('suggestions').push({sender: payload.sender.id, content:payload.message.text}).write(); | |
convo.say("Saved. Thanks!!!").then(() => convo.end()) | |
}) | |
}) | |
}) | |
bot.hear([/points\?/i], (payload, chat) => { | |
chat.conversation(levelsExplanation) | |
}) | |
bot.hear(['Settings','Status'], (payload, chat) => { // Config messages go here | |
console.log("Settings or status menu needs to be built") | |
chat.say("42") | |
}) | |
bot.hear([/gifs (on|off)/i], (payload, chat) => { | |
if(user_db.get('users').has(payload.sender.id).value()) { | |
if(payload.message.text.match(/gifs on/i)) { | |
user_db.get('users').get(payload.sender.id).set('gifs', true).write() | |
chat.say('GIFs are turned on. Have fun!') | |
} else { | |
user_db.get('users').get(payload.sender.id).set('gifs', false).write() | |
chat.say('GIFs are turned off. Too distracting?') | |
} | |
} else { | |
chat.say("I don't have you registered yet. Easy fix...").then(() => newUser(payload, chat)); | |
} | |
}); | |
bot.hear([/silent (on|off)/i], (payload, chat) => { | |
if(user_db.get('users').has(payload.sender.id).value()) { | |
if(payload.message.text.match(/silent on/i)) { | |
user_db.get('users').get(payload.sender.id).set('silent', true).write() | |
chat.say("Silent mode on. You won't receive any more notifications.") | |
} else { | |
user_db.get('users').get(payload.sender.id).set('silent', false).write() | |
chat.say('Silent mode off. Yay to being connected!') | |
} | |
} else { | |
chat.say("I don't have you registered yet. Easy fix...").then(() => newUser(payload, chat)); | |
} | |
}) | |
bot.hear(['Add Partner'], (payload, chat) => { | |
chat.conversation((convo) => getPartner(convo, user)); | |
}); | |
function welcomeUser(convo, user) { | |
return new Promise((resolve, reject) => { | |
convo.say("Hey there "+user['first_name'], typing).then(() => { | |
convo.ask("Seems like this is your first time. What would you like me to call you (one or two words)?", (payload, convo) => { | |
convo.set('nickname',payload.message.text); | |
user_db.get('users').get(user['id']).assign({nickname: payload.message.text}).write() | |
convo.say('Nice to meet you, '+payload.message.text, typing).then(() => { | |
var user_type = "girlfriend" | |
if(user['gender'] === 'male') | |
user_type = 'boyfriend' | |
convo.say("You look like a "+user_type+".", typing).then(() => { | |
user_db.get('users').get(user['id']).assign({wp_state: 'welcomed'}).write(); | |
convo.end(); | |
resolve() | |
}).catch(reject) | |
}); | |
}) | |
}).catch(err => reject(err)); | |
}) | |
} | |
bot.start(9000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment