Class: Slackbotsy::Bot
- Inherits:
-
Object
- Object
- Slackbotsy::Bot
- Defined in:
- lib/slackbotsy/bot.rb
Instance Attribute Summary collapse
-
#last_description ⇒ Object
Returns the value of attribute last_description.
-
#listeners ⇒ Object
Returns the value of attribute listeners.
Instance Method Summary collapse
-
#attach(ary, options = {}) ⇒ Object
simple wrapper on post to send attachment(s).
-
#desc(command, description = nil) ⇒ Object
record a description of the next hear block, for use in help.
-
#eval_scripts(*files) ⇒ Object
pass list of files containing hear statements, to be opened and evaled.
-
#handle_item(msg) ⇒ Object
check message and run blocks for any matches.
-
#hear(regex, &block) ⇒ Object
add regex to things to hear.
-
#initialize(options, &block) ⇒ Bot
constructor
A new instance of Bot.
-
#post(options) ⇒ Object
raw post of hash to slack.
-
#say(text, options = {}) ⇒ Object
simple wrapper on post to send text.
-
#setup_incoming_webhook ⇒ Object
setup http connection for sending async incoming webhook messages to slack.
Constructor Details
#initialize(options, &block) ⇒ Bot
Returns a new instance of Bot.
13 14 15 16 17 18 19 20 21 |
# File 'lib/slackbotsy/bot.rb', line 13 def initialize(, &block) = ## use set of tokens for (more or less) O(1) lookup on multiple channels ['outgoing_token'] = Array(['outgoing_token']).to_set @listeners = [] setup_incoming_webhook # http connection for async replies instance_eval(&block) if block_given? # run any hear statements in block end |
Instance Attribute Details
#last_description ⇒ Object
Returns the value of attribute last_description.
11 12 13 |
# File 'lib/slackbotsy/bot.rb', line 11 def last_description @last_description end |
#listeners ⇒ Object
Returns the value of attribute listeners.
11 12 13 |
# File 'lib/slackbotsy/bot.rb', line 11 def listeners @listeners end |
Instance Method Details
#attach(ary, options = {}) ⇒ Object
simple wrapper on post to send attachment(s)
52 53 54 55 |
# File 'lib/slackbotsy/bot.rb', line 52 def attach(ary, = {}) = ary.is_a?(Array) ? ary : [ ary ] #force first arg to array post({ attachments: }.merge()) end |
#desc(command, description = nil) ⇒ Object
record a description of the next hear block, for use in help
58 59 60 |
# File 'lib/slackbotsy/bot.rb', line 58 def desc(command, description = nil) @last_desc = [ command, description ] end |
#eval_scripts(*files) ⇒ Object
pass list of files containing hear statements, to be opened and evaled
69 70 71 72 73 |
# File 'lib/slackbotsy/bot.rb', line 69 def eval_scripts(*files) files.flatten.each do |file| self.instance_eval File.open(file).read end end |
#handle_item(msg) ⇒ Object
check message and run blocks for any matches
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/slackbotsy/bot.rb', line 76 def handle_item(msg) return nil unless ['outgoing_token'].include? msg[:token] # ensure messages are for us from slack return nil if msg[:user_name] == 'slackbot' # do not reply to self return nil unless msg[:text].is_a?(String) # skip empty messages ## loop things to look for and collect immediate responses ## rescue everything here so the bot keeps running even with a broken script responses = @listeners.map do |hear| if mdata = msg[:text].strip.match(hear.regex) begin Slackbotsy::Message.new(self, msg).instance_exec(mdata, &hear.proc) rescue => err err end end end ## format any replies for http response if responses { text: responses.compact.join("\n") }.to_json end end |
#hear(regex, &block) ⇒ Object
add regex to things to hear
63 64 65 66 |
# File 'lib/slackbotsy/bot.rb', line 63 def hear(regex, &block) @listeners << OpenStruct.new(regex: regex, desc: @last_desc, proc: block) @last_desc = nil end |
#post(options) ⇒ Object
raw post of hash to slack
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/slackbotsy/bot.rb', line 34 def post() payload = { username: ['name'], channel: ['channel'] }.merge() payload[:channel] = payload[:channel].gsub(/^#?/, '#') #slack api needs leading # on channel request = Net::HTTP::Post.new(@uri.request_uri) request.set_form_data(payload: payload.to_json) @http.request(request) return nil # so as not to trigger text in outgoing webhook reply end |
#say(text, options = {}) ⇒ Object
simple wrapper on post to send text
47 48 49 |
# File 'lib/slackbotsy/bot.rb', line 47 def say(text, = {}) post({ text: text }.merge()) end |
#setup_incoming_webhook ⇒ Object
setup http connection for sending async incoming webhook messages to slack
24 25 26 27 28 29 30 31 |
# File 'lib/slackbotsy/bot.rb', line 24 def setup_incoming_webhook ## incoming_webhook will be used if provided, otherwise fallback to old-style url with team and token url = .fetch('incoming_webhook', false) || "https://#{@options['team']}.slack.com/services/hooks/incoming-webhook?token=#{@options['incoming_token']}" @uri = URI.parse(url) @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_PEER end |