noticed-openwa 0.1.0.rc1
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/LICENSE +21 -0
- data/README.md +136 -0
- data/lib/noticed/delivery_methods/openwa.rb +34 -0
- data/lib/noticed/openwa/client.rb +179 -0
- data/lib/noticed/openwa/error.rb +35 -0
- data/lib/noticed/openwa/version.rb +9 -0
- data/lib/noticed/openwa.rb +13 -0
- data/lib/noticed-openwa.rb +3 -0
- metadata +67 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 629165cc549aa18d6a34a427e8c0ca97f481d90e2b7d2dc1fc173d814b9bf95b
|
|
4
|
+
data.tar.gz: e4ef7f37efae839cac47c27e145cbdabf99ecc5b10218fb8da1305aec7f72301
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: dbdff5fdbe49005bc3df15cf1ca33c59fd7d61e5cb7c3bcf37eb58dc4e0bdf37f92bff61a19163fa04ff73ceb568fdd523aab47d725e110af3da54226a729d30
|
|
7
|
+
data.tar.gz: d724db3ddd865b9622f4eff3c83868a84ee9976840722b48713126fb2830472b01ab14b3dec97a9279375b02069d5096610aa42f91e29ff9a8652a67a06bedf6
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hugo-NF
|
|
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,136 @@
|
|
|
1
|
+
# noticed-openwa
|
|
2
|
+
|
|
3
|
+
WhatsApp delivery for [noticed](https://github.com/excid3/noticed) through [OpenWA](https://github.com/rmyndharis/openwa),
|
|
4
|
+
a self-hosted WhatsApp gateway. Written against OpenWA 0.30.0.
|
|
5
|
+
|
|
6
|
+
It implements only what a delivery needs: sending a text, an image or a document to a number, and checking a number is
|
|
7
|
+
on WhatsApp. Sessions, groups, contacts and OpenWA's webhooks are left to OpenWA itself.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem "noticed-openwa"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## The delivery method
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
class BookingConfirmedNotifier < Noticed::Event
|
|
19
|
+
deliver_by :openwa,
|
|
20
|
+
url: -> { ENV["OPENWA_URL"] },
|
|
21
|
+
api_key: -> { ENV["OPENWA_API_KEY"] },
|
|
22
|
+
session_id: -> { ENV["OPENWA_SESSION_ID"] },
|
|
23
|
+
to: -> { recipient.phone_number },
|
|
24
|
+
text: -> { "*Booking confirmed!* See you on board." },
|
|
25
|
+
files: -> { [ { filename: "ticket.pdf", mimetype: "application/pdf", content: -> { record.ticket_pdf } } ] }
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
| Option | |
|
|
30
|
+
|---|---|
|
|
31
|
+
| `url`, `api_key`, `session_id` | Required: OpenWA's API, its key (sent as `X-API-Key`) and the session that sends |
|
|
32
|
+
| `to` | Required: a phone number (punctuation and `+` are dropped) or a chat id (`<digits>@c.us`) |
|
|
33
|
+
| `text` | Required: the message, WhatsApp's markup included |
|
|
34
|
+
| `files` | `[{ filename:, mimetype:, content: }]`; `content` is the bytes or a lambda returning them, called only when that file is sent |
|
|
35
|
+
| `idempotency_key` | Defaults to `noticed-<notification id>` |
|
|
36
|
+
| `ca_file` | A CA bundle to trust, for an OpenWA behind a private CA's certificate |
|
|
37
|
+
|
|
38
|
+
Each option is a value, a lambda evaluated on the notification, or the name of a notifier method, as in noticed's own
|
|
39
|
+
delivery methods.
|
|
40
|
+
|
|
41
|
+
Without files it sends the text. With files it sends one message per file, the text as the first one's caption: images
|
|
42
|
+
(`image/*`) as images, the rest as documents. Files are sent as base64, never by URL.
|
|
43
|
+
|
|
44
|
+
Every message carries an `Idempotency-Key` (the option's key, suffixed `-1`, `-2`, ... per file): a retried job gets
|
|
45
|
+
OpenWA's first answer replayed instead of sending again.
|
|
46
|
+
|
|
47
|
+
## The client
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
client = Noticed::Openwa::Client.new(url:, api_key:, session_id:)
|
|
51
|
+
|
|
52
|
+
client.check_number("+55 61 99110-1515") # => { exists: true, whatsapp_id: "556191101515@c.us" }
|
|
53
|
+
client.send_text(chat_id, "Hello", idempotency_key: "welcome-42")
|
|
54
|
+
client.send_image(chat_id, content: png_bytes, mimetype: "image/png", caption: "...")
|
|
55
|
+
client.send_document(chat_id, content: pdf_bytes, mimetype: "application/pdf", filename: "ticket.pdf", caption: "...")
|
|
56
|
+
client.send_message(chat_id, text: "...", files: [...], idempotency_key: "...")
|
|
57
|
+
Noticed::Openwa::Client.chat_id("+55 61 99110-1515") # => "5561991101515@c.us"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The chat id WhatsApp holds can differ from a number's digits (Brazilian mobiles may be held without their ninth digit):
|
|
61
|
+
send to the `whatsapp_id` the check returns.
|
|
62
|
+
|
|
63
|
+
## Errors
|
|
64
|
+
|
|
65
|
+
All are `Noticed::Openwa::Error`, with the HTTP `status`, OpenWA's `code` and, for a rate limit, `retry_after` (seconds).
|
|
66
|
+
Their messages name the call, the status and OpenWA's reason, with any phone number filtered out.
|
|
67
|
+
|
|
68
|
+
| Error | When | Retry? |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| `AuthenticationError` | `401`, `403`: the key is missing, unknown, revoked or out of its scope | No |
|
|
71
|
+
| `NotOnWhatsappError` | WhatsApp can't resolve the recipient | No |
|
|
72
|
+
| `RetryableError` | Nothing was sent: the session isn't started or ready, `IDEMPOTENCY_KEY_IN_PROGRESS`, a rate limit (`429`), a timeout, `5xx`, OpenWA unreachable | Yes, with the same key |
|
|
73
|
+
| `UnknownOutcomeError` | `IDEMPOTENCY_OUTCOME_UNKNOWN`: the message may have been sent | No: a new key may send it twice |
|
|
74
|
+
| `RequestError` | OpenWA refused the request itself | No |
|
|
75
|
+
|
|
76
|
+
Retrying is the app's choice, for example on the delivery method's load hook:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
ActiveSupport.on_load(:noticed_delivery_methods_openwa) do
|
|
80
|
+
retry_on Noticed::Openwa::RetryableError, wait: :polynomially_longer, attempts: 5
|
|
81
|
+
end
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
bundle install
|
|
88
|
+
bundle exec rake # RuboCop and the specs
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The specs replay VCR cassettes (`spec/fixtures/vcr_cassettes/`) recorded against a real, paired OpenWA session; noticed
|
|
92
|
+
runs in a minimal Rails app (`spec/support/dummy_app.rb`). No request reaches OpenWA unless a cassette is missing.
|
|
93
|
+
|
|
94
|
+
### Recording the cassettes
|
|
95
|
+
|
|
96
|
+
Put a real session's values in the git-ignored `.env.test.local`, then delete the cassette (or run with
|
|
97
|
+
`VCR_RECORD_MODE=all`) and run its spec:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
OPENWA_URL=https://openwa.example
|
|
101
|
+
OPENWA_API_KEY=...
|
|
102
|
+
OPENWA_SESSION_ID=...
|
|
103
|
+
OPENWA_CA_FILE= # a private CA's bundle, if OpenWA's certificate needs one
|
|
104
|
+
OPENWA_TEST_NUMBER=+55... # your own number: it receives the test messages
|
|
105
|
+
OPENWA_TEST_NUMBER_NOT_ON_WHATSAPP=+55... # a number with no WhatsApp account
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The URL's host, the key, the session and both numbers (with and without the ninth digit) are replaced by placeholders
|
|
109
|
+
in the cassettes, and replayed with the placeholders of `spec/spec_helper.rb`. The "not on WhatsApp" spec checks the
|
|
110
|
+
number first and stops before sending if it is on WhatsApp: a real account would receive the message. Check a new
|
|
111
|
+
cassette for those values before committing it.
|
|
112
|
+
|
|
113
|
+
`spec/fixtures/vcr_cassettes/templates/send_text.yml` is an ERB template of a recorded send, for the answers a real
|
|
114
|
+
session can't be made to give (`409`, `429`, `503`); use it with `record: :none`.
|
|
115
|
+
|
|
116
|
+
## Releases
|
|
117
|
+
|
|
118
|
+
Published by `.github/workflows/release.yml`; versions come from the tags (`script/release_version.rb`), and
|
|
119
|
+
`lib/noticed/openwa/version.rb` is a placeholder the workflow fills when it builds.
|
|
120
|
+
|
|
121
|
+
- **Release candidates.** Every push to `main` that passes CI (docs-only changes aside) publishes the next candidate,
|
|
122
|
+
`X.Y.Z.rcN`, to RubyGems and as a GitHub pre-release tagged `vX.Y.Z.rcN`, with the changes since the previous tag as its
|
|
123
|
+
notes. The series continues (`0.1.0.rc12` → `0.1.0.rc13`) until it's released; after `0.1.0`, the next is `0.1.1.rc1`.
|
|
124
|
+
A PR labeled `release:minor` or `release:major` starts the next minor or major series instead (`0.2.0.rc1`,
|
|
125
|
+
`1.0.0.rc1`); the first ever is `0.1.0.rc1`.
|
|
126
|
+
- **Releases.** Untick "Set as a pre-release" on the latest candidate's GitHub release: the workflow publishes `X.Y.Z`
|
|
127
|
+
from that candidate's commit to RubyGems, tags it `vX.Y.Z` with a release whose notes hold every change since the
|
|
128
|
+
previous release (all its candidates'), and ticks the candidate back as a pre-release. A candidate that can't be
|
|
129
|
+
released (not the latest of its series, or its version already released) is ticked back, and the run fails with why.
|
|
130
|
+
|
|
131
|
+
Candidates are pre-releases on RubyGems: `gem install noticed-openwa --pre`, or name one in a Gemfile
|
|
132
|
+
(`gem "noticed-openwa", "0.1.0.rc1"`). A version can't be replaced: a mistake ships as the next candidate.
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
MIT.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Noticed
|
|
4
|
+
module DeliveryMethods
|
|
5
|
+
# deliver_by :openwa, url:, api_key:, session_id:, to:, text:, and optionally files:, idempotency_key:, ca_file:
|
|
6
|
+
#
|
|
7
|
+
# Each option is a value, a lambda (evaluated on the notification) or the name of a notifier method. `to` is a phone
|
|
8
|
+
# number or a chat id; `files` is [{ content:, mimetype:, filename: }] (content may be a lambda); the idempotency key
|
|
9
|
+
# defaults to the notification's, so a retried job never sends a message twice.
|
|
10
|
+
class Openwa < DeliveryMethod
|
|
11
|
+
required_options :url, :api_key, :session_id, :to, :text
|
|
12
|
+
|
|
13
|
+
def deliver
|
|
14
|
+
client.send_message(Noticed::Openwa::Client.chat_id(evaluate_option(:to)),
|
|
15
|
+
text: evaluate_option(:text),
|
|
16
|
+
files: Array(evaluate_option(:files)),
|
|
17
|
+
idempotency_key: evaluate_option(:idempotency_key) || default_idempotency_key)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The notification's id (an ephemeral notification has none: no key).
|
|
21
|
+
def default_idempotency_key
|
|
22
|
+
id = notification.id if notification.respond_to?(:id)
|
|
23
|
+
"noticed-#{id}" if id
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def client
|
|
27
|
+
Noticed::Openwa::Client.new(url: evaluate_option(:url), api_key: evaluate_option(:api_key),
|
|
28
|
+
session_id: evaluate_option(:session_id), ca_file: evaluate_option(:ca_file))
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
ActiveSupport.run_load_hooks :noticed_delivery_methods_openwa, Noticed::DeliveryMethods::Openwa
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "json"
|
|
5
|
+
require "net/http"
|
|
6
|
+
require "openssl"
|
|
7
|
+
require "uri"
|
|
8
|
+
|
|
9
|
+
module Noticed
|
|
10
|
+
module Openwa
|
|
11
|
+
# The parts of OpenWA's REST API a delivery needs: sending a text, an image or a document to a chat, and checking a
|
|
12
|
+
# number is on WhatsApp. Every call authenticates with the `X-API-Key` header, in the given session.
|
|
13
|
+
class Client
|
|
14
|
+
CHAT_SUFFIX = "@c.us"
|
|
15
|
+
IDEMPOTENCY_KEY = /\A[\x21-\x7e]{1,255}\z/
|
|
16
|
+
NETWORK_ERRORS = [ Timeout::Error, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::ETIMEDOUT,
|
|
17
|
+
SocketError, EOFError, IOError, OpenSSL::SSL::SSLError ].freeze
|
|
18
|
+
NOT_ON_WHATSAPP = /not (?:a )?registered|not on whatsapp/i
|
|
19
|
+
# A 400 for a session that isn't started: nothing was sent, and it can be started again.
|
|
20
|
+
SESSION_NOT_STARTED = /session is not (?:started|active)/i
|
|
21
|
+
# Digits long enough to be a number, kept out of error messages.
|
|
22
|
+
NUMBER = /\d{6,}/
|
|
23
|
+
|
|
24
|
+
attr_reader :url, :session_id
|
|
25
|
+
|
|
26
|
+
# ca_file: a CA bundle to trust, for an OpenWA behind a certificate of a private CA.
|
|
27
|
+
def initialize(url:, api_key:, session_id:, open_timeout: 5, read_timeout: 60, ca_file: nil)
|
|
28
|
+
raise ArgumentError, "url, api_key and session_id are required" if [ url, api_key, session_id ].any? { blank?(_1) }
|
|
29
|
+
|
|
30
|
+
@url = url.to_s.chomp("/")
|
|
31
|
+
@api_key = api_key
|
|
32
|
+
@session_id = session_id
|
|
33
|
+
@open_timeout = open_timeout
|
|
34
|
+
@read_timeout = read_timeout
|
|
35
|
+
@ca_file = ca_file
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The chat id of a number (any punctuation and a leading + are dropped); a chat id is returned as it is.
|
|
39
|
+
def self.chat_id(number)
|
|
40
|
+
number = number.to_s
|
|
41
|
+
number.include?("@") ? number : "#{number.delete('^0-9')}#{CHAT_SUFFIX}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# { exists:, whatsapp_id: }: whether WhatsApp has an account for the number, and the chat id it holds for it (it
|
|
45
|
+
# can differ from the digits given), nil when it has none.
|
|
46
|
+
def check_number(number)
|
|
47
|
+
digits = number.to_s.delete("^0-9")
|
|
48
|
+
raise ArgumentError, "the number has no digits" if digits.empty?
|
|
49
|
+
|
|
50
|
+
body = request(Net::HTTP::Get, "contacts/check/#{digits}", operation: "check_number")
|
|
51
|
+
{ exists: body["exists"] == true, whatsapp_id: body["whatsappId"] }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Each send returns OpenWA's answer ({ "messageId" =>, "timestamp" => }): the message was handed to the session,
|
|
55
|
+
# not yet delivered. The idempotency key makes a retry with the same key and body replay the first answer.
|
|
56
|
+
def send_text(chat_id, text, link_preview: nil, idempotency_key: nil)
|
|
57
|
+
send_message_request("send-text", { chatId: chat_id, text:, linkPreview: link_preview }, idempotency_key)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# content: the file's bytes, sent as base64.
|
|
61
|
+
def send_image(chat_id, content:, mimetype:, caption: nil, idempotency_key: nil)
|
|
62
|
+
body = { chatId: chat_id, base64: Base64.strict_encode64(content), mimetype:, caption: }
|
|
63
|
+
send_message_request("send-image", body, idempotency_key)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def send_document(chat_id, content:, mimetype:, filename:, caption: nil, idempotency_key: nil)
|
|
67
|
+
body = { chatId: chat_id, base64: Base64.strict_encode64(content), mimetype:, filename:, caption: }
|
|
68
|
+
send_message_request("send-document", body, idempotency_key)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# A text with its files: one message per file, the text as the first one's caption (images as images, the rest as
|
|
72
|
+
# documents), or the text alone without files. files: [{ content:, mimetype:, filename: }]; content may be a
|
|
73
|
+
# lambda, called only when that file is sent. Each message's idempotency key is the given one, suffixed with its
|
|
74
|
+
# position when there are files. Returns OpenWA's answers.
|
|
75
|
+
def send_message(chat_id, text:, files: [], idempotency_key: nil, link_preview: nil)
|
|
76
|
+
return [ send_text(chat_id, text, link_preview:, idempotency_key:) ] if files.empty?
|
|
77
|
+
|
|
78
|
+
files.each_with_index.map do |file, index|
|
|
79
|
+
send_file(chat_id, file, caption: index.zero? ? text : nil,
|
|
80
|
+
idempotency_key: idempotency_key && "#{idempotency_key}-#{index + 1}")
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def send_file(chat_id, file, caption:, idempotency_key:)
|
|
87
|
+
content = file.fetch(:content)
|
|
88
|
+
content = content.call if content.respond_to?(:call)
|
|
89
|
+
mimetype = file.fetch(:mimetype)
|
|
90
|
+
|
|
91
|
+
if mimetype.start_with?("image/")
|
|
92
|
+
send_image(chat_id, content:, mimetype:, caption:, idempotency_key:)
|
|
93
|
+
else
|
|
94
|
+
send_document(chat_id, content:, mimetype:, filename: file.fetch(:filename), caption:, idempotency_key:)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def send_message_request(endpoint, body, idempotency_key)
|
|
99
|
+
if idempotency_key && !IDEMPOTENCY_KEY.match?(idempotency_key)
|
|
100
|
+
raise ArgumentError, "the idempotency key must be 1-255 visible ASCII characters"
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
request(Net::HTTP::Post, "messages/#{endpoint}", body: body.compact,
|
|
104
|
+
headers: { "Idempotency-Key" => idempotency_key }.compact,
|
|
105
|
+
operation: endpoint.tr("-", "_"))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def request(method, path, operation:, body: nil, headers: {})
|
|
109
|
+
uri = URI("#{url}/api/sessions/#{URI.encode_www_form_component(session_id)}/#{path}")
|
|
110
|
+
request = method.new(uri, headers.merge("X-API-Key" => @api_key, "Accept" => "application/json"))
|
|
111
|
+
if body
|
|
112
|
+
request["Content-Type"] = "application/json"
|
|
113
|
+
request.body = JSON.generate(body)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
handle(http(uri).request(request), operation)
|
|
117
|
+
rescue *NETWORK_ERRORS => e
|
|
118
|
+
raise RetryableError, "OpenWA #{operation}: #{e.class}"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def http(uri)
|
|
122
|
+
Net::HTTP.new(uri.host, uri.port).tap do |http|
|
|
123
|
+
http.use_ssl = uri.scheme == "https"
|
|
124
|
+
http.open_timeout = @open_timeout
|
|
125
|
+
http.read_timeout = @read_timeout
|
|
126
|
+
http.ca_file = @ca_file if @ca_file
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def handle(response, operation)
|
|
131
|
+
status = response.code.to_i
|
|
132
|
+
body = parse(response.body)
|
|
133
|
+
return body if status.between?(200, 299)
|
|
134
|
+
|
|
135
|
+
raise error_class(status, body).new(error_message(operation, status, body),
|
|
136
|
+
status:, code: body["code"], retry_after: retry_after(response, body))
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def error_class(status, body)
|
|
140
|
+
case status
|
|
141
|
+
when 401, 403 then AuthenticationError
|
|
142
|
+
when 409 then body["code"] == "IDEMPOTENCY_OUTCOME_UNKNOWN" ? UnknownOutcomeError : RetryableError
|
|
143
|
+
when 408, 429, 500.. then RetryableError
|
|
144
|
+
when 400 then client_error_class(body)
|
|
145
|
+
else RequestError
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def client_error_class(body)
|
|
150
|
+
case messages(body)
|
|
151
|
+
when SESSION_NOT_STARTED then RetryableError
|
|
152
|
+
when NOT_ON_WHATSAPP then NotOnWhatsappError
|
|
153
|
+
else RequestError
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def error_message(operation, status, body)
|
|
158
|
+
details = messages(body).gsub(NUMBER, "[FILTERED]")
|
|
159
|
+
[ "OpenWA #{operation}: #{status}", body["code"], (details unless details.empty?) ].compact.join(" ")
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def messages(body) = Array(body["message"]).join("; ")
|
|
163
|
+
|
|
164
|
+
def retry_after(response, body)
|
|
165
|
+
seconds = body["retryAfterSeconds"] || response["Retry-After"]
|
|
166
|
+
Integer(seconds.to_s, 10, exception: false)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def parse(body)
|
|
170
|
+
parsed = body.to_s.empty? ? {} : JSON.parse(body)
|
|
171
|
+
parsed.is_a?(Hash) ? parsed : {}
|
|
172
|
+
rescue JSON::ParserError
|
|
173
|
+
{}
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def blank?(value) = value.nil? || value.to_s.strip.empty?
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Noticed
|
|
4
|
+
module Openwa
|
|
5
|
+
# An error of OpenWA's API: its HTTP status (nil when no response came back) and its stable `code`, when it sends
|
|
6
|
+
# one. The message names the operation, the status and the code, never the request: it holds numbers and texts.
|
|
7
|
+
class Error < StandardError
|
|
8
|
+
attr_reader :status, :code, :retry_after
|
|
9
|
+
|
|
10
|
+
def initialize(message = nil, status: nil, code: nil, retry_after: nil)
|
|
11
|
+
@status = status
|
|
12
|
+
@code = code
|
|
13
|
+
@retry_after = retry_after
|
|
14
|
+
super(message)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The API key is missing, unknown, revoked or expired, or its scope doesn't allow the call (401, 403).
|
|
19
|
+
class AuthenticationError < Error; end
|
|
20
|
+
|
|
21
|
+
# WhatsApp has no account for the number.
|
|
22
|
+
class NotOnWhatsappError < Error; end
|
|
23
|
+
|
|
24
|
+
# Nothing was sent, and the same call can be made again: the session isn't ready, a rate limit, a timeout, OpenWA or
|
|
25
|
+
# WhatsApp unavailable, or the same idempotency key still in progress.
|
|
26
|
+
class RetryableError < Error; end
|
|
27
|
+
|
|
28
|
+
# The message may have been sent (`IDEMPOTENCY_OUTCOME_UNKNOWN`): retrying with the same key gets the same answer,
|
|
29
|
+
# and a new key may send it twice.
|
|
30
|
+
class UnknownOutcomeError < Error; end
|
|
31
|
+
|
|
32
|
+
# OpenWA refused the request itself (an invalid number, file or body).
|
|
33
|
+
class RequestError < Error; end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Noticed
|
|
4
|
+
module Openwa
|
|
5
|
+
# A placeholder: the release workflow (.github/workflows/release.yml) writes the published version here when it
|
|
6
|
+
# builds the gem, computed from the tags. Never bumped by hand.
|
|
7
|
+
VERSION = "0.1.0.rc1"
|
|
8
|
+
end
|
|
9
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "noticed"
|
|
4
|
+
require "noticed/openwa/version"
|
|
5
|
+
require "noticed/openwa/error"
|
|
6
|
+
require "noticed/openwa/client"
|
|
7
|
+
|
|
8
|
+
module Noticed
|
|
9
|
+
# The delivery method is loaded on first use (`deliver_by :openwa`): it's an Active Job, so it needs the app's jobs.
|
|
10
|
+
module DeliveryMethods
|
|
11
|
+
autoload :Openwa, "noticed/delivery_methods/openwa"
|
|
12
|
+
end
|
|
13
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: noticed-openwa
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0.rc1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Hugo-NF
|
|
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: noticed
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '2.0'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '4'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '2.0'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '4'
|
|
32
|
+
description: 'A noticed delivery method and a small client for OpenWA''s REST API:
|
|
33
|
+
sends a text, an image or a document to a number, and checks a number is on WhatsApp.'
|
|
34
|
+
executables: []
|
|
35
|
+
extensions: []
|
|
36
|
+
extra_rdoc_files: []
|
|
37
|
+
files:
|
|
38
|
+
- LICENSE
|
|
39
|
+
- README.md
|
|
40
|
+
- lib/noticed-openwa.rb
|
|
41
|
+
- lib/noticed/delivery_methods/openwa.rb
|
|
42
|
+
- lib/noticed/openwa.rb
|
|
43
|
+
- lib/noticed/openwa/client.rb
|
|
44
|
+
- lib/noticed/openwa/error.rb
|
|
45
|
+
- lib/noticed/openwa/version.rb
|
|
46
|
+
licenses:
|
|
47
|
+
- MIT
|
|
48
|
+
metadata:
|
|
49
|
+
rubygems_mfa_required: 'true'
|
|
50
|
+
rdoc_options: []
|
|
51
|
+
require_paths:
|
|
52
|
+
- lib
|
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '3.3'
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
requirements: []
|
|
64
|
+
rubygems_version: 4.0.20
|
|
65
|
+
specification_version: 4
|
|
66
|
+
summary: WhatsApp delivery for noticed through OpenWA
|
|
67
|
+
test_files: []
|