Class: Lita::Handlers::Tipbot

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/tipbot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hipchat_apiObject



338
339
340
341
342
343
# File 'lib/lita/handlers/tipbot.rb', line 338

def hipchat_api
  if @hipchat_api.nil?
    @hipchat_api = HipChat::API.new(config.hipchat_api_token)
  end
  @hipchat_api
end

#tipbot_apiObject



345
346
347
348
349
350
351
352
353
354
355
# File 'lib/lita/handlers/tipbot.rb', line 345

def tipbot_api
  if @tipbot_api.nil?
    params = {
      url: config.tipbot_url,
      auth_token: config.tipbot_auth_token,
      log: log
    }
    @tipbot_api = TipbotApi.new(params)
  end
  @tipbot_api
end

Instance Method Details

#active_room_members(room_jid) ⇒ Object

return a list of hipchat api v1 user hashes exclude any user with email in config.emails_to_exclude exclude any non-active user TODO: dont make an api call for every participant instead: make one call for all users, then check to see if each is an active participant



375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/lita/handlers/tipbot.rb', line 375

def active_room_members(room_jid)
  log.debug "looking up room jid: #{room_jid}"
  data = room_data(room_jid)
  log.debug "room_data: #{data.inspect}"
  results = []
  data['participants'].each do |p|
    user = hipchat_api.users_show(p['user_id'])
    next if user['user']['status'] != 'available'
    next if exclude_user? user['user']
    results << user['user']
  end
  results
end

#address(response) ⇒ Object



133
134
135
136
# File 'lib/lita/handlers/tipbot.rb', line 133

def address(response)
  hash = user_hash(response.user.mention_name)
  response.reply tipbot_api.address(hash)
end

#balance(response) ⇒ Object



138
139
140
141
# File 'lib/lita/handlers/tipbot.rb', line 138

def balance(response)
  hash = user_hash(response.user.mention_name)
  response.reply tipbot_api.balance(hash).to_s
end

#exclude_user?(user_hash) ⇒ Boolean



389
390
391
# File 'lib/lita/handlers/tipbot.rb', line 389

def exclude_user?(user_hash)
  config.emails_to_exclude.include?(user_hash['email'])
end

#hash_email(email) ⇒ Object



363
364
365
# File 'lib/lita/handlers/tipbot.rb', line 363

def hash_email(email)
  Digest::MD5.hexdigest email
end

#history(response) ⇒ Object



143
144
145
146
# File 'lib/lita/handlers/tipbot.rb', line 143

def history(response)
  hash = user_hash(response.user.mention_name)
  response.reply tipbot_api.history(hash)
end

#make_it_blaine(response) ⇒ Object

give a random person a random amount of 1-200 doge



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/lita/handlers/tipbot.rb', line 240

def make_it_blaine(response)
  images = [
    "http://cdn.images.express.co.uk/img/dynamic/79/590x/444280_1.jpg",
    "http://currentbuzz.my/Documents/Article/508176/TV%20street%20magician%20David%20Blaine_i2_cdnds_net.jpg",
    "http://i.dailymail.co.uk/i/pix/2012/10/08/article-2214386-15633B31000005DC-349_306x423.jpg",
    "http://i.telegraph.co.uk/multimedia/archive/01394/blaine_1394717c.jpg",
    "http://i.ytimg.com/vi/fqJ0GaVU344/hqdefault.jpg"
  ]

  response.reply([
    "What's that behind your ear? #{response.user.name} is makin' it Blaine!",
    images.sample
  ])

  src_hash = user_hash(response.user.mention_name)
  room_jid = response.message.source.room
  users    = active_room_members room_jid

  user = users.sample
  amount = Random.rand(1..200)

  log.info "tipping #{user['email']}"

  dest_hash = hash_email user['email']
  log.debug "SRC  HASH: #{src_hash}"
  log.debug "DEST HASH: #{dest_hash}"
  log.debug "NAME:      #{user['name']}"

  response.reply "Someone just received #{amount}!"
  tipbot_api.tip src_hash, dest_hash, amount
end

#make_it_crane(response) ⇒ Object

give a random person a random amount of 1-32 doge



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/lita/handlers/tipbot.rb', line 273

def make_it_crane(response)
  images = [
    "http://upload.wikimedia.org/wikipedia/en/6/68/Frasier_Crane_Shrink_Wrap_radio_station_KACL.jpg",
    "http://0.media.dorkly.cvcdn.com/47/63/8525949c344ca18f060a73d22e4cafde-dr-frasier-crane.jpg",
    "http://rushthefence.com/content/images/2014/Mar/Morgan_Bateson.jpg",
    "https://38.media.tumblr.com/97d26f1f5c3979b5d0ca26a49490c946/tumblr_mk8hl1u5va1s2n8qho1_500.png",
    "http://snakkle.wpengine.netdna-cdn.com/wp-content/uploads/2012/09/kelsey-grammer-cheers-tv-1985-photo-GC.jpg"
  ]

  response.reply([
    "#{response.user.name} is makin' it Frasier Crane!",
    images.sample
  ])

  src_hash = user_hash(response.user.mention_name)
  room_jid = response.message.source.room
  users    = active_room_members room_jid

  user = users.sample
  amount = Random.rand(1..32)

  log.info "tipping #{user['email']}"

  dest_hash = hash_email user['email']
  log.debug "SRC  HASH: #{src_hash}"
  log.debug "DEST HASH: #{dest_hash}"
  log.debug "NAME:      #{user['name']}"

  response.reply "#{user['name']} just received #{amount}!"
  tipbot_api.tip src_hash, dest_hash, amount
end

#make_it_rain(response) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/lita/handlers/tipbot.rb', line 164

def make_it_rain(response)
  images = [
    "http://disinfo.s3.amazonaws.com/wp-content/uploads/2013/12/make-it-rain-1jk6.jpg",
    "http://voice.instructure.com/Portals/166399/images/scrooge-mcduck-make-it-rain.jpeg",
    "http://cdn01.dailycaller.com/wp-content/uploads/2012/10/Big-Bird-Makin-It-Rain-e1349457102996.jpeg",
    "http://i.imgur.com/jSaI0pv.jpg",
    "http://i.imgur.com/0Rz84wK.gif"
  ]

  response.reply([
    "#{response.user.name} is makin' it rain!",
    images.sample
  ])

  src_hash = user_hash(response.user.mention_name)
  room_jid = response.message.source.room
  users    = active_room_members room_jid

  users.shuffle.each do |user|
    # skip tipper
    next if user['mention_name'] == response.user.mention_name

    log.info "tipping #{user['email']}"

    dest_hash = hash_email user['email']
    log.debug "SRC  HASH: #{src_hash}"
    log.debug "DEST HASH: #{dest_hash}"
    log.debug "NAME:      #{user['name']}"

    response.reply "A coin for #{user['name']}!"
    tipbot_api.tip src_hash, dest_hash, 1
  end
end

#make_it_reign(response) ⇒ Object

give a random person a random amount of 1-32 doge



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/lita/handlers/tipbot.rb', line 306

def make_it_reign(response)
  images = [
    "http://i.imgur.com/WOzIWAs.gif",
    "http://i.imgur.com/QlhuS09.gif",
    "http://i.imgur.com/8RC90ul.gif"
  ]

  response.reply([
    "#{response.user.name} is makin' it Reign!",
    images.sample
  ])

  src_hash = user_hash(response.user.mention_name)
  room_jid = response.message.source.room
  users    = active_room_members room_jid

  user = users.sample
  amount = Random.rand(1..32)

  log.info "tipping #{user['email']}"

  dest_hash = hash_email user['email']
  log.debug "SRC  HASH: #{src_hash}"
  log.debug "DEST HASH: #{dest_hash}"
  log.debug "NAME:      #{user['name']}"

  response.reply "#{user['name']} just received #{amount}!"
  tipbot_api.tip src_hash, dest_hash, amount
end

#make_it_wayne(response) ⇒ Object

haphazardly give some percentage of people in the room 1-50 doge



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/lita/handlers/tipbot.rb', line 199

def make_it_wayne(response)
  images = [
    "http://a4.files.saymedia-content.com/image/upload/c_fill,g_face,h_300,q_80,w_300/MTE5NTU2MzE2NDIxMTk1Mjc1.jpg",
    "http://www.whale.to/c/9_23_09_wayne_newton_kabik-14-570.jpg",
    "http://www.aceshowbiz.com/images/wennpic/wayne-newton-2013-american-music-awards-01.jpg",
    "http://thestarsurgery.com/wp-content/uploads/2013/06/Wayne-Newton.jpg",
    "http://www.mtv.com/crop-images/2013/08/27/WayneNewton_cr_EthanMiller_2009.jpg"
  ]

  response.reply([
    "Watch out! #{response.user.name} is makin' it Wayne!",
    images.sample
  ])

  src_hash = user_hash(response.user.mention_name)
  room_jid = response.message.source.room
  users    = active_room_members room_jid

  # wayne's generosity typically runs out before everyone gets a tip
  max_recipients = Random.rand(1..users.size)

  max_recipients.times do

    # wayne has had a few too many and forgets who he has already tipped
    user = users.sample

    log.info "tipping #{user['email']}"

    dest_hash = hash_email user['email']
    log.debug "SRC  HASH: #{src_hash}"
    log.debug "DEST HASH: #{dest_hash}"
    log.debug "NAME:      #{user['name']}"

    amount = Random.rand(1..50)
    response.reply "#{amount} for #{user['name']}!"

    tipbot_api.tip src_hash, dest_hash, amount
  end
end

#register(response) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/lita/handlers/tipbot.rb', line 122

def register(response)
  hash = user_hash(response.user.mention_name)

  log.info "Registering #{hash}"
  body = tipbot_api.register hash

  log.debug "register response: #{body}"
  # TODO: check for errors
  response.reply "You have been registered."
end

#room_data(room_jid) ⇒ Object



393
394
395
396
397
398
# File 'lib/lita/handlers/tipbot.rb', line 393

def room_data(room_jid)
  room_id = room_id_from_jid room_jid
  data = hipchat_api.rooms_show room_id
  log.debug "room #{room_id} data: #{data.inspect}"
  data['room']
end

#room_id_from_jid(room_jid) ⇒ Object



400
401
402
403
404
405
# File 'lib/lita/handlers/tipbot.rb', line 400

def room_id_from_jid(room_jid)
  data = hipchat_api.rooms_list
  log.debug "all room data: #{data.inspect}"
  room = data['rooms'].select {|r| r['xmpp_jid'] == room_jid}.first
  room.nil? ? nil : room['room_id']
end

#tip(response) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/lita/handlers/tipbot.rb', line 148

def tip(response)
  recipient, amount = response.match_data[1..2]
  from_hash = user_hash(response.user.mention_name)
  to_hash   = user_hash(recipient.slice(1..-1))
  tipbot_api.tip(from_hash, to_hash, amount)
  response.reply "Tip sent! Such kind shibe."
end

#user_hash(mention_name) ⇒ Object



357
358
359
360
361
# File 'lib/lita/handlers/tipbot.rb', line 357

def user_hash(mention_name)
  user_data = hipchat_api.users_list
  user = user_data['users'].select {|u| u['mention_name'] == mention_name}.first
  hash_email user['email']
end

#withdraw(response) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/lita/handlers/tipbot.rb', line 156

def withdraw(response)
  src_hash     = user_hash(response.user.mention_name)
  dest_address = response.match_data[1]

  resp = tipbot_api.withdraw(src_hash, dest_address)
  response.reply resp
end