Class: Slackbotsy::Bot
- Inherits:
-
Object
- Object
- Slackbotsy::Bot
- Defined in:
- lib/slackbotsy/bot.rb
Instance Method Summary collapse
-
#attach(ary, options = {}) ⇒ Object
simple wrapper on post to send attachment(s).
-
#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) ⇒ 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) ⇒ Bot
Returns a new instance of Bot.
11 12 13 14 15 16 17 18 19 |
# File 'lib/slackbotsy/bot.rb', line 11 def initialize() @options = ## use set of tokens for (more or less) O(1) lookup on multiple channels @options['outgoing_token'] = Array(@options['outgoing_token']).to_set @regexes = {} setup_incoming_webhook # http connection for async replies yield if block_given? # run any hear statements in block end |
Instance Method Details
#attach(ary, options = {}) ⇒ Object
simple wrapper on post to send attachment(s)
48 49 50 51 |
# File 'lib/slackbotsy/bot.rb', line 48 def attach(ary, = {}) = ary.is_a?(Array) ? ary : [ ary ] #force first arg to array post({ attachments: }.merge()) end |
#eval_scripts(*files) ⇒ Object
pass list of files containing hear statements, to be opened and evaled
59 60 61 62 63 |
# File 'lib/slackbotsy/bot.rb', line 59 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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/slackbotsy/bot.rb', line 66 def handle_item(msg) return nil unless @options['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 = @regexes.map do |regex, proc| if mdata = msg[:text].strip.match(regex) begin Slackbotsy::Message.new(self, msg).instance_exec(mdata, &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
54 55 56 |
# File 'lib/slackbotsy/bot.rb', line 54 def hear(regex, &block) @regexes[regex] = block end |
#post(options) ⇒ Object
raw post of hash to slack
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/slackbotsy/bot.rb', line 30 def post() payload = { username: @options['name'], channel: @options['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
43 44 45 |
# File 'lib/slackbotsy/bot.rb', line 43 def say(text, = {}) post({ text: text }.merge()) end |
#setup_incoming_webhook ⇒ Object
setup http connection for sending async incoming webhook messages to slack
22 23 24 25 26 27 |
# File 'lib/slackbotsy/bot.rb', line 22 def setup_incoming_webhook @uri = URI.parse "https://#{@options['team']}.slack.com/services/hooks/incoming-webhook?token=#{@options['incoming_token']}" @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = true @http.verify_mode = OpenSSL::SSL::VERIFY_PEER end |