embulk-input-slack_message 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.ruby-version +1 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +74 -0
- data/Rakefile +3 -0
- data/embulk-input-slack_message.gemspec +22 -0
- data/lib/embulk/input/slack_message.rb +93 -0
- data/lib/slack.rb +22 -0
- data/lib/slack/client.rb +73 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1f24a85713970b8586eaccf14732e5462b4618be
|
4
|
+
data.tar.gz: 19fc15c2631d750c37c488a63c57bcb3d60803cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 593e59cc7e4d1a424d8c09c108300f4fa5d50973b45fa053e51506366217e8c359d9a8b4dda3f67bae3c92b810f355d634e22803cd7bfd37a5874cc04388bc36
|
7
|
+
data.tar.gz: 97d36c2cc8b58ba63ccb611139dd9f4cf847ac227d1285fa0df9178c321bff29d54451c3ae35600ddb4edc82fa71d07556ca45c1704ee1f72dba9325a61e0212
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
jruby-9.0.4.0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
MIT License
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Slack Message input plugin for Embulk
|
2
|
+
|
3
|
+
This plugin is embulk input from slack message.
|
4
|
+
Inspire of the [embulk-input-slack_history](https://github.com/yaggytter/embulk-input-slack-history/blob/master/lib/embulk/input/slack_history.rb)
|
5
|
+
|
6
|
+
## Overview
|
7
|
+
|
8
|
+
* **Plugin type**: input
|
9
|
+
* **Resume supported**: no
|
10
|
+
* **Cleanup supported**: no
|
11
|
+
* **Guess supported**: yes
|
12
|
+
|
13
|
+
## Advanced
|
14
|
+
|
15
|
+
### Complete use name
|
16
|
+
|
17
|
+
User name be id come from slack in message.
|
18
|
+
|
19
|
+
This plugin complete user name.
|
20
|
+
|
21
|
+
### Guess supported
|
22
|
+
|
23
|
+
This plugin guess supported.
|
24
|
+
|
25
|
+
Calculate between time from latest and oldest.
|
26
|
+
|
27
|
+
## Configuration
|
28
|
+
|
29
|
+
channel settings is references slack description ([here](https://api.slack.com/methods/channels.history))
|
30
|
+
|
31
|
+
- **channel**: channel settings (hash, required)
|
32
|
+
- name: channel name (string, required)
|
33
|
+
- type: channel type (string, required. this param is originated)
|
34
|
+
- channel: public channel
|
35
|
+
- group: private group
|
36
|
+
- direct: direct message
|
37
|
+
- multi_direct: multi direct message
|
38
|
+
- latest: End of time range of messages to include in results. (string, default: now date)
|
39
|
+
- oldest: Start of time range of messages to include in results. (string, default: 0)
|
40
|
+
- count: Number of messages to return, between 1 and 1000 (long, default: 100)
|
41
|
+
- inclusive: Include messages with latest or oldest timestamp in results (long, default: 0)
|
42
|
+
- unreads: Include `unread_count_display` in the output? (long, default: 0)
|
43
|
+
- **token**: Slack API Token. Generate from [Slack Authentication](https://api.slack.com/web) (string, required)
|
44
|
+
- **repeat**: Adjustment of time when next config generation (mill seconds). (long, default: 0)
|
45
|
+
|
46
|
+
## Example
|
47
|
+
|
48
|
+
```yaml
|
49
|
+
in:
|
50
|
+
type: slack_message
|
51
|
+
channel:
|
52
|
+
name: general
|
53
|
+
type: channel
|
54
|
+
latest: '2016-02-16 00:00:00'
|
55
|
+
oldest: '2016-02-15 00:00:00'
|
56
|
+
count: 100
|
57
|
+
inclusive: 0
|
58
|
+
unreads: 0
|
59
|
+
token: 'aaaa-1111111111-2222222222-33333333333-bbbbbbbbbb'
|
60
|
+
repeat: 0
|
61
|
+
columns:
|
62
|
+
- {index: 0, name: datetime, type: timestamp, format: null}
|
63
|
+
- {index: 1, name: channel_id, type: string, format: null}
|
64
|
+
- {index: 2, name: channel_name, type: string, format: null}
|
65
|
+
- {index: 3, name: user_id, type: string, format: null}
|
66
|
+
- {index: 4, name: user_name, type: string, format: null}
|
67
|
+
- {index: 5, name: message, type: string, format: null}
|
68
|
+
```
|
69
|
+
|
70
|
+
## Build
|
71
|
+
|
72
|
+
```
|
73
|
+
$ rake
|
74
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = "embulk-input-slack_message"
|
4
|
+
spec.version = "0.1.0"
|
5
|
+
spec.authors = ["yuemori"]
|
6
|
+
spec.summary = "Slack Message input plugin for Embulk"
|
7
|
+
spec.description = "Loads records from Slack Message."
|
8
|
+
spec.email = ["yuemori@aiming-inc.com"]
|
9
|
+
spec.licenses = ["MIT"]
|
10
|
+
spec.homepage = "https://github.com/yuemori/embulk-input-slack_message"
|
11
|
+
|
12
|
+
spec.files = `git ls-files`.split("\n") + Dir["classpath/*.jar"]
|
13
|
+
spec.test_files = spec.files.grep(%r{^(test|spec)/})
|
14
|
+
spec.require_paths = ["lib"]
|
15
|
+
|
16
|
+
spec.add_dependency 'rest-client'
|
17
|
+
spec.add_dependency 'activesupport'
|
18
|
+
spec.add_development_dependency 'embulk', ['>= 0.8.3']
|
19
|
+
spec.add_development_dependency 'bundler', ['>= 1.10.6']
|
20
|
+
spec.add_development_dependency 'rake', ['>= 10.0']
|
21
|
+
spec.add_development_dependency 'pry'
|
22
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'slack'
|
2
|
+
|
3
|
+
module Embulk
|
4
|
+
module Input
|
5
|
+
class SlackMessage < InputPlugin
|
6
|
+
Plugin.register_input("slack_message", self)
|
7
|
+
|
8
|
+
def self.transaction(config, &control)
|
9
|
+
task = {
|
10
|
+
'channel' => config.param('channel', :hash),
|
11
|
+
'token' => config.param('token', :string),
|
12
|
+
'repeat_at' => config.param('repeat_at', :long, default: 0)
|
13
|
+
}
|
14
|
+
|
15
|
+
yield(task, columns, 1)
|
16
|
+
|
17
|
+
channel = task['channel']
|
18
|
+
repeat_at = task['repeat_at']
|
19
|
+
latest = channel['latest'] ? Time.parse(channel['latest']).to_f : Time.now.to_f
|
20
|
+
oldest = channel['oldest'] ? Time.parse(channel['oldest']).to_f : 0
|
21
|
+
|
22
|
+
if repeat_at.zero?
|
23
|
+
time_diff = latest.to_i - oldest.to_i
|
24
|
+
channel = { oldest: Time.at(latest), latest: Time.at(latest + time_diff) }
|
25
|
+
else
|
26
|
+
channel = { oldest: oldest + repeat_at, latest: latest + repeat_at }
|
27
|
+
end
|
28
|
+
|
29
|
+
{ channel: channel }
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.columns
|
33
|
+
[
|
34
|
+
Column.new(0, "datetime", :timestamp),
|
35
|
+
Column.new(1, "channel_id", :string),
|
36
|
+
Column.new(2, "channel_name", :string),
|
37
|
+
Column.new(3, "user_id", :string),
|
38
|
+
Column.new(4, "user_name", :string),
|
39
|
+
Column.new(5, "message", :string)
|
40
|
+
]
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.guess(config)
|
44
|
+
channels = [
|
45
|
+
{
|
46
|
+
name: 'general',
|
47
|
+
type: 'channel',
|
48
|
+
latest: Time.now.strftime('%F %T'),
|
49
|
+
oldest: 0,
|
50
|
+
count: 100,
|
51
|
+
inclusive: 0,
|
52
|
+
unreads: 0
|
53
|
+
}
|
54
|
+
]
|
55
|
+
{ channel: channels.first, token: 'SLACK_API_TOKEN', repeat: 0, columns: Embulk::Schema.new(self.columns) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def init
|
59
|
+
@channel = task["channel"]
|
60
|
+
|
61
|
+
token = task['token']
|
62
|
+
raise StandardError.new, 'slack token is not found' unless token
|
63
|
+
|
64
|
+
Slack.token = token
|
65
|
+
end
|
66
|
+
|
67
|
+
def run
|
68
|
+
latest = @channel['latest'] ? Time.parse(@channel['latest']).to_f : Time.now.to_f
|
69
|
+
oldest = @channel['oldest'] ? Time.parse(@channel['oldest']).to_f : 0
|
70
|
+
inclusive = @channel['inclusive'] || 0
|
71
|
+
count = @channel['count'] || 100
|
72
|
+
unreads = @channel['unreads'] || 0
|
73
|
+
|
74
|
+
options = {
|
75
|
+
latest: latest,
|
76
|
+
oldest: oldest,
|
77
|
+
inclusive: inclusive,
|
78
|
+
count: count,
|
79
|
+
unreads: unreads
|
80
|
+
}
|
81
|
+
|
82
|
+
Slack.messages(@channel['name'], @channel['type'], options).each do |message|
|
83
|
+
page_builder.add message
|
84
|
+
end
|
85
|
+
|
86
|
+
page_builder.finish
|
87
|
+
|
88
|
+
task_report = {}
|
89
|
+
return task_report
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/slack.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'rest-client'
|
4
|
+
require 'json'
|
5
|
+
require 'slack/client'
|
6
|
+
|
7
|
+
module Slack
|
8
|
+
class << self
|
9
|
+
attr_accessor :token
|
10
|
+
delegate :channels, :messages, :users, :options=, to: :client
|
11
|
+
|
12
|
+
def configure(&block)
|
13
|
+
yield configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def client
|
19
|
+
@client = Slack::Client.new(token)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/slack/client.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
module Slack
|
2
|
+
class Client
|
3
|
+
attr_reader :options, :token
|
4
|
+
|
5
|
+
def initialize(token)
|
6
|
+
@token = token
|
7
|
+
end
|
8
|
+
|
9
|
+
def messages(name, type, options)
|
10
|
+
channel = case type
|
11
|
+
when 'channel' then channels.find { |item| item['name'] == name }
|
12
|
+
when 'group' then groups.find { |item| item['name'] == name }
|
13
|
+
when 'direct' then im.find { |item| item['user'] == name }
|
14
|
+
when 'multi_direct' then mpim.find { |item| item['name'] == name }
|
15
|
+
end
|
16
|
+
messages = JSON.parse(get('channels.history', options.merge(channel: channel['id'])))['messages']
|
17
|
+
messages.select { |message| message['type'] == 'message' && message['subtype'].nil? }.map do |message|
|
18
|
+
[
|
19
|
+
Time.at(message['ts'].to_i),
|
20
|
+
channel['id'],
|
21
|
+
channel['name'],
|
22
|
+
message['user'],
|
23
|
+
user_by_id(message['user'])['name'],
|
24
|
+
filter_message(message['text'])
|
25
|
+
]
|
26
|
+
end.reverse
|
27
|
+
end
|
28
|
+
|
29
|
+
def filter_message(text)
|
30
|
+
text = text.dup
|
31
|
+
scan = text.scan(/\<@[0-9A-Z]+\>/)
|
32
|
+
return text unless scan
|
33
|
+
|
34
|
+
scan.each do |pattern|
|
35
|
+
user_id = pattern.match(/<@(?<user_id>.*)>/)[:user_id]
|
36
|
+
text.gsub!(/#{pattern}/, "@#{user_by_id(user_id)['name']}")
|
37
|
+
end
|
38
|
+
text
|
39
|
+
end
|
40
|
+
|
41
|
+
def user_by_id(id)
|
42
|
+
users.find { |user| user['id'] == id }
|
43
|
+
end
|
44
|
+
|
45
|
+
def user_by_name(name)
|
46
|
+
users.find { |user| user['name'] == name }
|
47
|
+
end
|
48
|
+
|
49
|
+
def users
|
50
|
+
@users ||= JSON.parse(get('users.list', { presence: 0 }))['members']
|
51
|
+
end
|
52
|
+
|
53
|
+
def channels
|
54
|
+
@channels ||= JSON.parse(get('channels.list', { exclude_archived: 1 }))['channels']
|
55
|
+
end
|
56
|
+
|
57
|
+
def groups
|
58
|
+
@groups ||= JSON.parse(get('groups.list', { exclude_archived: 1 }))['groups']
|
59
|
+
end
|
60
|
+
|
61
|
+
def ims
|
62
|
+
@ims ||= JSON.parse(get('im.list'))['ims']
|
63
|
+
end
|
64
|
+
|
65
|
+
def mpim
|
66
|
+
@mpims ||= JSON.parse(get('mpim.list'))['groups']
|
67
|
+
end
|
68
|
+
|
69
|
+
def get(api, options = {})
|
70
|
+
RestClient.get("https://slack.com/api/#{api}", { params: options.merge(token: token) })
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: embulk-input-slack_message
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yuemori
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '0'
|
25
|
+
prerelease: false
|
26
|
+
type: :runtime
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
prerelease: false
|
40
|
+
type: :runtime
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: embulk
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 0.8.3
|
53
|
+
prerelease: false
|
54
|
+
type: :development
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.10.6
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 1.10.6
|
67
|
+
prerelease: false
|
68
|
+
type: :development
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '10.0'
|
81
|
+
prerelease: false
|
82
|
+
type: :development
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirement: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
prerelease: false
|
96
|
+
type: :development
|
97
|
+
description: Loads records from Slack Message.
|
98
|
+
email:
|
99
|
+
- yuemori@aiming-inc.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".ruby-version"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- embulk-input-slack_message.gemspec
|
111
|
+
- lib/embulk/input/slack_message.rb
|
112
|
+
- lib/slack.rb
|
113
|
+
- lib/slack/client.rb
|
114
|
+
homepage: https://github.com/yuemori/embulk-input-slack_message
|
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.4.8
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Slack Message input plugin for Embulk
|
138
|
+
test_files: []
|