slack-rtm-bot-helper 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5db6fe4f1bcffd16097cc3efd9ae4c3f476e2f01
4
- data.tar.gz: 5d8062090bd254d1befe5c10c4becbdca356ca3d
3
+ metadata.gz: 9b58e4b64f1453466b18c928a2b05ec29c87505e
4
+ data.tar.gz: a81c51c70b783ba18847e334cb6a818ed7207060
5
5
  SHA512:
6
- metadata.gz: 96fd89d7df065454df2a1831e6495363dba20f2abb43d9f23285f68beb2860e52b5339e328d286a27e91875c1f9401ba788adf5bb94130d3c9c52ffd6dc7da11
7
- data.tar.gz: 3092b411bf904edba57174093c9ffa1f50352cf690eab297673b242761ad5af53907852a24a569226468f039e3338968778b76579e5726265b8655a7d3bb5628
6
+ metadata.gz: 725d1aabb7671925b3f975cf77ced749b9e623a9d579802f99b8cf05deb7c359f29cd7d33d149fb25e069cd5b26cb202f20e0931204e68fc2125e18ebe4a83b4
7
+ data.tar.gz: 9cdcdbad412fbf9725281abd38de40a4392748a408ae05392181f3b915d5b6124706022172159016a1c1147eb692c996ca428395816bcd9016889d2f8da9a5ce
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Slack::Rtm::Bot::Helper
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/slack/rtm/bot/helper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- slack-rtm-bot-helper supports development slack real time messaging bot.
3
+ This gem help develop slack bot using Real Time Message API.
6
4
 
7
5
  ## Installation
8
6
 
@@ -25,26 +23,14 @@ Or install it yourself as:
25
23
  ```rb
26
24
  require 'slack-rtm-bot-helper'
27
25
 
28
- # channelはなかったら送信してきたchannelに返す。
29
- Slack::Rtm::Bot::Helper.run(token='', channel='') do
30
- # messageを作るロジック
26
+ Slack::Rtm::Bot::Helper.run(token: '<your slack token>', channel: '<channel name>', name: '<bot name>') do
27
+ # input your logic that create reply message.
28
+ # return string
31
29
  end
32
30
  ```
33
-
34
- ## Development
35
-
36
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
-
38
- 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).
39
-
40
- ## Contributing
41
-
42
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/slack-rtm-bot-helper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
31
+ - token and block is required.
32
+ - if set channel , send message only specified channel.
33
+ - if set name, only reaction message starts `@botname`.
43
34
 
44
35
  ## License
45
-
46
36
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
-
48
- ## Code of Conduct
49
-
50
- Everyone interacting in the Slack::Rtm::Bot::Helper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/slack-rtm-bot-helper/blob/master/CODE_OF_CONDUCT.md).
@@ -3,28 +3,30 @@ module Slack
3
3
  module Bot
4
4
  class Helper
5
5
  class NotEnoughArgumentsError < StandardError; end
6
- SLACK_URL = 'https://slack.com'
7
- RTM_PATH = '/api/rtm.start'
6
+ SLACK_URL = 'https://slack.com'
7
+ RTM_PATH = '/api/rtm.start'
8
+ USERS_PATH = '/api/users.list'
8
9
 
9
- attr_accessor :channel, :message_block, :id, :wss
10
+ attr_accessor :channel, :message_block, :id, :me, :wss
10
11
 
11
- def initialize(token, channel, message_block)
12
+ def initialize(token, channel, name, message_block)
12
13
  @token = token
13
14
  @channel = channel
14
15
  @message_block = message_block
16
+ @me = me_id(token, name) unless name.nil?
15
17
 
16
18
  @id = 1
17
19
  @wss = nil
18
20
  end
19
21
 
20
- def self.run(token=nil, channel=nil, &message_block)
22
+ def self.run(token:nil, channel:nil, name: nil, &message_block)
21
23
  raise NotEnoughArgumentsError if token.nil? || message_block.nil?
22
24
 
23
- s = new(token, channel, message_block)
25
+ s = new(token, channel, name, message_block)
24
26
  s.connect
25
27
 
26
28
  loop do
27
- sleep 5
29
+ sleep 2
28
30
  unless s.wss.open?
29
31
  s.connect
30
32
  end
@@ -46,27 +48,48 @@ module Slack
46
48
  @wss.on :message, &(base_block(self))
47
49
  end
48
50
 
51
+ def to_me?(text)
52
+ text.match?(/^<@#{@me}/)
53
+ end
54
+
49
55
  private
50
- def base_block(helper)
51
- Proc.new do |msg|
52
- data = ::JSON.parse(msg.data)
53
- close if data.empty?
54
-
55
- msg = helper.message_block.call(data) if !data['text'].empty?
56
-
57
- if !msg.nil? && !msg.empty?
58
- target_ch = helper.channel || data['channel']
59
- msg_json = {
60
- id: helper.id,
61
- type: 'message',
62
- channel: target_ch,
63
- text: msg
64
- }.to_json
65
-
66
- send(msg_json) && helper.id += 1
67
- end
56
+
57
+ def me_id(token, name)
58
+ conn = ::Faraday.new(SLACK_URL) do |faraday|
59
+ faraday.request :url_encoded
60
+ faraday.adapter ::Faraday.default_adapter
61
+ end
62
+
63
+ res = conn.post USERS_PATH, token: @token
64
+ (::JSON.parse(res.body)['members'].select { |m| m['name'] == name }.first)&.fetch('id')
65
+ end
66
+
67
+ def base_block(helper)
68
+ Proc.new do |msg|
69
+ data = ::JSON.parse(msg.data)
70
+ close if data.empty?
71
+
72
+ msg = if !data['text'].empty?
73
+ if helper.me.nil?
74
+ helper.message_block.call(data)
75
+ else
76
+ helper.message_block.call(data) if helper.to_me?(data['text'])
77
+ end
78
+ end
79
+
80
+ if !(msg.nil? || msg.empty?)
81
+ target_ch = helper.channel || data['channel']
82
+ msg_json = {
83
+ id: helper.id,
84
+ type: 'message',
85
+ channel: target_ch,
86
+ text: msg.to_s
87
+ }.to_json
88
+
89
+ send(msg_json) && helper.id += 1
68
90
  end
69
91
  end
92
+ end
70
93
  end
71
94
  end
72
95
  end
@@ -2,7 +2,7 @@ module Slack
2
2
  module Rtm
3
3
  module Bot
4
4
  class Helper
5
- VERSION = "0.1.3"
5
+ VERSION = "1.0.0"
6
6
  end
7
7
  end
8
8
  end
@@ -35,4 +35,6 @@ Gem::Specification.new do |spec|
35
35
  spec.add_development_dependency "bundler", "~> 1.15"
36
36
  spec.add_development_dependency "rake", "~> 10.0"
37
37
  spec.add_development_dependency "rspec", "~> 3.0"
38
+
39
+ spec.add_development_dependency "pry"
38
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-rtm-bot-helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - littlekbt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-24 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - kr.kubota.11@gmail.com
@@ -89,7 +103,6 @@ extra_rdoc_files: []
89
103
  files:
90
104
  - ".gitignore"
91
105
  - ".rspec"
92
- - CODE_OF_CONDUCT.md
93
106
  - Gemfile
94
107
  - LICENSE.txt
95
108
  - README.md
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at kr.kubota.11@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/