repp 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: 8d7c022e0eece174a0e7cd1e0e0d569307353982586fca0a71001d27425cad48
4
- data.tar.gz: 7e8aaeea3a3b61bfa29846c4ba248ef3c7e4f2f15a9de728d72e80bf84657f90
3
+ metadata.gz: 95bb3f7cd6e674913e4c9c9862570ad84360922e70e6b0c610292c3fa63346f4
4
+ data.tar.gz: 64aaf03c29c5a7d561aa0bfd4f49575ee32dd03a78e433651bb306d46a921ff7
5
5
  SHA512:
6
- metadata.gz: 3aee6a054338de1436ba5297f85a3c9f60770de504e64f38eb3f376bd898a244911479b5e6393c39dac4a820dc5650fd0ba55b496cba2ec816fbeaa3a71e15d5
7
- data.tar.gz: 39f65759bbdcc3d61c8cb70a427bdde0e1920d09e944505d952670dc936f1a1e7e836accd041a948f1051b0d0ff0758c5558960426eeae46759b0333111322a3
6
+ metadata.gz: 5ae22b70a81277b959555c5851ac0623a5e6f68a3ea3cc25d4ad9d1fa4f35357981fbb945e382968e7620e483fe50795857e4549b33dc6baa5cd6ee813ed3312
7
+ data.tar.gz: 712527ac39e00ff3dbcbdfa8ac66e557e8a768a86cdea43a41fa3b45394cd39acc68d61f3ffd6d2e732d5d08761d87936ae798521d8a714b4c85881a9199d3ad
@@ -3,26 +3,55 @@ module Repp
3
3
  class Slack
4
4
  require 'slack-ruby-client'
5
5
 
6
+ REPLY_REGEXP = /<@(\w+?)>/
7
+
6
8
  class SlackReceive < Event::Receive
7
- interface :channel, :user, :type, :ts
9
+ interface :channel, :user, :type, :ts, :reply_to
10
+
11
+ def bot?; !!@is_bot; end
12
+ def bot=(switch); @is_bot = switch; end
8
13
  end
9
14
 
10
- module SlackMessageHandler
11
- def self.handle(client, web_client, app)
15
+ class SlackMessageHandler
16
+ attr_reader :client, :web_client, :app
17
+ def initialize(client, web_client, app)
18
+ @client = client
19
+ @web_client = web_client
20
+ @app = app
21
+ end
22
+
23
+ def users(refresh = false)
24
+ @users = @web_client.list_users.members if refresh
25
+ @users ||= @web_client.users_list.members
26
+ end
27
+
28
+ def handle
12
29
  client.on :message do |data|
30
+ reply_to = data.text.scan(REPLY_REGEXP).map do |node|
31
+ user = users.find { |u| u.id == node.first }
32
+ user ? user.name : nil
33
+ end
34
+
13
35
  receive = SlackReceive.new(
14
36
  body: data.text,
15
37
  channel: data.channel,
16
38
  user: data.user,
17
39
  type: data.type,
18
- ts: data.ts
40
+ ts: data.ts,
41
+ reply_to: reply_to.compact
19
42
  )
43
+
44
+ user = users.find { |u| u.id == data.user } || users(true).find { |u| u.id == data.user }
45
+ receive.bot = (data['subtype'] == 'bot_message' || user.nil? || user['is_bot'])
46
+
20
47
  res = app.call(receive)
21
48
  if res.first
22
- channel_to_post = res.last&.[](:channel) || receive.channel
49
+ channel_to_post = res.last.nil? ? receive.channel : res.last[:channel]
23
50
  web_client.chat_postMessage(text: res.first, channel: channel_to_post, as_user: true)
24
51
  end
25
52
  end
53
+
54
+ client.start!
26
55
  end
27
56
  end
28
57
 
@@ -35,8 +64,8 @@ module Repp
35
64
  end
36
65
  @client = ::Slack::RealTime::Client.new
37
66
  @web_client = ::Slack::Web::Client.new
38
- SlackMessageHandler.handle(@client, @web_client, app.new)
39
- @client.start!
67
+ handler = SlackMessageHandler.new(@client, @web_client, app.new)
68
+ handler.handle
40
69
  end
41
70
 
42
71
  def stop!
data/lib/repp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Repp
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kinoppyd
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-29 00:00:00.000000000 Z
11
+ date: 2018-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine