ruboty-chatwork 0.0.2 → 0.0.3

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: 246286c18bf0f12d4711d3e535c5acb029fc92b5
4
- data.tar.gz: 673ff2c18228ebdc3f851cb826acad18936e5016
3
+ metadata.gz: 8a4d6f6a32e777888e2529adbd2ee2028508f03b
4
+ data.tar.gz: 8d96ddb59bc8b2df1ac4fbae33e6e72a07453a88
5
5
  SHA512:
6
- metadata.gz: 21b6d6daf6fd62aad61191f60eb7ea388091725954589cec28079aedcb7364b27980aa2d91defaf12dfd9141a8ef6772e81d25ed1df9c346dbf7631c0e06991e
7
- data.tar.gz: 9340100902a04a4c76ffce61eca1fadc57c87e93a6bfebecc3464ad8bf77ffe5ef0d374006cebde6e61476adc119ab912df2a036f94e6244477f2e8ace258ddd
6
+ metadata.gz: d1a78528b9f1dc5c0bd4c5d31bff60337e2ce11b65f03576cefe64891224cb9043fae21de29970b9a71afea24d7f471d5c1819efbe4114c9ffc75c4d21969851
7
+ data.tar.gz: 24099b27170a9a89e5a3e319f5e51d0a51a05b5e1bdb48c0ea1632ad8442a07579753505bfcf5c4d05512072691afcbece75176593a948e5b8670d8ec0d2e0c9
data/README.md CHANGED
@@ -16,8 +16,20 @@ gem 'ruboty-chatwork'
16
16
  CHATWORK_API_TOKEN - ChatWork API Token
17
17
  CHATWORK_ROOM - ChatWork Room ID
18
18
  CHATWORK_API_RATE - ChatWork API Rate(Requests per Hour)
19
+ CHATWORK_ROOM_FOR_SAYING - ChatWork Room ID for Saying(Optional)
19
20
  ```
20
21
 
22
+ ## HOW TO SET THE ROOM TO SAY MESSAGE
23
+ - Priority is high in numerical order (lower is ignored)
24
+ 1. `room_id:12345678` statement at last in message by saying
25
+ - ex. `ruboty Good Morning! room_id:12345678`
26
+ 2. `room_id` key in `reply` method
27
+ - ex. `message.reply("Hello!", room_id:12345678)`
28
+ 3. ENV["CHATWORK_ROOM_FOR_SAYING"]
29
+ 4. ENV["CHATWORK_ROOM"]
30
+ - Note
31
+ - regexp of extracting `room_id` is poor ;)
32
+
21
33
  ## Contributing
22
34
 
23
35
  1. Fork it ( http://github.com/mhag/ruboty-chatwork/fork )
@@ -18,9 +18,13 @@ module Ruboty
18
18
 
19
19
  def say(message)
20
20
  pp message
21
- req = Net::HTTP::Post.new(chatwork_messages_url.path, headers)
21
+
22
+ url_for_saying = chatwork_messages_url_for_saying(message)
23
+
24
+ req = Net::HTTP::Post.new(url_for_saying.path, headers)
22
25
  req.form_data = { 'body' => message[:body] }
23
- https = Net::HTTP.new(chatwork_messages_url.host, chatwork_messages_url.port)
26
+
27
+ https = Net::HTTP.new(url_for_saying.host, url_for_saying.port)
24
28
  https.use_ssl = true
25
29
  https.start {|https| https.request(req) }
26
30
  end
@@ -45,6 +49,14 @@ module Ruboty
45
49
  end
46
50
  memoize :chatwork_messages_url
47
51
 
52
+ def chatwork_room_for_saying(message)
53
+ message[:original][:room_id] || message[:room_id] || ENV["CHATWORK_ROOM_FOR_SAYING"] || ENV["CHATWORK_ROOM"]
54
+ end
55
+
56
+ def chatwork_messages_url_for_saying(message)
57
+ URI.join(chatwork_url, "/v2/rooms/#{chatwork_room_for_saying(message)}/messages")
58
+ end
59
+
48
60
  def chatwork_api_rate
49
61
  ENV["CHATWORK_API_RATE"].to_i
50
62
  end
@@ -62,19 +74,42 @@ module Ruboty
62
74
  req = Net::HTTP::Get.new(chatwork_messages_url.path, headers)
63
75
  https = Net::HTTP.new(chatwork_messages_url.host, chatwork_messages_url.port)
64
76
  https.use_ssl = true
77
+
65
78
  res = https.start {|https| https.request(req) }
66
79
  pp res.body
80
+
67
81
  unless res.body.nil?
68
- message = JSON.parse(res.body)
69
- robot.receive(
70
- body: message[0]['body'],
71
- from_name: message[0]['account']['name']
72
- )
82
+ message = JSON.parse(res.body)
83
+ message.each do | m |
84
+ original_body = m['body']
85
+
86
+ body = remove_room_id_statement(original_body)
87
+ from_name = m['account']['name']
88
+ room_id = extract_room_id_statement(original_body)
89
+
90
+ robot.receive(
91
+ body: body,
92
+ from_name: from_name,
93
+ room_id: room_id,
94
+ )
95
+ end
73
96
  end
74
97
 
75
98
  sleep (60 * 60) / chatwork_api_rate
76
99
  end
77
100
  end
101
+
102
+ def extract_room_id_statement(original_body)
103
+ original_body[/(.*)( room_id:([0-9]+))\z/, 3]
104
+ end
105
+
106
+ def remove_room_id_statement(original_body)
107
+ if !(original_body[/(.*)( room_id:([0-9]+)\z)/, 1].nil?)
108
+ return original_body[/(.*)( room_id:([0-9]+)\z)/, 1].rstrip
109
+ else
110
+ original_body
111
+ end
112
+ end
78
113
  end
79
114
  end
80
115
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Chatwork
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-chatwork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mhag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-02 00:00:00.000000000 Z
11
+ date: 2018-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty