Class: SlackRTMApi

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

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ SlackRTMApi

Returns a new instance of SlackRTMApi.



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
# File 'lib/botanalytics/slack.rb', line 9

def initialize(params={})
    super(params)
    @slack_token = params.fetch(:slack_token, nil)
    raise ArgumentError.new "slack_token can not be nil or empty" if @slack_token.nil? || @slack_token.to_s.empty?
    @path = 'messages/slack/'
    @initialize_path = 'bots/slack/initialize/'
    @active_user = nil
    @active_team = nil
    @async = params.fetch(:async, false)
    informs("Logging enabled for #{self.class.name}...")
    if @async
        require 'concurrent'
        @executor_service = Concurrent::ThreadPoolExecutor.new(
            min_threads: 1,
            max_threads: 2,
            max_queue: 1000,
            fallback_policy: :caller_runs
        )
        @executor_service.post do
            fetch
        end
        informs("Mode: Async...")
    else
        Thread.new do
            fetch
        end
    end
end

Instance Method Details

#log_incoming(payload) ⇒ Object

Parameters:

  • payload

    Hash



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/botanalytics/slack.rb', line 91

def log_incoming(payload)
    if @active_team.nil? or @active_user.nil?
        fails(Exception.new('Team and Bot info is missing!'), 'Not initialized yet...')
    else
        validation = validate(payload)
        if validation[:ok]
            informs('Logging message...')
            payload['is_bot'] = false
            payload['team']= @active_team
            informs(payload)
            if @async
                @executor_service.post do
                    submits(payload, @path)
                end
            else
                submits(payload, @path)
            end
        else
            fails(validation[:err], validation[:reason], payload)
        end
    end
end

#log_outgoing(channel_id, message, params = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/botanalytics/slack.rb', line 114

def log_outgoing(channel_id, message, params = {})
    #thread=None, reply_broadcast=None, msg_payload=None

    if @active_team.nil? or @active_user.nil?
        fails(Exception.new('Team and Bot info is missing!'), 'Not initialized yet...')
    else
        thread = params.fetch(:thread, nil)
        reply_broadcast = params.fetch(:reply_broadcast, nil)
        msg_payload = params.fetch(:msg_payload, nil)
        if !msg_payload.nil?
            unless msg_payload.is_a?(Hash)
                fails(Exception.new("Expected format for msg_payload is Hash found #{msg_payload.class.name}"), 'Unexpected payload format!')
                return
            end
            msg = msg_payload.clone
            validation = validate(msg)
            if validation[:ok]
                msg['is_bot'] = true
                msg['ts'] = (Time.new.to_f*1000).to_s
                msg['team'] = @active_team
                msg['user'] = @active_user
                informs('Logging message...')
                informs(msg)
                if @async
                    @executor_service.post do
                        submits(msg, @path)
                    end
                else
                    submits(msg, @path)
                end
            else
                informs("Message does not contain 'type' field. Ignoring...")
            end
        else
            payload = Hash.new
            payload['type'] = 'message'
            payload['channel'] = channel_id
            payload['text'] = message
            payload['thread_ts'] = thread unless thread.nil?
            payload['reply_broadcast'] = reply_broadcast unless reply_broadcast.nil?
            payload['is_bot'] = true
            payload['ts'] = (Time.new.to_f*1000).to_s
            payload['team'] = @active_team
            payload['user'] = @active_user
            if @async
                @executor_service.post do
                    submits(payload, @path)
                end
            else
                submits(payload, @path)
            end
        end
    end
end