slack-bot-server 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +19 -0
- data/README.md +136 -5
- data/lib/slack_bot_server/bot.rb +2 -3
- data/lib/slack_bot_server/remote_control.rb +5 -1
- data/lib/slack_bot_server/server.rb +4 -0
- data/lib/slack_bot_server/simple_bot.rb +7 -0
- data/lib/slack_bot_server/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1a580f416814eef8f1582047b3f748022b2f094
|
4
|
+
data.tar.gz: e307931094203fe4590049d3d4eb966ad4a96fed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88d60ed5fc1b11d4df51d4ebfe9f92042e660a61bed513130d22a200fb4bee70001655320f5683b0c0418336ec50ede9facfbc36961105bbfa90199b1962a0d2
|
7
|
+
data.tar.gz: 48eae3c8528df390699a124782b558d64d36685d4f83b014b11dc3b4d89857c654c6c27efdfb02ae47e200b112e8c5773af0ed0b1eaebd9c0c4a992f8b8ac721
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
Thanks for helping make this software better! Contributing is super easy, with just a few guidelines.
|
4
|
+
|
5
|
+
## Bugs
|
6
|
+
|
7
|
+
If you've found a bug in the software, please report it using [GitHub Issues](https://github.com/exciting-io/slack_bot_server/issues). Please include the version (or SHA) of this project that you're using, along with which version of Ruby and any other dependencies that you think might be relevant.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
If you'd got an idea for an improvement or a new feature, that's fantastic! We only ask that you also include specs to cover that behaviour, and that you implement it in a branch to easy merging.
|
12
|
+
|
13
|
+
1. Create a new branch for your feature
|
14
|
+
2. Implement it, along with new/modified specs
|
15
|
+
3. Submit a pull request describing the motivation for your new feature, and how to use it.
|
16
|
+
|
17
|
+
Please don't bump the gem version -- we'll take care of that.
|
18
|
+
|
19
|
+
Thanks again, and have a great day!
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# SlackBotServer
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/exciting-io/slack-bot-server)
|
4
4
|
|
5
|
-
|
5
|
+
If you're building an integration just for yourself, running a single bot isn't too hard and there are plenty of examples available. However, if you're building an integration for your *product* to connect with multiple teams, running multiple instances of that bot is a bit trickier.
|
6
|
+
|
7
|
+
This server is designed to hopefully make it easier to manage running bots for multiple teams at the same time, including managing their connections and adding and removing them dynamically.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -22,17 +24,146 @@ Or install it yourself as:
|
|
22
24
|
|
23
25
|
## Usage
|
24
26
|
|
25
|
-
|
27
|
+
To use the server in your application, you'll need to create a short script that sets up your integration and then runs the server process. Here's a simple example:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
#!/usr/bin/env ruby
|
31
|
+
|
32
|
+
require 'slack_bot_server'
|
33
|
+
require 'slack_bot_server/redis_queue'
|
34
|
+
require 'slack_bot_server/simple_bot'
|
35
|
+
|
36
|
+
# Use a Redis-based queue to add/remove bots and to trigger
|
37
|
+
# bot messages to be sent
|
38
|
+
queue = SlackBotServer::RedisQueue.new(Redis.new)
|
39
|
+
|
40
|
+
# Create a new server using that queue
|
41
|
+
server = SlackBotServer::Server.new(queue: queue)
|
42
|
+
|
43
|
+
# How your application-specific should be created when the server
|
44
|
+
# is told about a new slack api token to connect with
|
45
|
+
server.on_new_token do |token|
|
46
|
+
# Return a new bot instance to the server. `SimpleBot` is a provided
|
47
|
+
# example bot with some very simple behaviour.
|
48
|
+
SlackBotServer::SimpleBot.new(token: token)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Actually start the server. This line is blocking; code after
|
52
|
+
# it won't be executed.
|
53
|
+
server.start
|
54
|
+
```
|
55
|
+
|
56
|
+
Running this script will start a server and keep it running; you may wish to use a tool like [Foreman](http://ddollar.github.io/foreman/) to actually start it and manage it in production.
|
57
|
+
|
58
|
+
### Writing a bot
|
59
|
+
|
60
|
+
The provided example `SimpleBot` illustrates the main ways to build a bot:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'slack_bot_server/bot'
|
64
|
+
|
65
|
+
class SlackBotServer::SimpleBot < SlackBotServer::Bot
|
66
|
+
# Set the username displayed in Slack
|
67
|
+
username 'SimpleBot'
|
68
|
+
|
69
|
+
# Respond to mentions in the connected chat room (defaults to #general).
|
70
|
+
# As well as the normal data provided by Slack's API, we add the `message`,
|
71
|
+
# which is the `text` parameter with the username stripped out. For example,
|
72
|
+
# When a user sends 'simple_bot: how are you?', the `message` data contains
|
73
|
+
# only 'how are you'.
|
74
|
+
on_mention do |data|
|
75
|
+
reply text: "You said '#{data['message']}', and I'm frankly fascinated."
|
76
|
+
end
|
77
|
+
|
78
|
+
# Respond to messages sent via IM communication directly with the bot.
|
79
|
+
on_im do
|
80
|
+
reply text: "Hmm, OK, let me get back to you about that."
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
### Advanced example
|
86
|
+
|
87
|
+
This is a more advanced example of a server script, based on the that used by [Harmonia](https://harmonia.io), the product from which this was extracted.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
#!/usr/bin/env ruby
|
91
|
+
|
92
|
+
require 'slack_bot_server'
|
93
|
+
require 'slack_bot_server/redis_queue'
|
94
|
+
require 'harmonia/slack_bot'
|
95
|
+
|
96
|
+
# Use a Redis-based queue to add/remove bots and to trigger
|
97
|
+
# bot messages to be sent. In this case we connect to the same
|
98
|
+
# redis instance as Resque, just for convenience.
|
99
|
+
queue = SlackBotServer::RedisQueue.new(Resque.redis)
|
100
|
+
|
101
|
+
server = SlackBotServer::Server.new(queue: queue)
|
102
|
+
|
103
|
+
server.on_new_token do |token|
|
104
|
+
# Our bots need to know some data about the team they are connecting
|
105
|
+
# to, like specifics of their account and their tasks
|
106
|
+
team_data = Harmonia.find_team_data_by_slack_api_token(token)
|
107
|
+
|
108
|
+
# Our bot instance stores that data in an instance variable internally
|
109
|
+
# and then refers to it when it receives messages
|
110
|
+
Harmonia::SlackBot.new(token: token, data: team_data)
|
111
|
+
end
|
112
|
+
|
113
|
+
# When the server starts we need to find all the teams which have already
|
114
|
+
# set up integrations and ensure their bots are launched immediately
|
115
|
+
Harmonia.all_existing_slack_api_tokens.each do |token|
|
116
|
+
server.add_token(token)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Actually start the server. The pre-loaded bots will connect immediately,
|
120
|
+
# and we can add new bots by sending messages using the queue.
|
121
|
+
server.start
|
122
|
+
```
|
123
|
+
|
124
|
+
### Managing bots
|
125
|
+
|
126
|
+
When someone in your application wspants to connect their account with Slack, they'll need to provide a bot API token, which your application should store.
|
127
|
+
|
128
|
+
In order to actually create and connect their bot, you can use the remote
|
129
|
+
control to add the token to the server.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
# Somewhere within your application
|
133
|
+
queue = SlackBotServer::RedisQueue.new(Redis.new)
|
134
|
+
slack_remote = SlackBotServer::RemoteControl.new(queue: queue)
|
135
|
+
slack_remote.add_token('user-accounts-slack-api-token')
|
136
|
+
```
|
137
|
+
|
138
|
+
This will queue the token to be added by the server, using the `on_new_token` block provided in the server script.
|
139
|
+
|
140
|
+
When a bot is created and added within the server, it is stored using a key, which the bot class itself can define, but defaults to the slack api token used to instantiate the bot.
|
141
|
+
|
142
|
+
Similarly, if a user disables their Slack integration, we should remove the bot. To remove a bot, call the `remove_bot` method on the remote using the key for the appropriate bot:
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
slack_remote.remove_bot('bot-key-which-is-normally-the-slack-api-token')
|
146
|
+
```
|
147
|
+
|
148
|
+
### Getting bots to talk
|
149
|
+
|
150
|
+
Up to this point, your bots could only respond to mentions and IM messages, but it's often useful to be able to externally trigger a bot into making an announcement.
|
151
|
+
|
152
|
+
We can tell a bot to send a message into its default room fairly simply using the remote:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
slack_remote.say('bot-key', text: 'I have an important announcement to make!')
|
156
|
+
```
|
26
157
|
|
27
158
|
## Development
|
28
159
|
|
29
|
-
After checking out the repo, run `
|
160
|
+
After checking out the repo, run `bundle` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec slack_bot_server` to use the gem in this directory, ignoring other installed copies of this gem.
|
30
161
|
|
31
162
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
163
|
|
33
164
|
## Contributing
|
34
165
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
166
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/exciting-io/slack_bot_server.
|
36
167
|
|
37
168
|
|
38
169
|
## License
|
data/lib/slack_bot_server/bot.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
require 'slack'
|
2
|
-
require 'securerandom'
|
3
2
|
|
4
3
|
class SlackBotServer::Bot
|
5
4
|
attr_reader :key
|
6
5
|
|
7
6
|
class InvalidToken < RuntimeError; end
|
8
7
|
|
9
|
-
def initialize(token:, key:
|
8
|
+
def initialize(token:, key: nil)
|
10
9
|
@token = token
|
11
|
-
@key = key
|
10
|
+
@key = key || @token
|
12
11
|
@api = ::Slack::Client.new(token: @token)
|
13
12
|
@im_channel_ids = []
|
14
13
|
|
@@ -5,7 +5,7 @@
|
|
5
5
|
# redis queue instance that points at the same redis server).
|
6
6
|
|
7
7
|
class SlackBotServer::RemoteControl
|
8
|
-
def initialize(queue)
|
8
|
+
def initialize(queue: queue)
|
9
9
|
@queue = queue
|
10
10
|
end
|
11
11
|
|
@@ -17,6 +17,10 @@ class SlackBotServer::RemoteControl
|
|
17
17
|
@queue.push([:remove_bot, key])
|
18
18
|
end
|
19
19
|
|
20
|
+
def say(key, message_data)
|
21
|
+
@queue.push([:say, key, message_data])
|
22
|
+
end
|
23
|
+
|
20
24
|
def call(key, method, args)
|
21
25
|
@queue.push([:call, [key, method, args]])
|
22
26
|
end
|
@@ -1,12 +1,19 @@
|
|
1
1
|
require 'slack_bot_server/bot'
|
2
2
|
|
3
3
|
class SlackBotServer::SimpleBot < SlackBotServer::Bot
|
4
|
+
# Set the username displayed in Slack
|
4
5
|
username 'SimpleBot'
|
5
6
|
|
7
|
+
# Respond to mentions in the connected chat room (defaults to #general).
|
8
|
+
# As well as the normal data provided by Slack's API, we add the `message`,
|
9
|
+
# which is the `text` parameter with the username stripped out. For example,
|
10
|
+
# When a user sends 'simple_bot: how are you?', the `message` data contains
|
11
|
+
# only 'how are you'.
|
6
12
|
on_mention do |data|
|
7
13
|
reply text: "You said '#{data['message']}', and I'm frankly fascinated."
|
8
14
|
end
|
9
15
|
|
16
|
+
# Respond to messages sent via IM communication directly with the bot.
|
10
17
|
on_im do
|
11
18
|
reply text: "Hmm, OK, let me get back to you about that."
|
12
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack-bot-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Adam
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: slack-api
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- ".gitignore"
|
106
106
|
- ".rspec"
|
107
107
|
- ".travis.yml"
|
108
|
+
- CONTRIBUTING.md
|
108
109
|
- Gemfile
|
109
110
|
- LICENSE.txt
|
110
111
|
- README.md
|