Class: Powerball::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/powerball/slack.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, channel, lottery, admins = []) ⇒ Slack

Returns a new instance of Slack.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/powerball/slack.rb', line 5

def initialize(token, channel, lottery, admins = [] )
  Slack.configure do |config|
    config.token = token
  end

  @client  = Slack::RealTime::Client.new
  @lottery = lottery
  @channel = channel
  @admins  = admins.map{|a| "@#{a}" }
  @messages = [
                "Yay, it's drawing time!",
                "Oh boy, I get to draw a winner!",
                "You mean I get to pick another winner?! :allthethings:",
                "It's time for another one. :confetti_ball:",
                "Another drawing :game_die:, who's it gonna be?",
                "Party on, Wayne :metal:. Draw us a winner.",
              ]

  @images   = [
                { title: "Powerball!",
                    image_url: "https://i.giphy.com/media/Ps8XflhsT5EVa/giphy.gif"                                },
                { title: "Who's it gonna be?",
                    image_url: "http://www.thinkgeek.com/images/products/zoom/2063_critical_hit_led_dice_set.gif" },
                { title: "Let's roll the dice!",
                    image_url: "https://studio.code.org/v3/assets/GBhvGLEcbJGFHdJfHkChqw/8TEb9oxGc.gif"           },
                { title: "Draw a card, any card...",
                    image_url: "https://c1.staticflickr.com/2/1262/1267623453_99002a752a_m.jpg"                   },
                { title: "Powerball!",
                    image_url: "http://a.abcnews.com/images/US/RT_Powerball_Machine_ER_160112_4x3_992.jpg"        },
              ]

  @lottery.chatroom = self

  @client.on :hello do
    puts "Successfully connected '#{@client.self.name}' to the '#{@client.team.name}' team at https://#{@client.team.domain}.slack.com."
    # bots cannot join channels. They must be invited
    #@client.web_client.channels_join(:name => @channel)
  end

  @client.on :close do |_data|
    puts 'Connection closing, exiting.'
  end

  @client.on :closed do |_data|
    puts 'Connection has been disconnected.'
  end

  @client.on :message do |data|
    puts data

    case data.text
    when "<@#{@client.self.id}> hi", 'powerball hi', "hi <@#{@client.self.id}>", 'hi powerball' then
      @client.message channel: data.channel, text: "Hi <@#{data.user}>!"

    when "<@#{@client.self.id}> drawing", 'powerball drawing' then
      username = @client.web_client.users_info(user: data.user)['user']['name']
      next unless admins.include? username

      @lottery.drawing
    end
  end
end

Instance Method Details

#active_membersObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/powerball/slack.rb', line 72

def active_members
  begin
    # The api will let you use ID or name, but only if the channel is public!
    list = @client.web_client.conversations_list(:types => 'public_channel, private_channel')
    chan = list['channels'].select { |c| c['name'] == @channel }.first['id']

    data = @client.web_client.conversations_members( :channel => chan, :limit => 500 )
    data['members'].map do |user|
      # we only want active users!
      next unless @client.web_client.users_getPresence(:user => user)['presence'] == 'active'

      @client.web_client.users_info(:user => user)['user']['name'] rescue nil
    end.compact
  rescue => e
    puts 'ERROR: cannot list channel members'
    puts e.message
    puts e.backtrace.join "\n"
    []
  end
end

#alert_admins(message) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/powerball/slack.rb', line 125

def alert_admins(message)
  @admins.each do |admin|
    begin
      channel = @client.web_client.im_open(:user => admin)['channel']['id']
      @client.web_client.chat_postMessage({ channel: channel, text: message, as_user: true })
    rescue => e
      puts "ERROR: could not send message to #{admin}"
    end
  end
end

#post_winner(winner) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/powerball/slack.rb', line 108

def post_winner(winner)
  begin
    slack = @client.web_client.users_info(user: "@#{winner}")['user']['id']
    @client.web_client.chat_postMessage({
                                      channel: @channel,
                                         text: "Congratulations <@#{slack}>, you're our latest winner! :tada: :clap:",
                                      as_user: true,
                                    })
    return true
  rescue => e
    puts 'ERROR: drawing incomplete'
    puts e.message
    puts e.backtrace.join "\n"
    return false
  end
end

#start!Object



68
69
70
# File 'lib/powerball/slack.rb', line 68

def start!
  @client.start!
end

#start_drawingObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/powerball/slack.rb', line 93

def start_drawing
  begin
    @client.web_client.chat_postMessage({
                                            channel: @channel,
                                               text: @messages.sample,
                                        attachments: [@images.sample],
                                            as_user: true,
                                      })
  rescue => e
    puts 'ERROR: cannot start drawing'
    puts e.message
    puts e.backtrace.join "\n"
  end
end