|
4 | 4 | token: ENV['API_TOKEN'],
|
5 | 5 | room_id: ENV['ROOM_ID'],
|
6 | 6 | message: ENV['MESSAGE'].delete_prefix('"').delete_suffix('"'),
|
7 |
| - message_type: ENV['MESSAGE_TYPE'], |
8 |
| - user_ids_to_assign_task: ENV['USER_IDS_TO_ASSIGN_TASK'] |
9 | 7 | }
|
10 | 8 |
|
11 |
| -# message type must be message or task. |
12 |
| -type = params[:message_type] |
13 |
| -valid_types = ["message", "task"] |
14 |
| -unless valid_types.include?(type) |
15 |
| - raise StandardError.new("type should be message or task.") |
16 |
| -end |
17 |
| - |
18 |
| -if params[:message].empty? |
19 |
| - raise StandardError.new("empty message is not allowed.") |
20 |
| -end |
21 |
| - |
22 |
| -if type == "message" |
23 |
| - uri = URI.parse("https://api.chatwork.com/v2/rooms/#{params[:room_id]}/messages") |
24 |
| - http = Net::HTTP.new(uri.host, uri.port) |
25 |
| - http.use_ssl = true |
26 |
| - |
27 |
| - body = "body=#{params[:message]}" |
28 |
| - headers = { "X-ChatWorkToken" => "#{params[:token]}" } |
29 |
| - |
30 |
| - response = http.post(uri.path, body, headers) |
31 |
| - |
32 |
| - if response.code == '200' |
33 |
| - puts response.body |
34 |
| - else |
35 |
| - raise StandardError.new("action failed! #{response.body}") |
36 |
| - end |
37 |
| -end |
38 |
| - |
39 |
| -if type == "task" |
40 |
| - uri = URI.parse("https://api.chatwork.com/v2/rooms/#{params[:room_id]}/tasks") |
41 |
| - http = Net::HTTP.new(uri.host, uri.port) |
42 |
| - http.use_ssl = true |
| 9 | +uri = URI.parse("https://api.chatwork.com/v2/rooms/#{params[:room_id]}/messages") |
| 10 | +http = Net::HTTP.new(uri.host, uri.port) |
| 11 | +http.use_ssl = true |
43 | 12 |
|
44 |
| - body = "body=#{params[:message]}&to_ids=#{params[:user_ids_to_assign_task]}" |
45 |
| - headers = { "X-ChatWorkToken" => "#{params[:token]}" } |
| 13 | +body = "body=#{params[:message]}" |
| 14 | +headers = { 'X-ChatWorkToken' => params[:token].to_s } |
46 | 15 |
|
47 |
| - response = http.post(uri.path, body, headers) |
| 16 | +response = http.post(uri.path, body, headers) |
48 | 17 |
|
49 |
| - if response.code == '200' |
50 |
| - puts response.body |
51 |
| - else |
52 |
| - raise StandardError.new("action failed! #{response.body}") |
53 |
| - end |
| 18 | +if response.code == '200' |
| 19 | + puts response.body |
| 20 | +else |
| 21 | + raise StandardError, "action failed! #{response.body}" |
54 | 22 | end
|
0 commit comments