slackbotsy 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -1
- data/lib/slackbotsy/api.rb +59 -0
- data/lib/slackbotsy/bot.rb +34 -3
- data/lib/slackbotsy/version.rb +1 -1
- data/lib/slackbotsy.rb +1 -0
- data/slackbotsy.gemspec +1 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce0da4a7d0888d5db3de53e15e4735fa5c4964bf
|
4
|
+
data.tar.gz: de9207d9d5a94e91d4efdb28c66a1d9164161dbd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6bd18dec8b80138b81ac4bab5386cead3eaf6df8c81b971777a68ab6fc7c4a607a3ac14351afa22d37ed8008c5b842dfcb7f87677443a976b05db4710ea8450
|
7
|
+
data.tar.gz: dcec448b9a05d8006071ba52656d75665fb24807ef093854c9f38e63fc294315bbabbcf724775915914a6117d2e84dcafcf5dd755b4278e92c27ef5b97950abb
|
data/README.md
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
# Slackbotsy
|
2
2
|
|
3
|
-
Ruby bot for Slack chat, inspired by
|
3
|
+
Ruby bot library for Slack chat, inspired by
|
4
|
+
the Campfire bot http://github.com/seejohnrun/botsy.
|
5
|
+
|
6
|
+
## Working example
|
7
|
+
|
8
|
+
This repo and gem provide a library for implementing slack botsy using
|
9
|
+
the web framework of your choice. For example, botsy could be embedded
|
10
|
+
into an existing Rails app to provide access to a database from slack.
|
11
|
+
|
12
|
+
For a fully-implemented and ready-to-deploy standalone bot, using
|
13
|
+
slackbotsy and sinatra, please proceed over to
|
14
|
+
https://github.com/rlister/slackbotsy_example. You will find full
|
15
|
+
instructions to configure and deploy your own bot.
|
4
16
|
|
5
17
|
## Installation
|
6
18
|
|
@@ -64,3 +76,35 @@ post '/' do
|
|
64
76
|
bot.handle_item(params)
|
65
77
|
end
|
66
78
|
```
|
79
|
+
|
80
|
+
## Web API
|
81
|
+
|
82
|
+
slackbotsy can now post to Slack using the
|
83
|
+
[Slack Web API](https://api.slack.com/web). It may be used alongside
|
84
|
+
the incoming webhooks to post.
|
85
|
+
|
86
|
+
Create a Slack user in your team for your bot, then create an api
|
87
|
+
token for that user at https://api.slack.com/web, and set the config
|
88
|
+
variable `api_token` when you configure botsy. Then you may use the
|
89
|
+
`post_message` or `upload` convenience methods to post simple messages
|
90
|
+
or upload files/text-snippets.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
config = {
|
94
|
+
'channel' => '#default',
|
95
|
+
'name' => 'botsy',
|
96
|
+
'api_token' => 'xoxp-0123456789-0123456789-0123456789-d34db33f'
|
97
|
+
}
|
98
|
+
|
99
|
+
bot = Slackbotsy::Bot.new(config) do
|
100
|
+
|
101
|
+
hear /ping/ do
|
102
|
+
post_message 'this is a simple posted message', channel: '#general'
|
103
|
+
end
|
104
|
+
|
105
|
+
hear /upload/ do
|
106
|
+
upload(file: File.new('/tmp/kitten.jpg'), channel: '#general')
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
```
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'httmultiparty'
|
2
|
+
|
3
|
+
module Slackbotsy
|
4
|
+
|
5
|
+
class Api
|
6
|
+
include HTTMultiParty
|
7
|
+
base_uri 'https://slack.com/api'
|
8
|
+
|
9
|
+
def initialize(token)
|
10
|
+
@token = token
|
11
|
+
end
|
12
|
+
|
13
|
+
## get a channel, group, im or user list
|
14
|
+
def get_objects(method, key)
|
15
|
+
self.class.get("/#{method}", query: { token: @token }).tap do |response|
|
16
|
+
raise "error retrieving #{key} from #{method}: #{response.fetch('error', 'unknown error')}" unless response['ok']
|
17
|
+
end.fetch(key)
|
18
|
+
end
|
19
|
+
|
20
|
+
def channels
|
21
|
+
@channels ||= get_objects('channels.list', 'channels')
|
22
|
+
end
|
23
|
+
|
24
|
+
def groups
|
25
|
+
@groups ||= get_objects('groups.list', 'groups')
|
26
|
+
end
|
27
|
+
|
28
|
+
def ims
|
29
|
+
@ims ||= get_objects('im.list', 'ims')
|
30
|
+
end
|
31
|
+
|
32
|
+
def users
|
33
|
+
@users ||= get_objects('users.list', 'members')
|
34
|
+
end
|
35
|
+
|
36
|
+
## join a channel, needed to post to channel
|
37
|
+
def join(channel)
|
38
|
+
self.class.post('/channels.join', body: {name: channel, token: @token}).tap do |response|
|
39
|
+
raise "error posting message: #{response.fetch('error', 'unknown error')}" unless response['ok']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
## send message to one channel as a single post with params text, channel, as_user
|
44
|
+
def post_message(params)
|
45
|
+
self.class.post('/chat.postMessage', body: params.merge({token: @token})).tap do |response|
|
46
|
+
raise "error posting message: #{response.fetch('error', 'unknown error')}" unless response['ok']
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
## upload a file or text snippet, with params file, filename, filetype, title, initial_comment, channels
|
51
|
+
def upload(params)
|
52
|
+
self.class.post('/files.upload', body: params.merge({token: @token})).tap do |response|
|
53
|
+
raise "error uploading file: #{response.fetch('error', 'unknown error')}" unless response['ok']
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/lib/slackbotsy/bot.rb
CHANGED
@@ -7,14 +7,14 @@ require 'set'
|
|
7
7
|
module Slackbotsy
|
8
8
|
|
9
9
|
class Bot
|
10
|
-
|
11
|
-
attr_accessor :listeners, :last_description
|
10
|
+
attr_accessor :listeners, :last_description, :api
|
12
11
|
|
13
12
|
def initialize(options, &block)
|
14
13
|
@options = options
|
15
14
|
@listeners = []
|
16
15
|
@options['outgoing_token'] = parse_outgoing_tokens(@options['outgoing_token'])
|
17
16
|
setup_incoming_webhook # http connection for async replies
|
17
|
+
setup_web_api # setup slack Web API
|
18
18
|
instance_eval(&block) if block_given? # run any hear statements in block
|
19
19
|
end
|
20
20
|
|
@@ -33,7 +33,14 @@ module Slackbotsy
|
|
33
33
|
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
34
34
|
end
|
35
35
|
|
36
|
-
##
|
36
|
+
## use api_token to setup Web API authentication
|
37
|
+
def setup_web_api
|
38
|
+
if @options['api_token']
|
39
|
+
@api = Api.new(@options['api_token'])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
## raw post of hash to slack webhook
|
37
44
|
def post(options)
|
38
45
|
payload = {
|
39
46
|
username: @options['name'],
|
@@ -57,6 +64,30 @@ module Slackbotsy
|
|
57
64
|
post({ attachments: attachments }.merge(options))
|
58
65
|
end
|
59
66
|
|
67
|
+
## simple wrapper on api.post_message (which calls chat.postMessage)
|
68
|
+
def post_message(text, options = {})
|
69
|
+
payload = {
|
70
|
+
username: @options['name'],
|
71
|
+
channel: @options['channel'],
|
72
|
+
text: text,
|
73
|
+
as_user: true
|
74
|
+
}.merge(options)
|
75
|
+
payload[:channel] = payload[:channel].gsub(/^#?/, '#') # chat.postMessage needs leading # on channel
|
76
|
+
@api.join(payload[:channel])
|
77
|
+
@api.post_message(payload)
|
78
|
+
end
|
79
|
+
|
80
|
+
## simple wrapper on api.upload (which calls files.upload)
|
81
|
+
## pass 'channel' as a csv list of channel names, otherwise same args as files.upload
|
82
|
+
def upload(options)
|
83
|
+
payload = options
|
84
|
+
channels = @api.channels # list of channel objects
|
85
|
+
payload[:channels] ||= (options.fetch(:channel, @options['channel'])).split(/[\s,]+/).map do |name|
|
86
|
+
channels.find { |c| name.match(/^#?#{c['name']}$/) }.fetch('id') # convert channel id to name
|
87
|
+
end.join(',')
|
88
|
+
@api.upload(payload)
|
89
|
+
end
|
90
|
+
|
60
91
|
## record a description of the next hear block, for use in help
|
61
92
|
def desc(command, description = nil)
|
62
93
|
@last_desc = [ command, description ]
|
data/lib/slackbotsy/version.rb
CHANGED
data/lib/slackbotsy.rb
CHANGED
data/slackbotsy.gemspec
CHANGED
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.
|
4
|
+
version: 0.3.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-
|
11
|
+
date: 2015-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: httmultiparty
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Ruby bot for Slack chat.
|
70
84
|
email:
|
71
85
|
- rlister@gmail.com
|
@@ -79,6 +93,7 @@ files:
|
|
79
93
|
- README.md
|
80
94
|
- Rakefile
|
81
95
|
- lib/slackbotsy.rb
|
96
|
+
- lib/slackbotsy/api.rb
|
82
97
|
- lib/slackbotsy/bot.rb
|
83
98
|
- lib/slackbotsy/helper.rb
|
84
99
|
- lib/slackbotsy/message.rb
|
@@ -104,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
119
|
version: '0'
|
105
120
|
requirements: []
|
106
121
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.2.3
|
108
123
|
signing_key:
|
109
124
|
specification_version: 4
|
110
125
|
summary: Ruby bot for Slack chat.
|