slackbotsy 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f5a31ca60d39e4a589b2b8b2b317f16fa3eee69
4
- data.tar.gz: 4e90621ec6c413ce68766cd17bb280d1739217ea
3
+ metadata.gz: 78670e10397ffcfd1cc98a51af115c7e5526e460
4
+ data.tar.gz: 53d8c0d28534b2068595e93cf9b9c64ddc669709
5
5
  SHA512:
6
- metadata.gz: 22ec1b91901b433cd01fa242958be1c72db3cd811ec6340ac9f4e5efe23294f547acbe07539f993fc83750085be5f8e0f63d9f6a54f2e5d893a57060961f26f9
7
- data.tar.gz: ff8d8701ad26335465be08863c6303a492dbb4b06e716871ea65ca91c91d948e395163bf951ea1c7d0fc5275b05d64e8a6f10771de2742af359d18a281d4b38c
6
+ metadata.gz: d3e36dc080d170fd71233061dd97ff52f9e14822a6aeedc96269ffacf98bcd0c55b79149abdfa81650a66ecaca1d80b2294251a5c13d4a874a94bce7dab29722
7
+ data.tar.gz: a2114b3535e6a0ca2e8146ed1828eec5e71a69de67325a3df42eadc1fdd6bd6513b1c5288368da87a494917d5a1168b6577b6a6004e85eb3bc3f92dfe291867a
data/README.md CHANGED
@@ -36,14 +36,100 @@ gem install slackbotsy
36
36
 
37
37
  ## Setup
38
38
 
39
- botsy requires (at least) two webhooks setup in slack:
39
+ botsy requires some or all of the following integrations setup in slack:
40
40
 
41
- * outgoing webhook: for slack to send messages out to botsy
41
+ * outgoing webhook: for slack to send free-format messages out to botsy
42
+ * slash command: for slack to send messages prefixed with a slash
43
+ command, for example `/botsy`
42
44
  * incoming webhook: for botsy `say` and `attach` methods to respond
43
45
 
44
46
  Set these up at https://your_team.slack.com/services/new and copy
45
47
  the webhook urls/tokens to botsy's config as below.
46
48
 
49
+ ## Sending messages to botsy
50
+
51
+ You have three choices of how to send messages to botsy. It is fine to
52
+ mix and match these to listen to different kinds of messages.
53
+
54
+ ### Global outgoing webhook
55
+
56
+ This sends all messages from all channels to botsy, but requires a
57
+ trigger word (e.g. `botsy`). This word must be included in
58
+ `hear`-block regexes. The return from the hear block is posted
59
+ publically to the sending channel. Alternatively, return `nil` and use
60
+ `say` or `attach` to craft an asynchronous response.
61
+
62
+ Requires you to set the `outgoing_token` config variable.
63
+
64
+ ### Per-channel outgoing webhooks
65
+
66
+ These do not require any trigger word or magic prefix, but you must
67
+ setup a webhook for every channel in which you want botsy to
68
+ respond. This is useful to give your botsy a little personality, by
69
+ seeming to respond to users' comments without being prompted.
70
+
71
+ As with global webhooks, `hear` block returns public responses, or use
72
+ `say` or `attach`.
73
+
74
+ Add all required channel tokens to the `outgoing_token` array.
75
+
76
+ ### Slash commands
77
+
78
+ You can define one or more slash integrations that send any messages
79
+ with a slash trigger prefix (for example `/botsy`). A major advantage
80
+ is that messages can be triggered from all channels, groups and
81
+ private chats.
82
+
83
+ The return value of the `hear`-block is sent as a _private_ response
84
+ to the user (like communication from the built-in `slackbot`). This
85
+ can be useful for requesting verbose bot information without spamming
86
+ channels. To respond _publically_ from a `hear`-block, post direct to
87
+ the channel using `say` or `attach`.
88
+
89
+ The slash trigger itself is used in the `hear`-block regex match, so
90
+ you may setup as many slash integrations as you like with different
91
+ triggers, and respond appropriately.
92
+
93
+ Add all slash integration tokens to config `slash_token`.
94
+
95
+ ## Sending messages to slack
96
+
97
+ There are four methods of sending data to slack in response to
98
+ matching a `hear` block. These may be mixed as necessary.
99
+
100
+ ### Simple response in return
101
+
102
+ The return value from a `hear`-block is returned to slack in the http
103
+ response to the sent message. This is a lightweight synchronous
104
+ response to the same channel, and is sufficient for many needs.
105
+
106
+ Note: responses to outgoing webhooks are posted publically, responses
107
+ to slash commands are private to the requesting user (and appear to
108
+ come from `slackbot`).
109
+
110
+ ### Post simple text to an incoming webhook with `say()`
111
+
112
+ Asynchronous plain-text responses may be sent with the `say()` method
113
+ from inside a `hear`-block. This is useful for multiple replies or to
114
+ pass extra arguments, such as `channel`, to post response to a
115
+ specific channel. It is also useful when botsy is expanded to be a
116
+ general API for slack integration with third-party applications.
117
+
118
+ Set the value of the `incoming_webhook` config variable to the URL
119
+ given in your slack Incoming Webhook integration.
120
+
121
+ ### Post attachments to an incoming webook with `attach()`
122
+
123
+ Works exactly like `say()`, except you may post JSON containing
124
+ complex attachment information.
125
+
126
+ ### Upload data directly to slack using the API
127
+
128
+ Call `post_message()` to get full access to the Slack Web API
129
+ `/chat.postMessage` call. Argument is a hash containing the same
130
+ variables described in the Web API docs. This allows uploading of text
131
+ snippets and binary data.
132
+
47
133
  ## Example usage
48
134
 
49
135
  ```ruby
@@ -60,8 +146,8 @@ config = {
60
146
 
61
147
  bot = Slackbotsy::Bot.new(config) do
62
148
 
63
- hear /echo\s+(.+)/ do |data, mdata|
64
- "I heard #{data['user_name']} say '#{mdata[1]}' in #{data['channel_name']}"
149
+ hear /echo\s+(.+)/ do |mdata|
150
+ "I heard #{user_name} say '#{mdata[1]}' in #{channel_name}"
65
151
  end
66
152
 
67
153
  hear /flip out/i do
@@ -109,32 +109,45 @@ module Slackbotsy
109
109
  ## pass list of files containing hear statements, to be opened and evaled
110
110
  def eval_scripts(*files)
111
111
  files.flatten.each do |file|
112
- self.instance_eval File.open(file).read
112
+ self.instance_eval(File.open(file).read)
113
113
  end
114
114
  end
115
115
 
116
116
  ## check message and run blocks for any matches
117
- def handle_item(msg)
118
- return nil unless @options['outgoing_token'].include? msg[:token] # ensure messages are for us from slack
117
+ def handle_outgoing_webhook(msg)
118
+ return nil unless @options['outgoing_token'].include?(msg[:token]) # ensure messages are for us from slack
119
119
  return nil if msg[:user_name] == 'slackbot' # do not reply to self
120
120
  return nil unless msg[:text].is_a?(String) # skip empty messages
121
121
 
122
- ## loop things to look for and collect immediate responses
123
- ## rescue everything here so the bot keeps running even with a broken script
124
- responses = @listeners.map do |hear|
125
- if mdata = msg[:text].strip.match(hear.regex)
122
+ responses = get_responses(msg, msg[:text].strip)
123
+
124
+ if responses
125
+ { text: responses.compact.join("\n") }.to_json # webhook responses are json
126
+ end
127
+ end
128
+
129
+ def handle_slash_command(msg)
130
+ return nil unless @options['slash_token'].include?(msg[:token])
131
+
132
+ responses = get_responses(msg, "#{msg[:command]} #{msg[:text]}".strip)
133
+
134
+ if responses
135
+ responses.join("\n") # plain text responses
136
+ end
137
+ end
138
+
139
+ ## run on msg all hear blocks matching text
140
+ def get_responses(msg, text)
141
+ message = Slackbotsy::Message.new(self, msg)
142
+ @listeners.map do |listener|
143
+ text.match(listener.regex) do |mdata|
126
144
  begin
127
- Slackbotsy::Message.new(self, msg).instance_exec(mdata, &hear.proc)
128
- rescue => err
145
+ message.instance_exec(mdata, &listener.proc)
146
+ rescue => err # keep running even with a broken script, but report the error
129
147
  err
130
148
  end
131
149
  end
132
150
  end
133
-
134
- ## format any replies for http response
135
- if responses
136
- { text: responses.compact.join("\n") }.to_json
137
- end
138
151
  end
139
152
 
140
153
  end
@@ -1,3 +1,3 @@
1
1
  module Slackbotsy
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slackbotsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-20 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.4.5
122
+ rubygems_version: 2.2.3
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Ruby bot for Slack chat.