slack-bot-server 0.2.0 → 0.3.0
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/CHANGELOG.md +7 -0
- data/README.md +16 -7
- data/lib/slack_bot_server/remote_control.rb +2 -2
- data/lib/slack_bot_server/server.rb +20 -19
- data/lib/slack_bot_server/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b273bf7f49d2acf1743f6ef74ac8e4ca3fc336d
|
4
|
+
data.tar.gz: a4b089d850c7a0cceef7fe9d1643ce4843831b5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 324c943e0ee722c366a08f5fb14e168b3f4145baf951c127bb9f82a5797236410552edbb6bb342da66b2abb41d79692066cab53ae86c4db5cb1335bbc1cfc06f
|
7
|
+
data.tar.gz: 1bfb87e6b026251971e0dd9a35b2601c37845c075910df809112a0c443bdeeafcb4be4cb4c9d5bc0f83daaeafe9defbeef8f6e89e9eea088d3a88396b1be5b36
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
## 0.3.0
|
2
|
+
|
3
|
+
Changes:
|
4
|
+
|
5
|
+
- The `SlackBotServer::Server#on_new_proc` has been renamed to `Server#on_add`
|
6
|
+
- The `add` and `add_bot` methods on `SlackBotServer::Server` and `SlackBotServer::RemoteControl` control have been merged as `add_bot`
|
7
|
+
- Multiple arguments may be passed via the `add_bot` method to the block given to `SlackBotServer::on_add`
|
data/README.md
CHANGED
@@ -42,7 +42,7 @@ server = SlackBotServer::Server.new(queue: queue)
|
|
42
42
|
|
43
43
|
# How your application-specific should be created when the server
|
44
44
|
# is told about a new slack api token to connect with
|
45
|
-
server.
|
45
|
+
server.on_add do |token|
|
46
46
|
# Return a new bot instance to the server. `SimpleBot` is a provided
|
47
47
|
# example bot with some very simple behaviour.
|
48
48
|
SlackBotServer::SimpleBot.new(token: token)
|
@@ -100,10 +100,17 @@ queue = SlackBotServer::RedisQueue.new(Resque.redis)
|
|
100
100
|
|
101
101
|
server = SlackBotServer::Server.new(queue: queue)
|
102
102
|
|
103
|
-
|
103
|
+
# The `on_add` block can take any number arguments - basically whatever
|
104
|
+
# is passed to the `add_bot` method (see below). Since the bot will almost
|
105
|
+
# certainly need to use a Slack API token to actually connect to Slack,
|
106
|
+
# this should either be one of the arguments, or be retrievable using one
|
107
|
+
# of the arguments.
|
108
|
+
# It should return a bot (something that responds to `start`); if anything
|
109
|
+
# else is returned, it will be ignored.
|
110
|
+
server.on_add do |token, team_id|
|
104
111
|
# Our bots need to know some data about the team they are connecting
|
105
112
|
# to, like specifics of their account and their tasks
|
106
|
-
team_data = Harmonia.
|
113
|
+
team_data = Harmonia.find_team_data(team_id)
|
107
114
|
|
108
115
|
# Our bot instance stores that data in an instance variable internally
|
109
116
|
# and then refers to it when it receives messages
|
@@ -112,8 +119,10 @@ end
|
|
112
119
|
|
113
120
|
# When the server starts we need to find all the teams which have already
|
114
121
|
# set up integrations and ensure their bots are launched immediately
|
115
|
-
Harmonia.
|
116
|
-
|
122
|
+
Harmonia.teams.each do |team|
|
123
|
+
# Any arguments can be passed to the `add_bot` method; they are passed
|
124
|
+
# on to the proc supplied to `on_add` for the server.
|
125
|
+
server.add_bot(team.slack_token, team.id)
|
117
126
|
end
|
118
127
|
|
119
128
|
# Actually start the server. The pre-loaded bots will connect immediately,
|
@@ -132,10 +141,10 @@ control to add the token to the server.
|
|
132
141
|
# Somewhere within your application
|
133
142
|
queue = SlackBotServer::RedisQueue.new(Redis.new)
|
134
143
|
slack_remote = SlackBotServer::RemoteControl.new(queue: queue)
|
135
|
-
slack_remote.
|
144
|
+
slack_remote.add_bot('user-accounts-slack-api-token')
|
136
145
|
```
|
137
146
|
|
138
|
-
This will queue
|
147
|
+
This will queue a bot be added by the server, using the `on_add` block provided in the server script.
|
139
148
|
|
140
149
|
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
150
|
|
@@ -8,12 +8,16 @@ class SlackBotServer::Server
|
|
8
8
|
def initialize(queue: SlackBotServer::LocalQueue.new)
|
9
9
|
@queue = queue
|
10
10
|
@bots = {}
|
11
|
-
@
|
11
|
+
@add_proc = -> (token) { SlackBotServer::SimpleBot.new(token: token) }
|
12
12
|
@running = false
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
# Define the block which should be called when the `add_bot` method is
|
16
|
+
# called, or the `add_bot` message is sent via a queue. This block
|
17
|
+
# should return a bot (which responds to start), in which case it will
|
18
|
+
# be added and started. If anything else is returned, it will be ignored.
|
19
|
+
def on_add(&block)
|
20
|
+
@add_proc = block
|
17
21
|
end
|
18
22
|
|
19
23
|
def start
|
@@ -43,15 +47,13 @@ class SlackBotServer::Server
|
|
43
47
|
@bots[key.to_sym]
|
44
48
|
end
|
45
49
|
|
46
|
-
def add_bot(
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
bot = @new_token_proc.call(token)
|
54
|
-
add_bot(bot) if bot
|
50
|
+
def add_bot(*args)
|
51
|
+
bot = @add_proc.call(*args)
|
52
|
+
if bot.respond_to?(:start)
|
53
|
+
log "adding bot #{bot}"
|
54
|
+
@bots[bot.key.to_sym] = bot
|
55
|
+
bot.start if @running
|
56
|
+
end
|
55
57
|
rescue => e
|
56
58
|
log_error(e)
|
57
59
|
end
|
@@ -70,26 +72,25 @@ class SlackBotServer::Server
|
|
70
72
|
def process_instruction(instruction)
|
71
73
|
type, *args = instruction
|
72
74
|
case type.to_sym
|
73
|
-
when :
|
74
|
-
|
75
|
-
|
76
|
-
add_token(token)
|
75
|
+
when :add_bot
|
76
|
+
log "adding bot: #{args.inspect}"
|
77
|
+
add_bot(*args)
|
77
78
|
when :remove_bot
|
78
79
|
key = args.first
|
79
80
|
remove_bot(key)
|
80
81
|
when :broadcast
|
81
82
|
key, message_data = args
|
82
|
-
log "[#{key}] #{message_data}"
|
83
|
+
log "[#{key}] broadcast: #{message_data}"
|
83
84
|
bot = bot(key)
|
84
85
|
bot.broadcast(message_data)
|
85
86
|
when :say
|
86
87
|
key, message_data = args
|
87
|
-
log "[#{key}] #{message_data}"
|
88
|
+
log "[#{key}] say: #{message_data}"
|
88
89
|
bot = bot(key)
|
89
90
|
bot.say(message_data)
|
90
91
|
when :say_to
|
91
92
|
key, user_id, message_data = args
|
92
|
-
log "[#{key}] (#{user_id}) #{message_data}"
|
93
|
+
log "[#{key}] say_to: (#{user_id}) #{message_data}"
|
93
94
|
bot = bot(key)
|
94
95
|
bot.say_to(user_id, message_data)
|
95
96
|
when :call
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Adam
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: slack-api
|
@@ -119,6 +119,7 @@ files:
|
|
119
119
|
- ".gitignore"
|
120
120
|
- ".rspec"
|
121
121
|
- ".travis.yml"
|
122
|
+
- CHANGELOG.md
|
122
123
|
- CONTRIBUTING.md
|
123
124
|
- Gemfile
|
124
125
|
- LICENSE.txt
|
@@ -153,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
154
|
version: '0'
|
154
155
|
requirements: []
|
155
156
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.
|
157
|
+
rubygems_version: 2.5.1
|
157
158
|
signing_key:
|
158
159
|
specification_version: 4
|
159
160
|
summary: A server for hosting slack bots.
|