Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] add the feature for setting the room to say message #2

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ gem 'ruboty-chatwork'
CHATWORK_API_TOKEN - ChatWork API Token
CHATWORK_ROOM - ChatWork Room ID
CHATWORK_API_RATE - ChatWork API Rate(Requests per Hour)
CHATWORK_ROOM_FOR_SAYING - ChatWork Room ID for Saying(Optional)
```

## HOW TO SET THE ROOM TO SAY MESSAGE
- Priority is high in numerical order (lower is ignored)
1. `room_id:12345678` statement at last in message by saying
- ex. `ruboty Good Morning! room_id:12345678`
2. `room_id` key in `reply` method
- ex. `message.reply("Hello!", room_id:12345678)`
3. ENV["CHATWORK_ROOM_FOR_SAYING"]
4. ENV["CHATWORK_ROOM"]
- Note
- regexp of extracting `room_id` is poor ;)

## Contributing

1. Fork it ( http://github.com/mhag/ruboty-chatwork/fork )
Expand Down
43 changes: 38 additions & 5 deletions lib/ruboty/adapters/chatwork.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ def run

def say(message)
pp message
req = Net::HTTP::Post.new(chatwork_messages_url.path, headers)

url_for_saying = chatwork_messages_url_for_saying(message)

req = Net::HTTP::Post.new(url_for_saying.path, headers)
req.form_data = { 'body' => message[:body] }
https = Net::HTTP.new(chatwork_messages_url.host, chatwork_messages_url.port)

https = Net::HTTP.new(url_for_saying.host, url_for_saying.port)
https.use_ssl = true
https.start {|https| https.request(req) }
end
Expand All @@ -45,6 +49,14 @@ def chatwork_messages_url
end
memoize :chatwork_messages_url

def chatwork_room_for_saying(message)
message[:original][:room_id] || message[:room_id] || ENV["CHATWORK_ROOM_FOR_SAYING"] || ENV["CHATWORK_ROOM"]
end

def chatwork_messages_url_for_saying(message)
URI.join(chatwork_url, "/v2/rooms/#{chatwork_room_for_saying(message)}/messages")
end

def chatwork_api_rate
ENV["CHATWORK_API_RATE"].to_i
end
Expand All @@ -62,19 +74,40 @@ def listen
req = Net::HTTP::Get.new(chatwork_messages_url.path, headers)
https = Net::HTTP.new(chatwork_messages_url.host, chatwork_messages_url.port)
https.use_ssl = true

res = https.start {|https| https.request(req) }
pp res.body

unless res.body.nil?
message = JSON.parse(res.body)
message = JSON.parse(res.body)
original_body = message[0]['body']

body = remove_room_id_statement(original_body)
from_name = message[0]['account']['name']
room_id = extract_room_id_statement(original_body)

robot.receive(
body: message[0]['body'],
from_name: message[0]['account']['name']
body: body,
from_name: from_name,
room_id: room_id,
)
end

sleep (60 * 60) / chatwork_api_rate
end
end

def extract_room_id_statement(original_body)
original_body[/(.*)( room_id:([0-9]+))\z/, 3]
end

def remove_room_id_statement(original_body)
if !(original_body[/(.*)( room_id:([0-9]+)\z)/, 1].nil?)
return original_body[/(.*)( room_id:([0-9]+)\z)/, 1].rstrip
else
original_body
end
end
end
end
end