huginn_elks46_phone_call_agent 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2ff16588e5a704a5c691cf45047ebb08fff4f4fef8542838dcb69d1683564bb7
4
+ data.tar.gz: 16fe1ad492149eea01b3cd93635f1e8eb7acdd2b5cb1bbabce1e74eab3753f68
5
+ SHA512:
6
+ metadata.gz: 04a1782eb34efe434c6c2ccc43b3b1e1d47993c9fb936db73bb02d530d63a1d8c1d81dce12d897603923735c2ecf30338478d0b2e352752486bea7bcddca4c68
7
+ data.tar.gz: cbe87280f0320c3234f90b82a34909248f0b9bec260a86cc19386094ddaa7bf1622ca7b128479154862df5a6b5242a916581c763727b8d3e38ab0c324cb802f1
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_elks46_phone_call_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_elks46_phone_call_agent/elks46_phone_call_agent'
@@ -0,0 +1,90 @@
1
+ require 'net/http'
2
+
3
+ module Agents
4
+ class Elks46PhoneCallAgent < Agent
5
+ no_bulk_receive!
6
+ cannot_create_events!
7
+ cannot_be_scheduled!
8
+ description <<-MD
9
+ Integration that enables you to make phone calls from Huginn.
10
+ Define your API credentials (`api_username` and `api_password`) which you can find at your [46elks account](https://46elks.com).
11
+ Options:
12
+ * `from` - A valid phone number in [E.164 format](https://46elks.com/kb/e164). Can be one of your voice enabled 46elks numbers, the phone number you signed up with, or an unlocked number.
13
+
14
+ * `to` - The phone number of the recipient in [E.164 format](https://46elks.com/kb/e164).
15
+ * `voice_start` - A webhook URL that returns the first action to execute. See [Call actions](https://46elks.com/docs/call-actions) for details. It is also possible to add a JSON struct for direct call actions without any logic, like: {"connect":"+46766861004"}.
16
+ MD
17
+
18
+ # There is a form_configurable for extra options, this can be used instead of the default options.
19
+ # It's located in app/concerns/form_configurable.rb
20
+
21
+
22
+ def default_options
23
+ {
24
+ 'api_username' => 'u6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
25
+ 'api_password' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
26
+ 'from' => '+xxxxxxxxxxx',
27
+ 'to' => ['+46700000000'],
28
+ 'voice_start' => '{"play":"https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3"}',
29
+ }
30
+ end
31
+
32
+ def validate_options
33
+ # Should validate even more, especially the api keys
34
+ unless options['api_username'].present?
35
+ errors.add(:base, '`api_username` is required.')
36
+ end
37
+ unless options['api_password'].present?
38
+ errors.add(:base, '`api_password` is required.')
39
+ end
40
+ unless interpolated['from'].present?
41
+ errors.add(:base, '`from` is required.')
42
+ end
43
+ unless interpolated['to'].present?
44
+ errors.add(:base, '`to` is required.')
45
+ end
46
+ unless interpolated['voice_start'].present?
47
+ errors.add(:base, '`voice_start` is required.')
48
+ end
49
+ unless /{"(play|record|recordcall|ivr|hangup)":".*"}/.match(interpolated['voice_start'])
50
+ errors.add(:base, '`voice_start` needs to include a valid call action and value. See https://46elks.com/docs/call-actions for more information.')
51
+ end
52
+ end
53
+
54
+ def working?
55
+ !recent_error_logs?
56
+ end
57
+
58
+ def receive(incoming_events)
59
+ incoming_events.each do |event|
60
+ interpolate_with event do
61
+ make_phone_call(interpolated)
62
+ end
63
+ end
64
+ end
65
+
66
+ def make_phone_call(payload)
67
+ payload['to'].each do |to_recipient|
68
+ uri = URI('https://api.46elks.com/a1/calls')
69
+ req = Net::HTTP::Post.new(uri)
70
+ req.basic_auth options['api_username'], options['api_password']
71
+ req.set_form_data(
72
+ :from => payload['from'],
73
+ :to => to_recipient,
74
+ :voice_start => payload['voice_start']
75
+ )
76
+
77
+ res = Net::HTTP.start(
78
+ uri.host,
79
+ uri.port,
80
+ :use_ssl => uri.scheme == 'https') do |http|
81
+ http.request req
82
+ end
83
+
84
+ unless res.is_a?(Net::HTTPSuccess)
85
+ error("Error: #{res.body}")
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,92 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::Elks46PhoneCallAgent do
5
+ before do
6
+ default_options = {
7
+ 'api_username' => 'u6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
8
+ 'api_password' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
9
+ 'from' => '+46700000000',
10
+ 'to' => ['+46700000000'],
11
+ 'voice_start' => '{"hangup":"busy"}',
12
+ }
13
+
14
+ @checker = Agents::Elks46PhoneCallAgent.new name: 'elks46 Phone Call Tester', options: default_options
15
+ @checker.user = users(:bob)
16
+ @checker.save!
17
+ end
18
+
19
+ def event_with_payload(payload)
20
+ event = Event.new
21
+ event.agent = agents(:bob_manual_event_agent)
22
+ event.payload = payload
23
+ event.save!
24
+ event
25
+ end
26
+
27
+
28
+ def stub_methods
29
+ stub.any_instance_of(Agents::Elks46PhoneCallAgent).make_phone_call do |params|
30
+ @sent_messages << params
31
+ end
32
+ end
33
+
34
+ describe 'validation' do
35
+ before do
36
+ expect(@checker).to be_valid
37
+ end
38
+
39
+ it "should validate the presence of api_username" do
40
+ @checker.options[:api_username] = ''
41
+ expect(@checker).not_to be_valid
42
+ end
43
+
44
+ it "should validate the presence of api_password" do
45
+ @checker.options[:api_password] = ''
46
+ expect(@checker).not_to be_valid
47
+ end
48
+
49
+ it "should validate the presence of from" do
50
+ @checker.options[:from] = ''
51
+ expect(@checker).not_to be_valid
52
+ end
53
+
54
+ it "should validate the presence of to" do
55
+ @checker.options[:to] = ''
56
+ expect(@checker).not_to be_valid
57
+ end
58
+
59
+ it "should validate the presence of voice_start" do
60
+ @checker.options[:voice_start] = ''
61
+ expect(@checker).not_to be_valid
62
+ end
63
+ it "should validate that only valid keys are allowed" do
64
+ @checker.options[:voice_start] = '{"invalid":"busy"}'
65
+ expect(@checker).not_to be_valid
66
+ end
67
+ end
68
+
69
+ describe '#receive' do
70
+ before do
71
+ stub_methods
72
+ @sent_messages = []
73
+
74
+ expect(@checker).to be_valid
75
+ end
76
+ it "should receive event" do
77
+ event = event_with_payload from: "+46700000000", to: ["+46700000000"], voice_start: '{"hangup":"busy"}'
78
+ @checker.receive [event]
79
+
80
+ expect(@sent_messages).to eq([
81
+ {
82
+ "api_username": 'u6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
83
+ "api_password": 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
84
+ "from": '+46700000000',
85
+ "to": ["+46700000000"],
86
+ "voice_start": '{"hangup":"busy"}'
87
+ }.stringify_keys
88
+ ])
89
+ end
90
+
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_elks46_phone_call_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - antonmyr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-30 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:
56
+ email:
57
+ - anton@46elks.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/huginn_elks46_phone_call_agent.rb
63
+ - lib/huginn_elks46_phone_call_agent/elks46_phone_call_agent.rb
64
+ - spec/elks46_phone_call_agent_spec.rb
65
+ homepage: https://github.com/antonmyr/huginn_elks46_phone_call_agent
66
+ licenses: []
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.0.3
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Make phone calls with various features from Huginn using https://46elks.com.
87
+ test_files:
88
+ - spec/elks46_phone_call_agent_spec.rb