huginn_sms77_voice_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: dc21165cb6ae38eba981e7ac497e6465b58753a22f06f56b76e8be82ce2b0169
4
+ data.tar.gz: c9bdff3bf5c357c3dfda74d739718ee6962852739d976f43c87f9e05f5b3c7ef
5
+ SHA512:
6
+ metadata.gz: dce30e6c354861b7dd96cbbf192d8e662e1a55d152101d96713da75773c414acd19963e29369876e9ce7b15b4a385a66d493fc825bb6d8f86045bc7c1d13210c
7
+ data.tar.gz: 642b0fbc8f9326e2f51e7c9137c3a0317537f077d01360bc5e9d7a7a1728be4c9e5213ab881b057919ae301176f46990b2158ddf023df7d94ad20acaacf5f510
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,120 @@
1
+ require 'net/http'
2
+
3
+ module Agents
4
+ class Sms77VoiceAgent < Agent
5
+ include ActiveModel::Type
6
+ include FormConfigurable
7
+
8
+ cannot_be_scheduled!
9
+ cannot_create_events!
10
+ no_bulk_receive!
11
+
12
+ description <<-MD
13
+ The Sms77.io Agent receives and collects events for dispatching Text2Voice phone calls to a given number.
14
+
15
+ Expects an event with key `text`, `message`, `voice` or `sms` with the text to send. Use the `EventFormattingAgent` if lacks such a key.
16
+
17
+ Requires an API key from [Sms77](https://sms77.io) for sending.
18
+
19
+ Options:
20
+
21
+ * `text` - The message you want to send *(required)*
22
+
23
+ * `to` - recipient phone number *(required)*
24
+
25
+ * `from` - Sender number. *Expects a string with 11 alphanumeric or 16 numeric characters*
26
+
27
+ * `xml` - Decides whether the given text is XML or not *Allowed values: 0, 1*
28
+ MD
29
+
30
+ def required_options
31
+ {
32
+ 'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
33
+ 'text' => 'Hope to see you again soon!',
34
+ 'to' => '+4900000000000'
35
+ }
36
+ end
37
+
38
+ def optional_options
39
+ {
40
+ 'from' => 'Huginn',
41
+ 'xml' => false
42
+ }
43
+ end
44
+
45
+ def default_options
46
+ optional_options.merge(required_options)
47
+ end
48
+
49
+ form_configurable :api_key, type: :text
50
+ form_configurable :from, type: :text
51
+ form_configurable :text, type: :text
52
+ form_configurable :to, type: :text
53
+ form_configurable :xml, type: :boolean
54
+
55
+ def source_keys
56
+ %w[text message voice sms]
57
+ end
58
+
59
+ def validate_options
60
+ required_options.keys.each do |k|
61
+ val = options[k]
62
+ errors.add(:base, "#{k} is required") if val.nil? || val.empty?
63
+ end
64
+ end
65
+
66
+ def working?
67
+ interpolated['api_key'].present? && !recent_error_logs?
68
+ end
69
+
70
+ def receive(incoming_events)
71
+ interpolate_with_each(incoming_events) do |event|
72
+
73
+ source_keys.each do |k|
74
+ text = event.payload[k].presence.to_s
75
+
76
+ next unless text.present?
77
+
78
+ send_voice text
79
+ end
80
+ end
81
+ end
82
+
83
+ def send_voice(text)
84
+ fd = default_options
85
+
86
+ fd.delete('api_key')
87
+
88
+ fd.keys.each do |k|
89
+ val = interpolated[k]
90
+
91
+ if 'xml' === k
92
+ val = ActiveModel::Type::Boolean.new.cast(val) ? '1' : '0'
93
+ end
94
+
95
+ fd[k] = val
96
+ end
97
+
98
+ fd['text'] = text
99
+
100
+ log "Sending Text2Speech with payload #{fd.to_json}"
101
+
102
+ body = HTTParty.post('https://gateway.sms77.io/api/voice', :body => fd, :headers => {
103
+ 'Authorization' => "Basic #{interpolated['api_key']}",
104
+ 'sentWith' => "Huginn"
105
+ })
106
+
107
+ code = body.split(/\n+/)[0].to_i
108
+
109
+ if code === 100
110
+ log body
111
+ elsif code === 900
112
+ raise StandardError, "SMS77_AUTH_ERROR: #{body}"
113
+ else
114
+ raise StandardError, "SMS77_DISPATCH_ERROR: #{body}"
115
+ end
116
+
117
+ body
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,3 @@
1
+ require 'huginn_agent'
2
+
3
+ HuginnAgent.register 'huginn_sms77_voice_agent/sms77_voice_agent'
@@ -0,0 +1,95 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::Sms77VoiceAgent 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: 'Sms77VoiceAgentTest', 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
+ '100'\
63
+ '174206'\
64
+ '0.1'
65
+ }
66
+ @checker.receive([@event])
67
+ end
68
+
69
+ it "should raise error on wrong credentials" do
70
+ unless is_live_test
71
+ stub(HTTParty).post { { "success" => "900" } }
72
+ end
73
+ opts = @default_options
74
+ opts[:api_key] = 'thisAintGonnaWork!'
75
+ expect { @checker.send_voice(opts.stringify_keys) }.to raise_error(StandardError, /SMS77_AUTH_ERROR:/)
76
+ end
77
+
78
+ it "should raise error on general dispatch error" do
79
+ unless is_live_test
80
+ stub(HTTParty).post { { "success" => "202" } }
81
+ end
82
+ opts = @default_options
83
+ opts[:to] = '0'
84
+ expect { @checker.send_voice(opts.stringify_keys) }.to raise_error(StandardError, /SMS77_DISPATCH_ERROR:/)
85
+ end
86
+
87
+ it "should WORK!" do
88
+ unless is_live_test
89
+ stub(HTTParty).post { "100" }
90
+ end
91
+
92
+ expect(@checker.send_voice(@default_options.stringify_keys)).to eq(100)
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_sms77_voice_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 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_voice_agent.rb
64
+ - lib/huginn_sms77_voice_agent/sms77_voice_agent.rb
65
+ - spec/sms77_voice_agent_spec.rb
66
+ homepage: https://github.com/sms77io/huginn-voice
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 Text2Voice messages from Huginn via https://sms77.io.
89
+ test_files:
90
+ - spec/sms77_voice_agent_spec.rb