ruboty-slack_rtm 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rubocop.yml +5 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/ruboty/adapters/slack_rtm.rb +142 -0
- data/lib/ruboty/slack_rtm/version.rb +5 -0
- data/lib/ruboty/slack_rtm.rb +2 -0
- data/ruboty-slack_rtm.gemspec +28 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 25f75fe95738928f39dc29cba7350c4106f48a2b
|
4
|
+
data.tar.gz: 3a93bf1e383e987c5f6344a6d7d1d99150d46684
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5316c88ebcf6806ac32966fad7fb82fcf1363a236b2b5503926d65bc945fd9551338e2d316f681021736a2a49c6df26bebd7d51c4244f2f2f3c240a06443dc9d
|
7
|
+
data.tar.gz: e7d76945472ae660b2645075831b226a49c11584005e803e8e754c6e79e383b4d2629c2c16a598c0b9ad9b1f2518998a6d6cf85bd0c081a7b74fbf9705bb4425
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Sho Kusano
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Ruboty::SlackRTM
|
2
|
+
|
3
|
+
Slack(real time api) adapter for [ruboty](https://github.com/r7kamura/ruboty).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ruboty-slack_rtm'
|
11
|
+
```
|
12
|
+
|
13
|
+
## ENV
|
14
|
+
|
15
|
+
```
|
16
|
+
SLACK_TOKEN - Account's token. get one on https://api.slack.com/web#basics
|
17
|
+
```
|
18
|
+
|
19
|
+
Do not requires real user account. using with bot is recommended.
|
20
|
+
|
21
|
+
see: https://api.slack.com/bot-users
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/rosylilly/ruboty-slack_rtm/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'slack'
|
3
|
+
require 'slack-rtmapi'
|
4
|
+
require 'ruboty/adapters/base'
|
5
|
+
|
6
|
+
module Ruboty
|
7
|
+
module Adapters
|
8
|
+
class SlackRTM < Base
|
9
|
+
env :SLACK_TOKEN, "Account's token. get one on https://api.slack.com/web#basics"
|
10
|
+
|
11
|
+
def run
|
12
|
+
init
|
13
|
+
bind
|
14
|
+
connect
|
15
|
+
end
|
16
|
+
|
17
|
+
def say(message)
|
18
|
+
realtime.send(
|
19
|
+
type: 'message',
|
20
|
+
channel: message[:to],
|
21
|
+
text: message[:code] ? "```\n#{message[:body]}\n```" : message[:body],
|
22
|
+
mrkdwn: true
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def init
|
29
|
+
response = client.auth_test
|
30
|
+
@user_info_caches = {}
|
31
|
+
@channel_info_cahces = {}
|
32
|
+
|
33
|
+
ENV['RUBOTY_NAME'] ||= response['user']
|
34
|
+
end
|
35
|
+
|
36
|
+
def bind
|
37
|
+
realtime.on(:message) do |data|
|
38
|
+
method_name = "on_#{data['type']}".to_sym
|
39
|
+
send(method_name, data) if respond_to?(method_name, true)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def connect
|
44
|
+
Thread.start do
|
45
|
+
loop do
|
46
|
+
sleep 5
|
47
|
+
set_active
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
realtime.main_loop
|
52
|
+
end
|
53
|
+
|
54
|
+
def url
|
55
|
+
@url ||= ::SlackRTM.get_url(token: ENV['SLACK_TOKEN'])
|
56
|
+
end
|
57
|
+
|
58
|
+
def client
|
59
|
+
@client ||= ::Slack::Client.new(token: ENV['SLACK_TOKEN'])
|
60
|
+
end
|
61
|
+
|
62
|
+
def realtime
|
63
|
+
@realtime ||= ::SlackRTM::Client.new(websocket_url: url)
|
64
|
+
end
|
65
|
+
|
66
|
+
def set_active
|
67
|
+
client.users_setActive
|
68
|
+
end
|
69
|
+
|
70
|
+
# event handlers
|
71
|
+
|
72
|
+
def on_message(data)
|
73
|
+
data = resolve_mention!(data)
|
74
|
+
user = user_info(data['user'])
|
75
|
+
|
76
|
+
robot.receive(
|
77
|
+
body: data['text'],
|
78
|
+
from: data['channel'],
|
79
|
+
from_name: user['name'],
|
80
|
+
to: data['channel'],
|
81
|
+
channel: channel_info(data['channel']),
|
82
|
+
user: user,
|
83
|
+
time: Time.at(data['ts'].to_f)
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
def on_channel_change(data)
|
88
|
+
channel_id = data['channel']
|
89
|
+
channel_id = channel_id['id'] if channel_id.is_a?(Hash)
|
90
|
+
@channel_info_cahces[channel_id] = nil
|
91
|
+
end
|
92
|
+
alias_method :on_channel_deleted, :on_channel_change
|
93
|
+
alias_method :on_channel_renamed, :on_channel_change
|
94
|
+
alias_method :on_channel_archived, :on_channel_change
|
95
|
+
alias_method :on_channel_unarchived, :on_channel_change
|
96
|
+
|
97
|
+
def on_user_change(data)
|
98
|
+
user = data['user'] || data['bot']
|
99
|
+
@user_info_caches[user['id']] = user
|
100
|
+
end
|
101
|
+
alias_method :on_bot_added, :on_user_change
|
102
|
+
alias_method :on_bot_changed, :on_user_change
|
103
|
+
|
104
|
+
def resolve_mention!(data)
|
105
|
+
data = data.dup
|
106
|
+
|
107
|
+
data['mention_to'] = []
|
108
|
+
|
109
|
+
data['text'].gsub!(/\<\@(?<uid>[0-9A-Z]+)\>/) do |_|
|
110
|
+
user = user_info(Regexp.last_match[:uid])
|
111
|
+
|
112
|
+
data['mention_to'] << user
|
113
|
+
|
114
|
+
"@#{user['name']}"
|
115
|
+
end
|
116
|
+
|
117
|
+
data
|
118
|
+
end
|
119
|
+
|
120
|
+
def user_info(user_id)
|
121
|
+
@user_info_caches[user_id] ||= begin
|
122
|
+
resp = client.users_info(user: user_id)
|
123
|
+
|
124
|
+
resp['user']
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def channel_info(channel_id)
|
129
|
+
@channel_info_cahces[channel_id] ||= begin
|
130
|
+
resp = case channel_id
|
131
|
+
when /^C/
|
132
|
+
client.channels_info(channel: channel_id)
|
133
|
+
else
|
134
|
+
{}
|
135
|
+
end
|
136
|
+
|
137
|
+
resp['channel']
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ruboty/slack_rtm/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ruboty-slack_rtm'
|
8
|
+
spec.version = Ruboty::SlackRTM::VERSION
|
9
|
+
spec.authors = ['Sho Kusano']
|
10
|
+
spec.email = ['rosylilly@aduca.org']
|
11
|
+
spec.summary = 'Slack real time messaging adapter for Ruboty'
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = 'https://github.com/rosylilly/ruboty-slack_rtm'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rubocop', '>= 0.28.0'
|
24
|
+
|
25
|
+
spec.add_dependency 'ruboty', '>= 1.1.4'
|
26
|
+
spec.add_dependency 'slack-api', '~> 1.0.0'
|
27
|
+
spec.add_dependency 'slack-rtmapi', '~> 1.0.0.rc4'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruboty-slack_rtm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sho Kusano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.28.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.28.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ruboty
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.4
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.1.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: slack-api
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.0.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.0.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: slack-rtmapi
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.0.0.rc4
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.0.0.rc4
|
97
|
+
description: Slack real time messaging adapter for Ruboty
|
98
|
+
email:
|
99
|
+
- rosylilly@aduca.org
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rubocop.yml"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/ruboty/adapters/slack_rtm.rb
|
111
|
+
- lib/ruboty/slack_rtm.rb
|
112
|
+
- lib/ruboty/slack_rtm/version.rb
|
113
|
+
- ruboty-slack_rtm.gemspec
|
114
|
+
homepage: https://github.com/rosylilly/ruboty-slack_rtm
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.2.2
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Slack real time messaging adapter for Ruboty
|
138
|
+
test_files: []
|