Skip to content

Instantly share code, notes, and snippets.

@lagbox
Last active March 29, 2016 03:37
Show Gist options
  • Save lagbox/32c6b234c044c4b29b8c to your computer and use it in GitHub Desktop.
Save lagbox/32c6b234c044c4b29b8c to your computer and use it in GitHub Desktop.
Greeter - laying out ideas
# Description:
# Language based greetings, and laravenewbie nick change welcome message
#
# Commands:
# hubot show greeting for {language}
# hubot I speak {language}
# hubot forget my greeting
# hubot what is my greeting
# hubot greeting
# hubot list languages
#
# hubot greet me
# hubot clear last greet
# hubot forget me
UserRepo = require '../support/user_repository'
greetings = require '../support/greetings'
CACHETIME = 100 * 60 * 15
# greeting helpers
randomGreeting = ->
keys = Object.keys(greetings)
findGreeting(keys[Math.floor(Math.random() * keys.length)])
findGreeting = (lang) ->
greetings[lang.toLowerCase()]
buildGreeting = (obj, user) ->
{greeting: greeting, lang: lang} = obj
"#{greeting} #{user}! (#{lang})"
greetingsNames = ->
Object.keys(greetings).map((a) -> greetings[a].lang)
cantFindIt = (lang) ->
"Sorry no such language, #{lang}"
# user helpers
userName = (msg) ->
msg.message.user.name
doUserGreet = (user, repo) ->
if user?.lang?
if user.last_greeted_at? && (Date.now() - user.last_greeted_at) < CACHETIME
return
repo.greeted user
return buildGreeting findGreeting(user.lang), user.name
# add some sh...tuff to the UserRepo
UserRepo::greeted = (user) ->
user.last_greeted_at = Date.now()
@save user.name, user
user
UserRepo::nameUser = (msg) ->
name = userName msg
[name, @find name]
# Da Exports
module.exports = (robot) ->
userRepo = new UserRepo(robot.brain)
robot.respond /greet me/i, (msg) ->
[name, user] = userRepo.nameUser msg
message = doUserGreet user, userRepo
msg.send message if message
robot.respond /clear last greet/i, (msg) ->
[name, user] = userRepo.nameUser msg
delete user.last_greeted_at if user.last_greeted_at
userRepo.save name, user
robot.respond /greeting/i, (msg) ->
greeting = randomGreeting()
user = userName msg
msg.send buildGreeting greeting, user
robot.respond /I speak (.+)/i, (msg) ->
lang = msg.match[1]
user = userName msg
greeting = findGreeting lang
if greeting
u = userRepo.findOrNew user
u.lang = lang
userRepo.save user, u
msg.send "You will now be greeted in #{greeting['lang']}."
else
msg.send cantFindIt lang
robot.respond /forget me/i, (msg) ->
[name, user] = userRepo.nameUser msg
userRepo.remove name
robot.respond /forget my greeting/i, (msg) ->
[name, user] = userRepo.nameUser msg
if user?.lang?
delete user.lang
userRepo.save name, user
# do in private
msg.send "I have already forgotten what we were talking about."
robot.respond /what is my greeting(\?)?/i, (msg) ->
[name, user] = userRepo.nameUser msg
if user?.lang?
msg.send buildGreeting findGreeting(user.lang), name
else
msg.send "No greeting set."
robot.respond /show greeting for (.+)/i, (msg) ->
lang = msg.match[1]
user = userName msg
greeting = findGreeting lang
# do in private
msg.send greeting && buildGreeting(greeting, user) || cantFindIt(lang)
#list languages
# robot.respond /list languages/i, (msg) ->
# # do in private
# msg.send greetingsNames().join(', ')
# #robot.sendPrivate "Here are the languages: " + greetingsNames().join(', ')
#user enters
robot.enter (msg) ->
name = userName msg
user = userRepo.find name
message = doUserGreet user, userRepo
console.log message
msg.send message if message
# name = userName msg
# console.log name
# if name.indexOf('laravelnewbie') > -1
# msg.send "#{name}, Hello, please type \"/nick AnyNameYouWant\" to change your name so we can more easily distinguish you, thanks."
# # cache hostname for a few minutes
# else
# user = userRepo.find name
# message = doUserGreet
# msg.send message if message
# user structure
# {
# name: 'liona',
# ...
# }
#
class UserRepository
KEY = 'user-settings'
constructor: (@brain) ->
find: (user) ->
@all().filter((u) -> u.name is user)?[0]
save: (name, data) ->
if data == null then data = {}
users = @all().filter (u) -> u.name isnt name
data.name = name
data.updated_at = Date.now()
users.push data
@brain.set KEY, users
name
findOrNew: (name) ->
user = @find(name) || @new(name)
new: (name, data) ->
data = name: name unless data
data.name = name
data.created_at = Date.now()
data
remove: (user) ->
@brain.set KEY, @all().filter (u) -> u.name isnt user
all: ->
@brain.get(KEY) || []
module.exports = UserRepository
# Da Greetings
greetings =
"afrikaans": {"lang": "Afrikaans", "greeting": "Goeie môre"},
"albanian": {"lang": "Albanian", "greeting": "Mirëmëngjes"},
"amharic": {"lang": "Amharic", "greeting": "Endermen aderkh"},
"arabic": {"lang": "Arabic", "greeting": "Sabah-il-kheir"},
"aramaic": {"lang": "Aramaic", "greeting": "Yasetel liesbukh"},
"armenian": {"lang": "Armenian", "greeting": "Bari luys"},
"assyrian": {"lang": "Assyrian", "greeting": "Kedamtookh brikhta"},
"azerbaijani": {"lang": "Azerbaijani", "greeting": "Sabahiniz Xeyr"},
"bamougoum": {"lang": "Bamougoum", "greeting": "Oli yah"},
"bangla": {"lang": "Bangla", "greeting": "Shuvo sokal"},
"basque": {"lang": "Basque", "greeting": "Egun on"},
"belarussian": {"lang": "Belarussian", "greeting": "Dobray ranitsy"},
"bemba": {"lang": "Bemba", "greeting": "Mwashibukeni"},
"bengali": {"lang": "Bengali", "greeting": "Shu-probhaat"},
"bisaya": {"lang": "Bisaya", "greeting": "Maayong adlaw"},
"binary": {"lang": "Binary", "greeting": "011001110110111101101111011001000010000001101101011011110111001001101110011010010110111001100111"},
"bosnian": {"lang": "Bosnian", "greeting": "Dobro jutro"},
"bulgarian": {"lang": "Bulgarian", "greeting": "Dobro utro"},
"cantonese": {"lang": "Cantonese", "greeting": "Zou san"},
"catalan": {"lang": "Catalan", "greeting": "Bon dia"},
"cebuano": {"lang": "Cebuano", "greeting": "Maayong buntag!"},
"chichewa": {"lang": "Chichewa", "greeting": "Mwadzuka bwanji"},
"chinese": {"lang": "Chinese", "greeting": "Zao shang hao"},
"mandarin": {"lang": "Mandarin", "greeting": "Zao shang hao"},
"cornish": {"lang": "Cornish", "greeting": "Myttin da"},
"corse": {"lang": "Corse", "greeting": "Bun ghjiornu"},
"creole": {"lang": "Creole", "greeting": "Bonjou"},
"croatian": {"lang": "Croatian", "greeting": "Dobro jutro"},
"czech": {"lang": "Czech", "greeting": "Dobré ráno"},
"danish": {"lang": "Danish", "greeting": "God morgen"},
"dari": {"lang": "Dari", "greeting": "Sob Bakhaer"},
"divehi": {"lang": "Divehi", "greeting": "Baajjaveri hedhuneh"},
"dutch": {"lang": "Dutch", "greeting": "Goedemorgen"},
"english": {"lang": "English", "greeting": "Good morning"},
"esperanto": {"lang": "Esperanto", "greeting": "Bonan matenon"},
"estonian": {"lang": "Estonian", "greeting": "Tere hommikust"},
"etsakor": {"lang": "Etsakor", "greeting": "Naigbia"},
"fanti": {"lang": "Fanti", "greeting": "Me ma wo akye"},
"fijian": {"lang": "Fijian", "greeting": "Sa Yadra"},
"filipino": {"lang": "Filipino", "greeting": "Magandang umaga po"},
"finnish": {"lang": "Finnish", "greeting": "Hyvää huomenta"},
"flanders": {"lang": "Flanders", "greeting": "Hey-Diddly-Ho"},
"flemish": {"lang": "Flemish", "greeting": "Goeie morgen"},
"french": {"lang": "French", "greeting": "Bonjour"},
"frisian": {"lang": "Frisian", "greeting": "Goeie moarn"},
"galician": {"lang": "Galician", "greeting": "Bos dias"},
"georgian": {"lang": "Georgian", "greeting": "Dila mshvidobisa"},
"german": {"lang": "German", "greeting": "Guten Morgen"},
"greek": {"lang": "Greek", "greeting": "Kali mera"},
"greenlandic": {"lang": "Greenlandic", "greeting": "Iterluarit"},
"gujarati": {"lang": "Gujarati", "greeting": "Subh Prabhat"},
"hakka": {"lang": "Hakka", "greeting": "On zoh"},
"hausa": {"lang": "Hausa", "greeting": "Inaa kwana"},
"hawaiian": {"lang": "Hawaiian", "greeting": "Aloha kakahiaka"},
"hebrew": {"lang": "Hebrew", "greeting": "Boker tov"},
"hexadecimal": {"lang": "Hexadecimal", "greeting": "67 6F 6F 64 20 6D 6F 72 6E 69 6E 67"},
"hiligaynon": {"lang": "Hiligaynon", "greeting": "Maayong aga"},
"hindi": {"lang": "Hindi", "greeting": "Shubh prabhat"},
"hungarian": {"lang": "Hungarian", "greeting": "Jo reggelt"},
"icelandic": {"lang": "Icelandic", "greeting": "Gódan daginn"},
"ilocano": {"lang": "Ilocano", "greeting": "Naimbag nga Aldaw"},
"indonesian": {"lang": "Indonesian", "greeting": "Selamat pagi"},
"irish": {"lang": "Irish", "greeting": "Dia duit ar maidin"},
"italian": {"lang": "Italian", "greeting": "Buon giorno"},
"japanese": {"lang": "Japanese", "greeting": "Ohayo gozaimasu"},
"kannada": {"lang": "Kannada", "greeting": "Shubhodaya"},
"kapampangan": {"lang": "Kapampangan", "greeting": "Mayap a abak"},
"kazakh": {"lang": "Kazakh", "greeting": "Kairly Tan"},
"khmer": {"lang": "Khmer", "greeting": "Arrun Suo Sdey"},
"kimeru": {"lang": "Kimeru", "greeting": "Muga rukiiri"},
"kinyarwanda": {"lang": "Kinyarwanda", "greeting": "Muraho"},
"konkani": {"lang": "Konkani", "greeting": "Dev Tuka Boro Dis Divum"},
"korean": {"lang": "Korean", "greeting": "Annyunghaseyo"},
"kurdish badini": {"lang": "Kurdish Badini", "greeting": "Spede bash"},
"kurdish sorani": {"lang": "Kurdish Sorani", "greeting": "Beyani bash"},
"lao": {"lang": "Lao", "greeting": "Sabaidee"},
"latvian": {"lang": "Latvian", "greeting": "Labrit"},
"lithuanian": {"lang": "Lithuanian", "greeting": "Labas rytas"},
"lozi": {"lang": "Lozi", "greeting": "U zuhile"},
"luganda": {"lang": "Luganda", "greeting": "Wasuze otyano"},
"luo": {"lang": "Luo", "greeting": "Oyawore"},
"luxembourg": {"lang": "Luxembourg", "greeting": "Gudde moien"},
"macedonian": {"lang": "Macedonian", "greeting": "Dobro utro"},
"malayalam": {"lang": "Malayalam", "greeting": "Suprabhatham"},
"malay": {"lang": "Malay", "greeting": "Selamat pagi"},
"maltese": {"lang": "Maltese", "greeting": "Għodwa it-tajba"},
"manx": {"lang": "Manx", "greeting": "Moghrey mie"},
"maori": {"lang": "Maori", "greeting": "Ata marie"},
"mapudungun": {"lang": "Mapudungun", "greeting": "Mari mari"},
"marathi": {"lang": "Marathi", "greeting": "Suprabhat"},
"mongolian": {"lang": "Mongolian", "greeting": "Öglouny mend"},
"navajo": {"lang": "Navajo", "greeting": "Yá'át'ééh abíní"},
"ndebele": {"lang": "Ndebele", "greeting": "Livukenjani"},
"nepali": {"lang": "Nepali", "greeting": "Subha prabhat"},
"northern sotho": {"lang": "Northern Sotho", "greeting": "Thobela"},
"norwegian": {"lang": "Norwegian", "greeting": "God morgen"},
"owambo": {"lang": "Owambo", "greeting": "Wa lalapo"},
"pashto": {"lang": "Pashto", "greeting": "Sahar de pa Khair"},
"persian": {"lang": "Persian", "greeting": "Subbakhair"},
"pirate": {"lang": "Pirate", "greeting": "Avast, ye scurvy dog"},
"polish": {"lang": "Polish", "greeting": "Witaj"},
"polynesian": {"lang": "Polynesian", "greeting": "Ia ora na"},
"portuguese": {"lang": "Portuguese", "greeting": "Bom dia"},
"punjabi": {"lang": "Punjabi", "greeting": "Sat Shri Akal"},
"rapa nui": {"lang": "Rapa Nui", "greeting": "Iorana"},
"romanian": {"lang": "Romanian", "greeting": "Buna dimineata"},
"russian": {"lang": "Russian", "greeting": "Dobroye utro"},
"samoan": {"lang": "Samoan", "greeting": "Talofa lava"},
"sanskrit": {"lang": "Sanskrit", "greeting": "Suprabhataha"},
"sardinian": {"lang": "Sardinian", "greeting": "Bona dia"},
"serbian": {"lang": "Serbian", "greeting": "Dobro jutro"},
"shona": {"lang": "Shona", "greeting": "Mangwanani"},
"sinhalese": {"lang": "Sinhalese", "greeting": "Suba Udesanak Wewa"},
"slovak": {"lang": "Slovak", "greeting": "Dobré ráno"},
"slovenian": {"lang": "Slovenian", "greeting": "Dobro jutro"},
"somalian": {"lang": "Somalian", "greeting": "Subax wanaagsan"},
"southern sotho": {"lang": "Southern Sotho", "greeting": "Dumela"},
"spanish": {"lang": "Spanish", "greeting": "Buenos dias"},
"swahili": {"lang": "Swahili", "greeting": "Habari za asubuhi"},
"swazi": {"lang": "Swazi", "greeting": "Sawubona"},
"swedish": {"lang": "Swedish", "greeting": "God morgon"},
"tagalog": {"lang": "Tagalog", "greeting": "Magandang umaga"},
"taiwanese": {"lang": "Taiwanese", "greeting": "Gau cha"},
"tamil": {"lang": "Tamil", "greeting": "Kaalai Vannakkam"},
"telugu": {"lang": "Telugu", "greeting": "Subhodayamu"},
"tetum": {"lang": "Tetum", "greeting": "Dader diak"},
"thai": {"lang": "Thai", "greeting": "Aroon-Sawass"},
"tibetan": {"lang": "Tibetan", "greeting": "Nyado delek"},
"tonga": {"lang": "Tonga", "greeting": "Mwabuka buti"},
"tswana": {"lang": "Tswana", "greeting": "Dumela"},
"twi": {"lang": "Twi", "greeting": "Me ma wo maakye"},
"turkish": {"lang": "Turkish", "greeting": "Günaydin"},
"turkmen": {"lang": "Turkmen", "greeting": "Ertiringiz haiyirli bolsun"},
"ukrainian": {"lang": "Ukrainian", "greeting": "Dobri ranok"},
"urdu": {"lang": "Urdu", "greeting": "Subha Ba-khair"},
"uzbek": {"lang": "Uzbek", "greeting": "Khairli kun"},
"vietnamese": {"lang": "Vietnamese", "greeting": "Xin chao"},
"welsh": {"lang": "Welsh", "greeting": "Bore da"},
"xhosa": {"lang": "Xhosa", "greeting": "Bhota"},
"xitsonga": {"lang": "Xitsonga", "greeting": "Avuxeni"},
"yoruba": {"lang": "Yoruba", "greeting": "E karo"},
"zulu": {"lang": "Zulu", "greeting": "Sawubona"},
"!kung san": {"lang": "!Kung san", "greeting": "Tuwa"},
"australian": {"lang": "Australian", "greeting": "G'day"},
"klingon": {"lang": "Klingon", "greeting": "nuqneH"},
"tlhingan hol": {"lang": "tlhIngan Hol", "greeting": "nuqneH"},
"vulcan": {"lang": "Vulcan", "greeting": "Dif-tor heh smusma"},
"romulan": {"lang": "Romulan", "greeting": "Brhon mnekha"},
"ferengi": {"lang": "Ferengi", "greeting": "Welcome to our home. Please place your thumbprint on the legal waivers and deposit your admission fee in the slot by the door. Remember, my house is my house,"}
module.exports = greetings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment