hangouts-chat 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +32 -0
- data/LICENSE +21 -0
- data/README.md +86 -0
- data/Rakefile +9 -0
- data/lib/hangouts_chat.rb +40 -2
- data/lib/hangouts_chat/exceptions.rb +16 -0
- data/lib/hangouts_chat/http.rb +10 -2
- data/lib/hangouts_chat/version.rb +2 -1
- data/test/{hangouts-chat → hangouts_chat}/http_test.rb +0 -0
- data/test/hangouts_chat_test.rb +51 -0
- data/test/test_helper.rb +1 -1
- metadata +61 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69ed8b47b674154e8659e4089f70fe8f74bc7b6721a4cd6d9b907c487ad014b0
|
4
|
+
data.tar.gz: d3f323a0c4d91517cbfc40c643038d580c251925c146007a1befe1759809494c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c4f191fa52600b96c73850515a2dc3f1c95839ec3d528ec46123845ab87f62ac235da1f77348ddbe2697c314dacd7ccf94916065008d9aa8dab80db58d58647
|
7
|
+
data.tar.gz: b0ea2b1d2ebaf6c1a1d6e4b3614b46a4a15bafff09033a7f82dbfdebf29b7efe5298188a43df17f40733a26f4d4f570fb92ba60283554b281c8baf77c5b78183
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 0.1.0
|
4
|
+
|
5
|
+
- Added ability to specify a thread when send messages (thanks to @hazi)
|
6
|
+
|
7
|
+
## 0.0.6
|
8
|
+
|
9
|
+
- Fixed proxy ENV support for ruby versions < 2.5.0
|
10
|
+
|
11
|
+
## 0.0.5
|
12
|
+
|
13
|
+
- Resolved missing dependencies for development and testing
|
14
|
+
|
15
|
+
## 0.0.4
|
16
|
+
|
17
|
+
- Added Card Messages support
|
18
|
+
|
19
|
+
## 0.0.3
|
20
|
+
|
21
|
+
- Added YARD documentation
|
22
|
+
- Added HangoutsChat::Sender::APIError exception for unsuccessful responses
|
23
|
+
|
24
|
+
## 0.0.2
|
25
|
+
|
26
|
+
- Added tests
|
27
|
+
- Improved appearance
|
28
|
+
- Added and performed rubocop code analyze
|
29
|
+
|
30
|
+
## 0.0.1
|
31
|
+
|
32
|
+
- Initial version with Hangouts Chat simple text messages support
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 enzinia
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/hangouts-chat.svg)](https://badge.fury.io/rb/hangouts-chat)
|
2
|
+
[![Build Status](https://travis-ci.org/enzinia/hangouts-chat.svg?branch=master)](https://travis-ci.org/enzinia/hangouts-chat)
|
3
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/c6106eab23781ab0be46/maintainability)](https://codeclimate.com/github/enzinia/hangouts-chat/maintainability)
|
4
|
+
[![Inline docs](http://inch-ci.org/github/enzinia/hangouts-chat.svg?branch=master)](http://inch-ci.org/github/enzinia/hangouts-chat)
|
5
|
+
|
6
|
+
# Hangouts Chat gem
|
7
|
+
Send messages to [Hangouts Chat](https://gsuite.google.com/products/chat/) rooms using incoming webhooks.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
```
|
11
|
+
$ gem install hangouts-chat
|
12
|
+
```
|
13
|
+
|
14
|
+
or add to your Gemfile
|
15
|
+
|
16
|
+
```
|
17
|
+
gem 'hangouts-chat'
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
```ruby
|
22
|
+
require 'hangouts_chat'
|
23
|
+
```
|
24
|
+
### Simple Text Message
|
25
|
+
Simple messages that appear inline as if typed by a user. Details and format: [Simple Text Messages](https://developers.google.com/hangouts/chat/reference/message-formats/basic)
|
26
|
+
```ruby
|
27
|
+
sender = HangoutsChat::Sender.new 'webhook_URL'
|
28
|
+
sender.simple 'text'
|
29
|
+
```
|
30
|
+
|
31
|
+
### Card Message
|
32
|
+
More complex messages that have UI elements with actions and HTML support. Details and format: [Card messages](https://developers.google.com/hangouts/chat/reference/message-formats/cards)
|
33
|
+
```ruby
|
34
|
+
sender = HangoutsChat::Sender.new 'webhook_URL'
|
35
|
+
header = { title: 'Pizza Bot Customer Support',
|
36
|
+
subtitle: 'pizzabot@example.com',
|
37
|
+
imageUrl: 'https://goo.gl/aeDtrS' }
|
38
|
+
sections = [{ widgets: [{ keyValue: { topLabel: 'Order No.', content: '12345' } },
|
39
|
+
{ keyValue: { topLabel: 'Status', content: 'In Delivery' } }] }]
|
40
|
+
|
41
|
+
sender.card(header, sections)
|
42
|
+
```
|
43
|
+
|
44
|
+
### Message to thread
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# Create new thread
|
48
|
+
sender = HangoutsChat::Sender.new 'webhook_URL'
|
49
|
+
res = sender.simple 'text'
|
50
|
+
|
51
|
+
# Response parse
|
52
|
+
res_json = JSON.parse(res.body)
|
53
|
+
thread_name = res_json.dig("thread", "name") #=> "spaces/SPACES_ID/threads/THREADS_ID"
|
54
|
+
|
55
|
+
# Send to thread (simple)
|
56
|
+
sender.simple('res message', thread: thread_name)
|
57
|
+
|
58
|
+
# Send to thread (card)
|
59
|
+
header = {...}
|
60
|
+
sections = {...}
|
61
|
+
sender.card(header, sections, thread: thread_name)
|
62
|
+
```
|
63
|
+
|
64
|
+
### How to get Webhook URL
|
65
|
+
1. Open channel to which you want to send messages
|
66
|
+
2. Click on the channel name in top bar and select 'Configure webhooks'
|
67
|
+
3. Click on 'Add webhook' and fill name for bot, that will be display messages
|
68
|
+
4. Click on 'Save' and copy 'Webhook URL'
|
69
|
+
|
70
|
+
Details: [Setting up an incoming webhook](https://developers.google.com/hangouts/chat/how-tos/webhooks)
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
Please feel free to contribute any changes, that you want too see in this gem.
|
74
|
+
Feature requests are also accepted.
|
75
|
+
|
76
|
+
Before Pull Request submitting please check
|
77
|
+
* Changed or added code has tests and YARD documentation
|
78
|
+
* All tests are pass
|
79
|
+
* `rubocop` doesn't report any offenses
|
80
|
+
|
81
|
+
## Tests
|
82
|
+
All tests are use usual Minitest `assert` syntax.
|
83
|
+
To run tests execute `rake tests`.
|
84
|
+
|
85
|
+
## Changelog
|
86
|
+
Changelog is available [here](CHANGELOG.md).
|
data/Rakefile
ADDED
data/lib/hangouts_chat.rb
CHANGED
@@ -1,16 +1,54 @@
|
|
1
1
|
require_relative 'hangouts_chat/version'
|
2
2
|
require_relative 'hangouts_chat/http'
|
3
|
+
require_relative 'hangouts_chat/exceptions'
|
3
4
|
|
5
|
+
# Main namespace
|
4
6
|
module HangoutsChat
|
7
|
+
# Provide methods to send messages to Hangouts Chat rooms using webhooks API
|
5
8
|
class Sender
|
9
|
+
# @return [String] Webhook URL, given on initialization
|
10
|
+
attr_reader :url
|
11
|
+
|
12
|
+
# Creates Sender object
|
13
|
+
# @param webhook_url [String] URL for incoming webhook
|
6
14
|
def initialize(webhook_url)
|
7
15
|
@url = webhook_url
|
8
16
|
@http = HTTP.new(@url)
|
9
17
|
end
|
10
18
|
|
11
|
-
|
19
|
+
# Sends Simple Text Message
|
20
|
+
# @param text [String] text to send to room
|
21
|
+
# @param thread [String] it will be sent as a reply (`nil` is a new thread will be created)
|
22
|
+
# @return [Net::HTTPResponse] response object
|
23
|
+
def simple(text, thread: nil)
|
12
24
|
payload = { text: text }
|
13
|
-
|
25
|
+
send_request(payload, thread: thread)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sends Card Message
|
29
|
+
# @since 0.0.4
|
30
|
+
# @param header [Hash] card header content
|
31
|
+
# @param sections [Array<Hash>] card widgets array
|
32
|
+
# @param thread [String] it will be sent as a reply (`nil` is a new thread will be created)
|
33
|
+
# @return [Net::HTTPResponse] response object
|
34
|
+
def card(header, sections, thread: nil)
|
35
|
+
payload = { cards: [header: header, sections: sections] }
|
36
|
+
send_request(payload, thread: thread)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
# Sends payload and check response
|
42
|
+
# @param payload [Hash] data to send by POST
|
43
|
+
# @param thread [String] it will be sent as a reply (`nil` is a new thread will be created)
|
44
|
+
# @return [Net::HTTPResponse] response object
|
45
|
+
# @raise [APIError] if got unsuccessful response
|
46
|
+
def send_request(payload, thread: nil)
|
47
|
+
payload[:thread] = { name: thread } if thread
|
48
|
+
|
49
|
+
response = @http.post payload
|
50
|
+
raise APIError, response unless response.is_a?(Net::HTTPSuccess)
|
51
|
+
response
|
14
52
|
end
|
15
53
|
end
|
16
54
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'net/http/response'
|
2
|
+
|
3
|
+
module HangoutsChat
|
4
|
+
class Sender
|
5
|
+
# Unsuccessful API respond exception
|
6
|
+
class APIError < StandardError
|
7
|
+
# Creates exception object with generated message
|
8
|
+
# @param response [Net::HTTPResponse] API response
|
9
|
+
def initialize(response)
|
10
|
+
msg = "HTTP #{response.code} #{response.msg}\n"
|
11
|
+
msg += "Body:\n#{response.body}"
|
12
|
+
super(msg)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/hangouts_chat/http.rb
CHANGED
@@ -3,20 +3,28 @@ require 'json'
|
|
3
3
|
|
4
4
|
module HangoutsChat
|
5
5
|
class Sender
|
6
|
+
# Unsuccessful respond exception
|
7
|
+
class APIError < StandardError; end
|
8
|
+
|
9
|
+
# Service class to send HTTP POST requests
|
6
10
|
class HTTP
|
11
|
+
# Creates HTTP::Post object with JSON content type
|
12
|
+
# @param url [String] URL to send request
|
7
13
|
def initialize(url)
|
8
14
|
@uri = URI(url)
|
9
15
|
@req = Net::HTTP::Post.new(@uri)
|
10
16
|
@req['Content-Type'] = 'application/json'
|
11
17
|
end
|
12
18
|
|
19
|
+
# Sends HTTP POST request
|
20
|
+
# @param payload [String] request body to send
|
21
|
+
# @return [Net::HTTPResponse] response object
|
13
22
|
def post(payload)
|
14
23
|
@req.body = payload.to_json
|
15
24
|
|
16
|
-
|
25
|
+
Net::HTTP.start(@uri.hostname, @uri.port, :ENV, use_ssl: true) do |http|
|
17
26
|
http.request(@req)
|
18
27
|
end
|
19
|
-
res
|
20
28
|
end
|
21
29
|
end
|
22
30
|
end
|
File without changes
|
data/test/hangouts_chat_test.rb
CHANGED
@@ -23,4 +23,55 @@ class HangoutsChatTest < Minitest::Test
|
|
23
23
|
assert_requested :post, @webhook_url, times: 1, body:
|
24
24
|
{ text: message }.to_json
|
25
25
|
end
|
26
|
+
|
27
|
+
def test_card_message_request
|
28
|
+
stub_request(:any, /chat\.googleapis\.com/).to_return(status: 200)
|
29
|
+
header = { title: 'Pizza Bot Customer Support',
|
30
|
+
subtitle: 'pizzabot@example.com',
|
31
|
+
imageUrl: 'https://goo.gl/aeDtrS' }
|
32
|
+
sections = [{ widgets: [{ keyValue: { topLabel: 'Order No.', content: '12345' } },
|
33
|
+
{ keyValue: { topLabel: 'Status', content: 'In Delivery' } }] }]
|
34
|
+
|
35
|
+
@sender.card(header, sections)
|
36
|
+
|
37
|
+
assert_requested :post, @webhook_url, times: 1, body:
|
38
|
+
{ cards: [header: header, sections: sections] }.to_json
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_simple_message_threaded_request
|
42
|
+
stub_request(:any, /chat\.googleapis\.com/).to_return(status: 200)
|
43
|
+
message = 'Test simple message'
|
44
|
+
thread = 'spaces/space_id/threads/threads_id'
|
45
|
+
|
46
|
+
@sender.simple(message, thread: thread)
|
47
|
+
|
48
|
+
assert_requested :post, @webhook_url, times: 1, body:
|
49
|
+
{ text: message, thread: { name: thread} }.to_json
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_card_message_threaded_request
|
53
|
+
stub_request(:any, /chat\.googleapis\.com/).to_return(status: 200)
|
54
|
+
header = { title: 'Pizza Bot Customer Support',
|
55
|
+
subtitle: 'pizzabot@example.com',
|
56
|
+
imageUrl: 'https://goo.gl/aeDtrS' }
|
57
|
+
sections = [{ widgets: [{ keyValue: { topLabel: 'Order No.', content: '12345' } },
|
58
|
+
{ keyValue: { topLabel: 'Status', content: 'In Delivery' } }] }]
|
59
|
+
thread = 'spaces/space_id/threads/threads_id'
|
60
|
+
|
61
|
+
@sender.card(header, sections, thread: thread)
|
62
|
+
|
63
|
+
assert_requested :post, @webhook_url, times: 1, body:
|
64
|
+
{ cards: [header: header, sections: sections], thread: { name: thread} }.to_json
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_api_error_exception_message
|
68
|
+
stub_request(:any, /chat\.googleapis\.com/)
|
69
|
+
.to_return(status: [403, 'Forbidden'], body: 'Response body')
|
70
|
+
|
71
|
+
exception = assert_raises HangoutsChat::Sender::APIError do
|
72
|
+
@sender.simple('Exception test')
|
73
|
+
end
|
74
|
+
assert_match(/^HTTP 403 Forbidden$/, exception.message)
|
75
|
+
assert_match(/^Body:\nResponse body$/, exception.message)
|
76
|
+
end
|
26
77
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hangouts-chat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- enzinia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: rubocop
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,18 +66,42 @@ dependencies:
|
|
38
66
|
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
68
|
version: '3'
|
41
|
-
|
42
|
-
|
43
|
-
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.9'
|
76
|
+
- - ">"
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 0.9.11
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - "~>"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0.9'
|
86
|
+
- - ">"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 0.9.11
|
89
|
+
description: Send messages to G Suite Hangouts Chat rooms using incoming webhooks
|
90
|
+
and Net::HTTP::Post
|
44
91
|
email: vkukovskij@gmail.com
|
45
92
|
executables: []
|
46
93
|
extensions: []
|
47
94
|
extra_rdoc_files: []
|
48
95
|
files:
|
96
|
+
- CHANGELOG.md
|
97
|
+
- LICENSE
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
49
100
|
- lib/hangouts_chat.rb
|
101
|
+
- lib/hangouts_chat/exceptions.rb
|
50
102
|
- lib/hangouts_chat/http.rb
|
51
103
|
- lib/hangouts_chat/version.rb
|
52
|
-
- test/
|
104
|
+
- test/hangouts_chat/http_test.rb
|
53
105
|
- test/hangouts_chat_test.rb
|
54
106
|
- test/test_helper.rb
|
55
107
|
homepage: https://github.com/enzinia/hangouts-chat
|
@@ -71,12 +123,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
123
|
- !ruby/object:Gem::Version
|
72
124
|
version: '0'
|
73
125
|
requirements: []
|
74
|
-
|
75
|
-
rubygems_version: 2.7.4
|
126
|
+
rubygems_version: 3.1.2
|
76
127
|
signing_key:
|
77
128
|
specification_version: 4
|
78
129
|
summary: Library for sending messages to Hangouts Chat rooms
|
79
130
|
test_files:
|
80
|
-
- test/hangouts-chat/http_test.rb
|
81
|
-
- test/hangouts_chat_test.rb
|
82
131
|
- test/test_helper.rb
|
132
|
+
- test/hangouts_chat_test.rb
|
133
|
+
- test/hangouts_chat/http_test.rb
|