connector-ruby 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/LICENSE +21 -0
- data/README.md +59 -0
- data/Rakefile +11 -0
- data/connector-ruby.gemspec +33 -0
- data/lib/connector_ruby/channels/base.rb +29 -0
- data/lib/connector_ruby/channels/telegram.rb +101 -0
- data/lib/connector_ruby/channels/whatsapp.rb +134 -0
- data/lib/connector_ruby/configuration.rb +22 -0
- data/lib/connector_ruby/error.rb +22 -0
- data/lib/connector_ruby/event.rb +49 -0
- data/lib/connector_ruby/http_client.rb +92 -0
- data/lib/connector_ruby/message.rb +43 -0
- data/lib/connector_ruby/version.rb +5 -0
- data/lib/connector_ruby/webhook_verifier.rb +26 -0
- data/lib/connector_ruby.rb +32 -0
- metadata +102 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6c22469b3849b241124a069729e5b127cb3459d4f9f55458a79b089fc3f33a28
|
|
4
|
+
data.tar.gz: 66fbf3cb744d58622775bed0a17840f6635984e9fc6a488ea507da5b2f5fbd6c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fef0557ed46edaf27e1c50b2e28e0cde341c8343fe394b96c51d3a46e17c1b62ccd2fbc88974abd41c3b59bf37f4b20349d2cd794ede451c7074107ebdd81824
|
|
7
|
+
data.tar.gz: 706e414863cc4f8d556166a6fd60d2a092593ded6bea22c6617768148e2c596e2121bbf6f934df5170680eeaf40d5421805db07348099a854de5466afd841d0e
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-03-09)
|
|
4
|
+
|
|
5
|
+
- Initial release
|
|
6
|
+
- WhatsApp Cloud API support (send text, buttons, image; parse webhooks)
|
|
7
|
+
- Telegram Bot API support (send text, buttons, image; parse webhooks)
|
|
8
|
+
- Unified Event model for normalized inbound messages
|
|
9
|
+
- Webhook signature verification (WhatsApp HMAC-SHA256)
|
|
10
|
+
- HTTP client with retry and timeout support
|
|
11
|
+
- Configuration DSL
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Johannes Dwi Cahyo
|
|
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,59 @@
|
|
|
1
|
+
# connector-ruby
|
|
2
|
+
|
|
3
|
+
Unified channel messaging SDK for Ruby. Framework-agnostic SDK for sending/receiving messages across chat platforms.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem "connector-ruby", "~> 0.1"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Supported Channels
|
|
12
|
+
|
|
13
|
+
- WhatsApp Cloud API
|
|
14
|
+
- Telegram Bot API
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### WhatsApp
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
client = ConnectorRuby::WhatsApp.new(
|
|
22
|
+
phone_number_id: "...",
|
|
23
|
+
access_token: "..."
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
client.send_text(to: "+62812...", text: "Hello!")
|
|
27
|
+
client.send_buttons(to: "+62812...", body: "Choose:", buttons: [
|
|
28
|
+
{ id: "opt1", title: "Option 1" },
|
|
29
|
+
{ id: "opt2", title: "Option 2" }
|
|
30
|
+
])
|
|
31
|
+
client.send_image(to: "+62812...", url: "https://...")
|
|
32
|
+
|
|
33
|
+
event = ConnectorRuby::WhatsApp.parse_webhook(request_body)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Telegram
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
client = ConnectorRuby::Telegram.new(bot_token: "...")
|
|
40
|
+
|
|
41
|
+
client.send_text(chat_id: 12345, text: "Hello!")
|
|
42
|
+
event = ConnectorRuby::Telegram.parse_webhook(request_body)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Configuration
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
ConnectorRuby.configure do |config|
|
|
49
|
+
config.whatsapp_phone_number_id = ENV["WA_PHONE_ID"]
|
|
50
|
+
config.whatsapp_access_token = ENV["WA_TOKEN"]
|
|
51
|
+
config.telegram_bot_token = ENV["TG_TOKEN"]
|
|
52
|
+
config.http_timeout = 30
|
|
53
|
+
config.http_retries = 3
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
MIT
|
data/Rakefile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/connector_ruby/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "connector-ruby"
|
|
7
|
+
spec.version = ConnectorRuby::VERSION
|
|
8
|
+
spec.authors = ["Johannes Dwi Cahyo"]
|
|
9
|
+
spec.email = ["johannes@example.com"]
|
|
10
|
+
spec.summary = "Unified channel messaging SDK for Ruby"
|
|
11
|
+
spec.description = "Framework-agnostic SDK for sending/receiving messages across chat platforms. Supports WhatsApp Cloud API, Telegram Bot API, and more."
|
|
12
|
+
spec.homepage = "https://github.com/johannesdwicahyo/connector-ruby"
|
|
13
|
+
spec.license = "MIT"
|
|
14
|
+
spec.required_ruby_version = ">= 3.0.0"
|
|
15
|
+
|
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
19
|
+
|
|
20
|
+
spec.files = Dir[
|
|
21
|
+
"lib/**/*.rb",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"CHANGELOG.md",
|
|
25
|
+
"Rakefile",
|
|
26
|
+
"connector-ruby.gemspec"
|
|
27
|
+
]
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
|
|
30
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
31
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
|
32
|
+
spec.add_development_dependency "webmock", "~> 3.0"
|
|
33
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectorRuby
|
|
4
|
+
module Channels
|
|
5
|
+
class Base
|
|
6
|
+
def send_text(*)
|
|
7
|
+
raise NotImplementedError, "#{self.class}#send_text not implemented"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def send_buttons(*)
|
|
11
|
+
raise NotImplementedError, "#{self.class}#send_buttons not implemented"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def send_image(*)
|
|
15
|
+
raise NotImplementedError, "#{self.class}#send_image not implemented"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.parse_webhook(*)
|
|
19
|
+
raise NotImplementedError, "#{self}.parse_webhook not implemented"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def http_client
|
|
25
|
+
@http_client ||= HttpClient.new
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectorRuby
|
|
4
|
+
module Channels
|
|
5
|
+
class Telegram < Base
|
|
6
|
+
BASE_URL = "https://api.telegram.org"
|
|
7
|
+
|
|
8
|
+
def initialize(bot_token: nil)
|
|
9
|
+
@bot_token = bot_token || ConnectorRuby.configuration.telegram_bot_token
|
|
10
|
+
raise ConfigurationError, "Telegram bot_token is required" unless @bot_token
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def send_text(chat_id:, text:, parse_mode: nil)
|
|
14
|
+
validate_send!(chat_id: chat_id, text: text)
|
|
15
|
+
payload = { chat_id: chat_id, text: text }
|
|
16
|
+
payload[:parse_mode] = parse_mode if parse_mode
|
|
17
|
+
api_call("sendMessage", payload)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def send_buttons(chat_id:, text:, buttons:)
|
|
21
|
+
validate_send!(chat_id: chat_id, text: text)
|
|
22
|
+
keyboard = buttons.map do |btn|
|
|
23
|
+
[{ text: btn[:title], callback_data: btn[:id] }]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
payload = {
|
|
27
|
+
chat_id: chat_id,
|
|
28
|
+
text: text,
|
|
29
|
+
reply_markup: { inline_keyboard: keyboard }
|
|
30
|
+
}
|
|
31
|
+
api_call("sendMessage", payload)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def send_image(chat_id:, url:, caption: nil)
|
|
35
|
+
validate_send!(chat_id: chat_id)
|
|
36
|
+
payload = { chat_id: chat_id, photo: url }
|
|
37
|
+
payload[:caption] = caption if caption
|
|
38
|
+
api_call("sendPhoto", payload)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.parse_webhook(body)
|
|
42
|
+
data = body.is_a?(String) ? JSON.parse(body) : body
|
|
43
|
+
|
|
44
|
+
if data["message"]
|
|
45
|
+
parse_message(data["message"])
|
|
46
|
+
elsif data["callback_query"]
|
|
47
|
+
parse_callback(data["callback_query"])
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def api_call(method, payload)
|
|
54
|
+
url = "#{BASE_URL}/bot#{@bot_token}/#{method}"
|
|
55
|
+
http_client.post(url, body: payload)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def validate_send!(chat_id:, text: nil)
|
|
59
|
+
raise ConnectorRuby::Error, "Recipient 'chat_id' cannot be nil or empty" if chat_id.nil? || chat_id.to_s.strip.empty?
|
|
60
|
+
if text
|
|
61
|
+
raise ConnectorRuby::Error, "Text cannot be nil or empty" if text.nil? || text.to_s.strip.empty?
|
|
62
|
+
raise ConnectorRuby::Error, "Text exceeds 4096 character limit" if text.length > 4096
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.parse_message(msg)
|
|
67
|
+
return nil unless msg
|
|
68
|
+
Event.new(
|
|
69
|
+
type: :message,
|
|
70
|
+
channel: :telegram,
|
|
71
|
+
from: msg.dig("from", "id")&.to_s,
|
|
72
|
+
text: msg["text"],
|
|
73
|
+
timestamp: msg["date"] ? Time.at(msg["date"]) : nil,
|
|
74
|
+
message_id: msg["message_id"]&.to_s,
|
|
75
|
+
metadata: {
|
|
76
|
+
chat_id: msg.dig("chat", "id"),
|
|
77
|
+
chat_type: msg.dig("chat", "type"),
|
|
78
|
+
from_username: msg.dig("from", "username"),
|
|
79
|
+
from_first_name: msg.dig("from", "first_name")
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.parse_callback(callback)
|
|
85
|
+
return nil unless callback
|
|
86
|
+
Event.new(
|
|
87
|
+
type: :callback,
|
|
88
|
+
channel: :telegram,
|
|
89
|
+
from: callback.dig("from", "id")&.to_s,
|
|
90
|
+
text: callback["data"],
|
|
91
|
+
message_id: callback["id"],
|
|
92
|
+
metadata: {
|
|
93
|
+
chat_id: callback.dig("message", "chat", "id"),
|
|
94
|
+
original_message_id: callback.dig("message", "message_id"),
|
|
95
|
+
from_username: callback.dig("from", "username")
|
|
96
|
+
}
|
|
97
|
+
)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectorRuby
|
|
4
|
+
module Channels
|
|
5
|
+
class WhatsApp < Base
|
|
6
|
+
BASE_URL = "https://graph.facebook.com/v21.0"
|
|
7
|
+
|
|
8
|
+
def initialize(phone_number_id: nil, access_token: nil)
|
|
9
|
+
@phone_number_id = phone_number_id || ConnectorRuby.configuration.whatsapp_phone_number_id
|
|
10
|
+
@access_token = access_token || ConnectorRuby.configuration.whatsapp_access_token
|
|
11
|
+
|
|
12
|
+
raise ConfigurationError, "WhatsApp phone_number_id is required" unless @phone_number_id
|
|
13
|
+
raise ConfigurationError, "WhatsApp access_token is required" unless @access_token
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def send_text(to:, text:)
|
|
17
|
+
validate_send!(to: to, text: text)
|
|
18
|
+
payload = {
|
|
19
|
+
messaging_product: "whatsapp",
|
|
20
|
+
to: to,
|
|
21
|
+
type: "text",
|
|
22
|
+
text: { body: text }
|
|
23
|
+
}
|
|
24
|
+
post_message(payload)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def send_buttons(to:, body:, buttons:)
|
|
28
|
+
validate_send!(to: to, text: body)
|
|
29
|
+
formatted_buttons = buttons.map do |btn|
|
|
30
|
+
{ type: "reply", reply: { id: btn[:id], title: btn[:title] } }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
payload = {
|
|
34
|
+
messaging_product: "whatsapp",
|
|
35
|
+
to: to,
|
|
36
|
+
type: "interactive",
|
|
37
|
+
interactive: {
|
|
38
|
+
type: "button",
|
|
39
|
+
body: { text: body },
|
|
40
|
+
action: { buttons: formatted_buttons }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
post_message(payload)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def send_image(to:, url:, caption: nil)
|
|
47
|
+
validate_send!(to: to)
|
|
48
|
+
image = { link: url }
|
|
49
|
+
image[:caption] = caption if caption
|
|
50
|
+
|
|
51
|
+
payload = {
|
|
52
|
+
messaging_product: "whatsapp",
|
|
53
|
+
to: to,
|
|
54
|
+
type: "image",
|
|
55
|
+
image: image
|
|
56
|
+
}
|
|
57
|
+
post_message(payload)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.parse_webhook(body, signature: nil)
|
|
61
|
+
data = body.is_a?(String) ? JSON.parse(body) : body
|
|
62
|
+
|
|
63
|
+
entry = data.dig("entry", 0)
|
|
64
|
+
return nil unless entry
|
|
65
|
+
|
|
66
|
+
changes = entry.dig("changes", 0)
|
|
67
|
+
return nil unless changes
|
|
68
|
+
|
|
69
|
+
value = changes["value"]
|
|
70
|
+
return nil unless value
|
|
71
|
+
|
|
72
|
+
if value["messages"]&.any?
|
|
73
|
+
parse_message(value)
|
|
74
|
+
elsif value["statuses"]&.any?
|
|
75
|
+
parse_status(value)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def post_message(payload)
|
|
82
|
+
url = "#{BASE_URL}/#{@phone_number_id}/messages"
|
|
83
|
+
http_client.post(url, body: payload, headers: auth_headers)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def auth_headers
|
|
87
|
+
{ "Authorization" => "Bearer #{@access_token}" }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def validate_send!(to:, text: nil)
|
|
91
|
+
raise ConnectorRuby::Error, "Recipient 'to' cannot be nil or empty" if to.nil? || to.to_s.strip.empty?
|
|
92
|
+
if text
|
|
93
|
+
raise ConnectorRuby::Error, "Text cannot be nil or empty" if text.nil? || text.to_s.strip.empty?
|
|
94
|
+
raise ConnectorRuby::Error, "Text exceeds 4096 character limit" if text.length > 4096
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.parse_message(value)
|
|
99
|
+
return nil unless value["messages"]&.any?
|
|
100
|
+
msg = value["messages"][0]
|
|
101
|
+
contact = value.dig("contacts", 0)
|
|
102
|
+
|
|
103
|
+
Event.new(
|
|
104
|
+
type: :message,
|
|
105
|
+
channel: :whatsapp,
|
|
106
|
+
from: msg["from"],
|
|
107
|
+
text: msg.dig("text", "body"),
|
|
108
|
+
timestamp: msg["timestamp"] ? Time.at(msg["timestamp"].to_i) : nil,
|
|
109
|
+
message_id: msg["id"],
|
|
110
|
+
metadata: {
|
|
111
|
+
contact_name: contact&.dig("profile", "name"),
|
|
112
|
+
message_type: msg["type"]
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def self.parse_status(value)
|
|
118
|
+
return nil unless value["statuses"]&.any?
|
|
119
|
+
status = value["statuses"][0]
|
|
120
|
+
|
|
121
|
+
Event.new(
|
|
122
|
+
type: :status,
|
|
123
|
+
channel: :whatsapp,
|
|
124
|
+
to: status["recipient_id"],
|
|
125
|
+
message_id: status["id"],
|
|
126
|
+
metadata: {
|
|
127
|
+
status: status["status"],
|
|
128
|
+
timestamp: status["timestamp"]
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectorRuby
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_accessor :whatsapp_phone_number_id, :whatsapp_access_token,
|
|
6
|
+
:telegram_bot_token,
|
|
7
|
+
:http_timeout, :http_retries, :http_open_timeout,
|
|
8
|
+
:on_request, :on_response, :on_error
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@whatsapp_phone_number_id = nil
|
|
12
|
+
@whatsapp_access_token = nil
|
|
13
|
+
@telegram_bot_token = nil
|
|
14
|
+
@http_timeout = 30
|
|
15
|
+
@http_retries = 3
|
|
16
|
+
@http_open_timeout = 10
|
|
17
|
+
@on_request = nil
|
|
18
|
+
@on_response = nil
|
|
19
|
+
@on_error = nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectorRuby
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
class ConfigurationError < Error; end
|
|
6
|
+
|
|
7
|
+
class ApiError < Error
|
|
8
|
+
attr_reader :status, :body, :channel, :response
|
|
9
|
+
|
|
10
|
+
def initialize(message, status: nil, body: nil, channel: nil, response: nil)
|
|
11
|
+
@status = status
|
|
12
|
+
@body = body
|
|
13
|
+
@channel = channel
|
|
14
|
+
@response = response
|
|
15
|
+
super(message)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class WebhookVerificationError < Error; end
|
|
20
|
+
class RateLimitError < ApiError; end
|
|
21
|
+
class AuthenticationError < ApiError; end
|
|
22
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectorRuby
|
|
4
|
+
class Event
|
|
5
|
+
attr_reader :type, :channel, :from, :to, :text, :timestamp,
|
|
6
|
+
:message_id, :payload, :metadata
|
|
7
|
+
|
|
8
|
+
TYPES = %i[message callback status delivery read reaction].freeze
|
|
9
|
+
|
|
10
|
+
def initialize(type:, channel:, from: nil, to: nil, text: nil,
|
|
11
|
+
timestamp: nil, message_id: nil, payload: nil, metadata: {})
|
|
12
|
+
@type = type
|
|
13
|
+
@channel = channel
|
|
14
|
+
@from = from
|
|
15
|
+
@to = to
|
|
16
|
+
@text = text
|
|
17
|
+
@timestamp = timestamp
|
|
18
|
+
@message_id = message_id
|
|
19
|
+
@payload = payload
|
|
20
|
+
@metadata = metadata
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def message?
|
|
24
|
+
type == :message
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def callback?
|
|
28
|
+
type == :callback
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def status?
|
|
32
|
+
type == :status
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def to_h
|
|
36
|
+
{
|
|
37
|
+
type: @type,
|
|
38
|
+
channel: @channel,
|
|
39
|
+
from: @from,
|
|
40
|
+
to: @to,
|
|
41
|
+
text: @text,
|
|
42
|
+
timestamp: @timestamp,
|
|
43
|
+
message_id: @message_id,
|
|
44
|
+
payload: @payload,
|
|
45
|
+
metadata: @metadata
|
|
46
|
+
}
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "uri"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module ConnectorRuby
|
|
8
|
+
class HttpClient
|
|
9
|
+
def initialize(timeout: nil, retries: nil, open_timeout: nil)
|
|
10
|
+
config = ConnectorRuby.configuration
|
|
11
|
+
@timeout = timeout || config.http_timeout
|
|
12
|
+
@retries = retries || config.http_retries
|
|
13
|
+
@open_timeout = open_timeout || config.http_open_timeout
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def get(url, headers: {})
|
|
17
|
+
request(:get, url, headers: headers)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def post(url, body:, headers: {})
|
|
21
|
+
request(:post, url, body: body, headers: headers)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def request(method, url, body: nil, headers: {})
|
|
27
|
+
uri = URI.parse(url)
|
|
28
|
+
attempts = 0
|
|
29
|
+
|
|
30
|
+
begin
|
|
31
|
+
attempts += 1
|
|
32
|
+
http = build_http(uri)
|
|
33
|
+
req = build_request(method, uri, body: body, headers: headers)
|
|
34
|
+
response = http.request(req)
|
|
35
|
+
handle_response(response)
|
|
36
|
+
rescue RateLimitError => e
|
|
37
|
+
if attempts < @retries
|
|
38
|
+
sleep(2 ** (attempts - 1))
|
|
39
|
+
retry
|
|
40
|
+
end
|
|
41
|
+
raise
|
|
42
|
+
rescue Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNRESET => e
|
|
43
|
+
retry if attempts < @retries
|
|
44
|
+
raise ConnectorRuby::Error, "HTTP request failed after #{@retries} attempts: #{e.message}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def build_http(uri)
|
|
49
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
50
|
+
http.use_ssl = uri.scheme == "https"
|
|
51
|
+
http.read_timeout = @timeout
|
|
52
|
+
http.open_timeout = @open_timeout
|
|
53
|
+
http
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def build_request(method, uri, body: nil, headers: {})
|
|
57
|
+
klass = case method
|
|
58
|
+
when :get then Net::HTTP::Get
|
|
59
|
+
when :post then Net::HTTP::Post
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
req = klass.new(uri.request_uri)
|
|
63
|
+
headers.each { |k, v| req[k] = v }
|
|
64
|
+
req["Content-Type"] ||= "application/json"
|
|
65
|
+
|
|
66
|
+
if body
|
|
67
|
+
req.body = body.is_a?(String) ? body : JSON.generate(body)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
req
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def handle_response(response)
|
|
74
|
+
body = begin
|
|
75
|
+
JSON.parse(response.body)
|
|
76
|
+
rescue JSON::ParserError
|
|
77
|
+
response.body
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
case response.code.to_i
|
|
81
|
+
when 200..299
|
|
82
|
+
body
|
|
83
|
+
when 401
|
|
84
|
+
raise AuthenticationError.new("Authentication failed", status: response.code.to_i, body: body)
|
|
85
|
+
when 429
|
|
86
|
+
raise RateLimitError.new("Rate limited", status: 429, body: body)
|
|
87
|
+
else
|
|
88
|
+
raise ApiError.new("API error: #{response.code}", status: response.code.to_i, body: body)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConnectorRuby
|
|
4
|
+
class Message
|
|
5
|
+
attr_reader :type, :to, :text, :buttons, :image_url, :caption, :metadata
|
|
6
|
+
|
|
7
|
+
TYPES = %i[text buttons image template].freeze
|
|
8
|
+
|
|
9
|
+
def initialize(type:, to:, text: nil, buttons: nil, image_url: nil, caption: nil, metadata: {})
|
|
10
|
+
@type = type
|
|
11
|
+
@to = to
|
|
12
|
+
@text = text
|
|
13
|
+
@buttons = buttons
|
|
14
|
+
@image_url = image_url
|
|
15
|
+
@caption = caption
|
|
16
|
+
@metadata = metadata
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.text(to:, text:)
|
|
20
|
+
new(type: :text, to: to, text: text)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.buttons(to:, body:, buttons:)
|
|
24
|
+
new(type: :buttons, to: to, text: body, buttons: buttons)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.image(to:, url:, caption: nil)
|
|
28
|
+
new(type: :image, to: to, image_url: url, caption: caption)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_h
|
|
32
|
+
{
|
|
33
|
+
type: @type,
|
|
34
|
+
to: @to,
|
|
35
|
+
text: @text,
|
|
36
|
+
buttons: @buttons,
|
|
37
|
+
image_url: @image_url,
|
|
38
|
+
caption: @caption,
|
|
39
|
+
metadata: @metadata
|
|
40
|
+
}.compact
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
|
|
5
|
+
module ConnectorRuby
|
|
6
|
+
class WebhookVerifier
|
|
7
|
+
def self.verify_whatsapp(payload, signature:, app_secret:)
|
|
8
|
+
expected = "sha256=#{OpenSSL::HMAC.hexdigest("SHA256", app_secret, payload)}"
|
|
9
|
+
secure_compare(expected, signature)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.verify_telegram(token:, payload:, secret_token: nil, header_value: nil)
|
|
13
|
+
return false unless secret_token && header_value
|
|
14
|
+
computed = OpenSSL::HMAC.hexdigest("SHA256", secret_token, payload.to_s)
|
|
15
|
+
secure_compare(computed, header_value.to_s)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.secure_compare(a, b)
|
|
19
|
+
a = a.to_s.downcase
|
|
20
|
+
b = b.to_s.downcase
|
|
21
|
+
return false unless a.bytesize == b.bytesize
|
|
22
|
+
|
|
23
|
+
OpenSSL.fixed_length_secure_compare(a, b)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "connector_ruby/version"
|
|
4
|
+
require_relative "connector_ruby/error"
|
|
5
|
+
require_relative "connector_ruby/configuration"
|
|
6
|
+
require_relative "connector_ruby/event"
|
|
7
|
+
require_relative "connector_ruby/message"
|
|
8
|
+
require_relative "connector_ruby/http_client"
|
|
9
|
+
require_relative "connector_ruby/webhook_verifier"
|
|
10
|
+
require_relative "connector_ruby/channels/base"
|
|
11
|
+
require_relative "connector_ruby/channels/whatsapp"
|
|
12
|
+
require_relative "connector_ruby/channels/telegram"
|
|
13
|
+
|
|
14
|
+
module ConnectorRuby
|
|
15
|
+
class << self
|
|
16
|
+
def configuration
|
|
17
|
+
@configuration ||= Configuration.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def configure
|
|
21
|
+
yield(configuration)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def reset_configuration!
|
|
25
|
+
@configuration = Configuration.new
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Convenience aliases
|
|
30
|
+
WhatsApp = Channels::WhatsApp
|
|
31
|
+
Telegram = Channels::Telegram
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: connector-ruby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Johannes Dwi Cahyo
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: minitest
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '5.0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '5.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rake
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '13.0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '13.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: webmock
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '3.0'
|
|
54
|
+
description: Framework-agnostic SDK for sending/receiving messages across chat platforms.
|
|
55
|
+
Supports WhatsApp Cloud API, Telegram Bot API, and more.
|
|
56
|
+
email:
|
|
57
|
+
- johannes@example.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- CHANGELOG.md
|
|
63
|
+
- LICENSE
|
|
64
|
+
- README.md
|
|
65
|
+
- Rakefile
|
|
66
|
+
- connector-ruby.gemspec
|
|
67
|
+
- lib/connector_ruby.rb
|
|
68
|
+
- lib/connector_ruby/channels/base.rb
|
|
69
|
+
- lib/connector_ruby/channels/telegram.rb
|
|
70
|
+
- lib/connector_ruby/channels/whatsapp.rb
|
|
71
|
+
- lib/connector_ruby/configuration.rb
|
|
72
|
+
- lib/connector_ruby/error.rb
|
|
73
|
+
- lib/connector_ruby/event.rb
|
|
74
|
+
- lib/connector_ruby/http_client.rb
|
|
75
|
+
- lib/connector_ruby/message.rb
|
|
76
|
+
- lib/connector_ruby/version.rb
|
|
77
|
+
- lib/connector_ruby/webhook_verifier.rb
|
|
78
|
+
homepage: https://github.com/johannesdwicahyo/connector-ruby
|
|
79
|
+
licenses:
|
|
80
|
+
- MIT
|
|
81
|
+
metadata:
|
|
82
|
+
homepage_uri: https://github.com/johannesdwicahyo/connector-ruby
|
|
83
|
+
source_code_uri: https://github.com/johannesdwicahyo/connector-ruby
|
|
84
|
+
changelog_uri: https://github.com/johannesdwicahyo/connector-ruby/blob/main/CHANGELOG.md
|
|
85
|
+
rdoc_options: []
|
|
86
|
+
require_paths:
|
|
87
|
+
- lib
|
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: 3.0.0
|
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
requirements: []
|
|
99
|
+
rubygems_version: 3.6.9
|
|
100
|
+
specification_version: 4
|
|
101
|
+
summary: Unified channel messaging SDK for Ruby
|
|
102
|
+
test_files: []
|