huginn_sms77_agent 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 65d9f9785bdc9d6967ffd31c6bdca8467930fae501827a70f7f33d2c802f610f
4
+ data.tar.gz: d22bc9255e81982237b775f10344be1d00500263974d0aad4ba5cb3085df8e39
5
+ SHA512:
6
+ metadata.gz: 1c94c1f2ca632c7dd28693c117a953a240c41da1574728c1d95729762fd9dd1ab6b31cffd51cd57c2a60cf8a50d9ebb049b90d504dfd4ae2eba80950666cb1b0
7
+ data.tar.gz: f93e3984a7ebbaaaf4dd473b9d19397be06c69ba4ff11f2d174ecf6ba61b3abd88fba0910faccba10d60498a057a78cab13363b50dcb85eb74c522111300e3c1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-present sms77 e.K.
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.
@@ -0,0 +1,113 @@
1
+ require 'net/http'
2
+
3
+ module Agents
4
+ class Sms77Agent < Agent
5
+ no_bulk_receive!
6
+ cannot_create_events!
7
+ cannot_be_scheduled!
8
+ description <<-MD
9
+ Agent for sending SMS from Huginn via Sms77.io.
10
+
11
+ Get an API key at [Sms77](https://sms77.io) and you are ready to start sending!
12
+
13
+ Options:
14
+ * `text` - The message you want to send (required)
15
+ * `to` - recipient(s) separated by comma - phone number(s) or contact(s) (required)
16
+
17
+ * `from` - Sender number. *Allowed value: A string of 11 alphanumeric or 16 numeric characters*
18
+ * `debug` - If activated no SMS will be sent or calculated *Allowed values: 0, 1*
19
+ * `delay` - Date/time for time-delayed SMS: *Allowed values: A UNIX timestamp or date with format yyyy-mm-dd hh:ii*
20
+ * `no_reload` - Switch off reload lock. This lock prevents the sending of the same SMS (text, type and recipient alike) within 180 seconds *Allowed values: 0, 1*
21
+ * `unicode` - For Cyrillic, Arabic etc characters. SMS length is then reduced to 70 characters. The API recognizes the coding automatically; this parameter enforces it *Allowed values: 0, 1*
22
+ * `flash` - Send SMS as Flash SMS. These are displayed directly in the receiver’s display *Allowed values: 0, 1*
23
+ * `udh` - Individual User Data Header of the SMS. If specified and parameter text contains hexcode, message gets sent as 8-bit binary
24
+ * `utf8` - Forces the detection as a UTF8 character set and overrides automatic recognition of the API *Allowed values: 0, 1*
25
+ * `ttl` - Specifies the validity period of the SMS in milliseconds *Allowed values: integer between 300000 and 86400000*
26
+ * `details` - Shows numerous details to the sent SMS. Handy for debugging *Allowed values: 0, 1*
27
+ * `return_msg_id` - If this parameter is set, the ID of the SMS is output in the second line after the status code *Allowed values: 0, 1*
28
+ * `label` - Custom label for whatever use. *Allowed characters*: a-z, A-Z, 0-9, .-_@
29
+ * `json` - The output is more detailed in JSON format *Allowed values: 0, 1*
30
+ * `performance_tracking` - Enable Performance Tracking for URLs found in the message text *Allowed values: 0, 1*
31
+ * `foreign_id` - Custom tag to return in DLR callbacks etc. Max. 64 chars. *Allowed characters: a-z, A-Z, 0-9, .-_@*
32
+ MD
33
+
34
+ def default_options
35
+ {
36
+ 'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
37
+ 'from' => 'Huginn',
38
+ 'text' => 'Hope to see you again soon!',
39
+ 'to' => '+4900000000000',
40
+ 'debug' => 0,
41
+ 'delay' => nil,
42
+ 'no_reload' => nil,
43
+ 'unicode' => 0,
44
+ 'flash' => 0,
45
+ 'udh' => nil,
46
+ 'utf8' => 0,
47
+ 'ttl' => nil,
48
+ 'details' => 0,
49
+ 'return_msg_id' => 0,
50
+ 'label' => nil,
51
+ 'json' => 0,
52
+ 'performance_tracking' => 0,
53
+ 'foreign_id' => nil,
54
+ }
55
+ end
56
+
57
+ def validate_options
58
+ unless options['api_key'].present?
59
+ errors.add(:base, '`api_key` is required.')
60
+ end
61
+
62
+ unless interpolated['text'].present?
63
+ errors.add(:base, '`text` is required.')
64
+ end
65
+
66
+ unless interpolated['to'].present?
67
+ errors.add(:base, '`to` is required.')
68
+ end
69
+ end
70
+
71
+ def working?
72
+ interpolated['api_key'].present? && !recent_error_logs?
73
+ end
74
+
75
+ def receive(incoming_events)
76
+ incoming_events.each do |event|
77
+ interpolate_with event do
78
+ send_sms(interpolated)
79
+ end
80
+ end
81
+ end
82
+
83
+ def send_sms(payload)
84
+ fd = default_options
85
+ fd.delete('api_key')
86
+ fd.each { |k, v| fd[k] = payload[k] }
87
+
88
+ body = HTTParty.post('https://gateway.sms77.io/api/sms',
89
+ :body => fd, :headers => {'Authorization' => "Basic #{payload['api_key']}"})
90
+
91
+ if !body.is_a?(Hash)
92
+ begin
93
+ body = JSON.parse(body)
94
+ rescue JSON::ParserError => e
95
+ # Ignored
96
+ end
97
+ end
98
+
99
+ code = Hash === body ? body['success'] : "#{body}".split(/\n+/)[0]
100
+ code = code.to_i
101
+
102
+ if [100, 101].include? code
103
+ log body
104
+ elsif 900 === code
105
+ raise StandardError, "SMS77_AUTH_ERROR: #{body}"
106
+ else
107
+ raise StandardError, "SMS77_DISPATCH_ERROR: #{body}"
108
+ end
109
+
110
+ body
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,3 @@
1
+ require 'huginn_agent'
2
+
3
+ HuginnAgent.register 'huginn_sms77_agent/sms77_agent'
@@ -0,0 +1,109 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::Sms77Agent do
5
+ is_live_test = nil != ENV["SMS77_LIVE_TEST"]
6
+ api_key = is_live_test ? ENV["SMS77_API_KEY"] : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
7
+ to = is_live_test ? ENV["SMS77_RECIPIENT"] : '+4900000000000'
8
+
9
+ before do
10
+ @default_options = {
11
+ api_key: api_key,
12
+ text: 'Hope to see you again soon!',
13
+ to: to,
14
+ }
15
+
16
+ if is_live_test
17
+ require 'vcr'
18
+
19
+ VCR.configure do |c|
20
+ c.allow_http_connections_when_no_cassette = true
21
+
22
+ c.hook_into :webmock
23
+ end
24
+
25
+ WebMock.allow_net_connect!
26
+ end
27
+
28
+ @checker = Agents::Sms77Agent.new name: 'Sms77AgentTest', options: @default_options
29
+ @checker.user = users(:bob)
30
+ @checker.save!
31
+
32
+ @event = Event.new
33
+ @event.agent = agents(:bob_manual_event_agent)
34
+ @event.payload = @default_options
35
+ @event.save!
36
+ end
37
+
38
+ describe 'validation' do
39
+ before do
40
+ expect(@checker).to be_valid
41
+ end
42
+
43
+ it "should validate the presence of api_key" do
44
+ @checker.options[:api_key] = ''
45
+ expect(@checker).not_to be_valid
46
+ end
47
+
48
+ it "should validate the presence of to" do
49
+ @checker.options[:to] = ''
50
+ expect(@checker).not_to be_valid
51
+ end
52
+
53
+ it "should validate the presence of text" do
54
+ @checker.options[:text] = ''
55
+ expect(@checker).not_to be_valid
56
+ end
57
+ end
58
+
59
+ describe '#receive' do
60
+ it "should receive event" do
61
+ stub(HTTParty).post { {
62
+ "success" => "100",
63
+ "total_price" => 0,
64
+ "balance" => 7.228,
65
+ "debug" => "true",
66
+ "sms_type" => "direct",
67
+ "messages" => [{
68
+ "id" => nil,
69
+ "sender" => "SMS",
70
+ "recipient" => "4900000000000",
71
+ "text" => "test",
72
+ "encoding" => "gsm",
73
+ "parts" => 1,
74
+ "price" => 0,
75
+ "success" => true,
76
+ "error" => nil,
77
+ "error_text" => nil
78
+ }]
79
+ } }
80
+ @checker.receive([@event])
81
+ end
82
+
83
+ it "should raise error on wrong credentials" do
84
+ unless is_live_test
85
+ stub(HTTParty).post { {"success" => "900"} }
86
+ end
87
+ opts = @default_options
88
+ opts[:api_key] = 'thisAintGonnaWork!'
89
+ expect { @checker.send_sms(opts.stringify_keys) }.to raise_error(StandardError, /SMS77_AUTH_ERROR:/)
90
+ end
91
+
92
+ it "should raise error on general dispatch error" do
93
+ unless is_live_test
94
+ stub(HTTParty).post { {"success" => "202"} }
95
+ end
96
+ opts = @default_options
97
+ opts[:to] = '0'
98
+ expect { @checker.send_sms(opts.stringify_keys) }.to raise_error(StandardError, /SMS77_DISPATCH_ERROR:/)
99
+ end
100
+
101
+ it "should WORK!" do
102
+ unless is_live_test
103
+ stub(HTTParty).post { "100" }
104
+ end
105
+
106
+ expect(@checker.send_sms(@default_options.stringify_keys)).to eq(100)
107
+ end
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_sms77_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sms77 e.K.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Send SMS & Text2Voice messages from Huginn via https://sms77.io.
56
+ email:
57
+ - support@sms77.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - lib/huginn_sms77_agent.rb
64
+ - lib/huginn_sms77_agent/sms77_agent.rb
65
+ - spec/sms77_agent_spec.rb
66
+ homepage: https://github.com/sms77io/huginn
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.7
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Send SMS & Text2Voice messages from Huginn via https://sms77.io.
89
+ test_files:
90
+ - spec/sms77_agent_spec.rb