dvelp_flow 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 836f240075b434f86ab36e9c5aa5d1f4d3ffc970
4
+ data.tar.gz: a999ef0637a34443da410e23165861a18bf1e33b
5
+ SHA512:
6
+ metadata.gz: c60010ad9ab529da8a0d64337fbcbab3972573729fd6206d22f0e825180b725bdcb031ddf62519c1401df7d1d46152e80370d0585f03b717831c9847e4381742
7
+ data.tar.gz: 2b8927fe269ca09bbe8045de0cc30890539aa4c64d5fd905d0fa9dc80751a54ff22c6e0ce652ce568e5f6315aa361908adcee6997ff712e6933c5566c0ed676c
@@ -0,0 +1,7 @@
1
+ database:
2
+ override:
3
+ - echo "no database setup"
4
+
5
+ test:
6
+ post:
7
+ - bundle exec rubocop
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # byebug history
14
+ .byebug_history
15
+
16
+ # DS_Store
17
+ .Ds_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,68 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/*'
4
+ TargetRubyVersion: 2.5
5
+
6
+ Layout/AlignParameters:
7
+ EnforcedStyle: with_fixed_indentation
8
+
9
+ Layout/ExtraSpacing:
10
+ Exclude:
11
+ - 'bin/*'
12
+
13
+ Layout/FirstParameterIndentation:
14
+ EnforcedStyle: consistent
15
+
16
+ Layout/IndentArray:
17
+ EnforcedStyle: consistent
18
+
19
+ Layout/MultilineMethodCallIndentation:
20
+ EnforcedStyle: indented
21
+
22
+ Metrics/BlockLength:
23
+ Exclude:
24
+ - 'text_to_speech_api_client.gemspec'
25
+ - 'spec/**/*'
26
+
27
+ Metrics/LineLength:
28
+ Max: 80
29
+
30
+ Metrics/MethodLength:
31
+ Enabled: false
32
+
33
+ Rails:
34
+ Enabled: true
35
+
36
+ Style/BlockComments:
37
+ Exclude:
38
+ - 'spec/spec_helper.rb'
39
+
40
+ Style/Documentation:
41
+ Enabled: false
42
+
43
+ Style/GuardClause:
44
+ Enabled: false
45
+
46
+ Style/HashSyntax:
47
+ Exclude:
48
+ - 'Rakefile'
49
+ - 'lib/tasks/*.rake'
50
+
51
+ Style/IfUnlessModifier:
52
+ Enabled: false
53
+
54
+ Style/PercentLiteralDelimiters:
55
+ PreferredDelimiters:
56
+ '%': ()
57
+ '%i': '[]'
58
+ '%q': ()
59
+ '%Q': ()
60
+ '%r': '{}'
61
+ '%s': ()
62
+ '%w': '[]'
63
+ '%W': '[]'
64
+ '%x': ()
65
+
66
+ Style/StringLiterals:
67
+ Exclude:
68
+ - 'bin/*'
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tom@dvelp.co.uk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in dvelp_flow.gemspec
6
+ gemspec
@@ -0,0 +1,122 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dvelp_flow (0.1.0)
5
+ activesupport
6
+ api-ai-ruby (~> 2.1.0)
7
+ googleauth (~> 0.6.2)
8
+ rest-client (~> 2.0.2)
9
+ twilio-ruby (~> 5.2.3)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (5.2.0)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 0.7, < 2)
17
+ minitest (~> 5.1)
18
+ tzinfo (~> 1.1)
19
+ addressable (2.5.2)
20
+ public_suffix (>= 2.0.2, < 4.0)
21
+ api-ai-ruby (2.1.0)
22
+ http (~> 2.0)
23
+ byebug (10.0.2)
24
+ concurrent-ruby (1.0.5)
25
+ crack (0.4.3)
26
+ safe_yaml (~> 1.0.0)
27
+ diff-lcs (1.3)
28
+ domain_name (0.5.20180417)
29
+ unf (>= 0.0.5, < 1.0.0)
30
+ faraday (0.15.0)
31
+ multipart-post (>= 1.2, < 3)
32
+ googleauth (0.6.2)
33
+ faraday (~> 0.12)
34
+ jwt (>= 1.4, < 3.0)
35
+ logging (~> 2.0)
36
+ memoist (~> 0.12)
37
+ multi_json (~> 1.11)
38
+ os (~> 0.9)
39
+ signet (~> 0.7)
40
+ hashdiff (0.3.7)
41
+ http (2.2.2)
42
+ addressable (~> 2.3)
43
+ http-cookie (~> 1.0)
44
+ http-form_data (~> 1.0.1)
45
+ http_parser.rb (~> 0.6.0)
46
+ http-cookie (1.0.3)
47
+ domain_name (~> 0.5)
48
+ http-form_data (1.0.3)
49
+ http_parser.rb (0.6.0)
50
+ i18n (1.0.1)
51
+ concurrent-ruby (~> 1.0)
52
+ jwt (1.5.6)
53
+ libxml-ruby (3.1.0)
54
+ little-plugger (1.1.4)
55
+ logging (2.2.2)
56
+ little-plugger (~> 1.1)
57
+ multi_json (~> 1.10)
58
+ memoist (0.16.0)
59
+ mime-types (3.1)
60
+ mime-types-data (~> 3.2015)
61
+ mime-types-data (3.2016.0521)
62
+ minitest (5.11.3)
63
+ multi_json (1.13.1)
64
+ multipart-post (2.0.0)
65
+ netrc (0.11.0)
66
+ os (0.9.6)
67
+ public_suffix (3.0.2)
68
+ rake (10.5.0)
69
+ rest-client (2.0.2)
70
+ http-cookie (>= 1.0.2, < 2.0)
71
+ mime-types (>= 1.16, < 4.0)
72
+ netrc (~> 0.8)
73
+ rspec (3.7.0)
74
+ rspec-core (~> 3.7.0)
75
+ rspec-expectations (~> 3.7.0)
76
+ rspec-mocks (~> 3.7.0)
77
+ rspec-core (3.7.1)
78
+ rspec-support (~> 3.7.0)
79
+ rspec-expectations (3.7.0)
80
+ diff-lcs (>= 1.2.0, < 2.0)
81
+ rspec-support (~> 3.7.0)
82
+ rspec-mocks (3.7.0)
83
+ diff-lcs (>= 1.2.0, < 2.0)
84
+ rspec-support (~> 3.7.0)
85
+ rspec-support (3.7.1)
86
+ safe_yaml (1.0.4)
87
+ shoulda-matchers (3.1.2)
88
+ activesupport (>= 4.0.0)
89
+ signet (0.8.1)
90
+ addressable (~> 2.3)
91
+ faraday (~> 0.9)
92
+ jwt (>= 1.5, < 3.0)
93
+ multi_json (~> 1.10)
94
+ thread_safe (0.3.6)
95
+ twilio-ruby (5.2.3)
96
+ faraday (~> 0.9)
97
+ jwt (~> 1.5)
98
+ libxml-ruby (>= 2.0, < 4.0)
99
+ tzinfo (1.2.5)
100
+ thread_safe (~> 0.1)
101
+ unf (0.1.4)
102
+ unf_ext
103
+ unf_ext (0.0.7.5)
104
+ webmock (3.3.0)
105
+ addressable (>= 2.3.6)
106
+ crack (>= 0.3.2)
107
+ hashdiff
108
+
109
+ PLATFORMS
110
+ ruby
111
+
112
+ DEPENDENCIES
113
+ bundler (~> 1.16)
114
+ byebug
115
+ dvelp_flow!
116
+ rake (~> 10.0)
117
+ rspec (~> 3.0)
118
+ shoulda-matchers (~> 3.1, >= 3.1.1)
119
+ webmock (~> 3.3.0)
120
+
121
+ BUNDLED WITH
122
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 tom-mullen
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # DVELP Flow
2
+
3
+ DVELP Flow is designed to facilitate omni-channel conversations with DialogFlow.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dvelp_flow'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dvelp_flow
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rake` to run the automated test suite.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`.
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DVELP/front_man. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
40
+ <br></br>
41
+ [![DVELP logo](https://raw.githubusercontent.com/DVELP/cookbook/master/assets/dvelp-logo.png 'DVELP logo')](http://dvelp.co.uk)
42
+
43
+ DVELP Flow was created and is maintained by DVELP Ltd.
44
+
45
+ If you like what you see and would like to hire us or join us, [get in touch](http://dvelp.co.uk)!
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dvelp_flow"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'dvelp_flow/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'dvelp_flow'
9
+ spec.version = DvelpFlow::VERSION
10
+ spec.authors = ['tom-mullen']
11
+ spec.email = ['tom@dvelp.co.uk']
12
+
13
+ spec.summary = %q{Helper library to build out communication on top of DialogFlow}
14
+ spec.description = %q{Build conversations across multiple channels on top of dialogflow}
15
+ spec.homepage = 'https://dvelp.co.uk'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activesupport'
26
+ spec.add_dependency 'api-ai-ruby', '~> 2.1.0'
27
+ spec.add_dependency 'googleauth', '~> 0.6.2'
28
+ spec.add_dependency 'rest-client', '~> 2.0.2'
29
+ spec.add_dependency 'twilio-ruby', '~> 5.2.3'
30
+
31
+ spec.add_development_dependency 'bundler', '~> 1.16'
32
+ spec.add_development_dependency 'byebug'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
+ spec.add_development_dependency 'shoulda-matchers', '~> 3.1', '>= 3.1.1'
36
+ spec.add_development_dependency 'webmock', '~> 3.3.0'
37
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dvelp_flow/version'
4
+ require 'api-ai-ruby'
5
+ require 'active_support'
6
+ require 'googleauth'
7
+ require 'rest-client'
8
+ require 'twilio-ruby'
9
+
10
+ Gem.find_files('dvelp_flow/**/*.rb').each { |f| require f }
11
+
12
+ module DvelpFlow
13
+ DialogFlow::VERSION = ENV['DIALOGFLOW_VERSION'] || 2
14
+
15
+ def cache
16
+ @cache ||= ActiveSupport::Cache::MemoryStore.new
17
+ end
18
+ module_function :cache
19
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Channels
5
+ class Base
6
+ def initialize(params: {}, responder:)
7
+ self.content = params[:content]
8
+ self.params = params
9
+ self.responder = responder
10
+ end
11
+
12
+ def communicate
13
+ response.final? ? reply_and_connect : reply_and_gather
14
+ end
15
+
16
+ private
17
+
18
+ attr_accessor :content, :params, :responder
19
+
20
+ def reply_and_connect
21
+ voice_response.say(content) if content.present?
22
+
23
+ enqueue_task
24
+ end
25
+
26
+ def enqueue_task
27
+ Twilio::TaskRouter::Task.create(task_attributes)
28
+ end
29
+
30
+ def task_attributes
31
+ { workflow_sid: workflow_sid }
32
+ end
33
+
34
+ def reply_and_gather
35
+ content
36
+ end
37
+
38
+ def response
39
+ responder_params = {
40
+ utterance: content,
41
+ session_uuid: params[:session_uuid]
42
+ }
43
+
44
+ responder.new(params: responder_params)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Channels
5
+ class Chat < Base
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Channels
5
+ class Voice < Base
6
+ def communicate
7
+ response.final? ? reply_and_connect : reply_and_gather
8
+ end
9
+
10
+ private
11
+
12
+ def reply_and_connect
13
+ voice_response.say(content) if content.present?
14
+
15
+ enqueue_task
16
+ end
17
+
18
+ def enqueue_task
19
+ voice_response.enqueue(enqueue_params) do |e|
20
+ e.task task_attributes
21
+ end.to_s
22
+ end
23
+
24
+ def enqueue_params
25
+ {
26
+ workflow_sid: params[:workflow_sid]
27
+ }
28
+ end
29
+
30
+ def task_attributes
31
+ {}
32
+ end
33
+
34
+ def reply_and_gather
35
+ ::Twilio::TwiML::VoiceResponse.new do |r|
36
+ r.gather(gather_attrs) do |g|
37
+ g.say content
38
+ g.pause length: ENV.fetch('TWILIO_GATHER_PAUSE', 0).to_i
39
+ end
40
+
41
+ r.redirect(params[:gathers_repeat_url], method: :post)
42
+ end.to_s
43
+ end
44
+
45
+ def gather_attrs
46
+ {
47
+ action: params[:gathers_url],
48
+ hints: ENV['ASR_HINTS'],
49
+ input: 'speech',
50
+ language: ENV['ASR_LANGUAGE'],
51
+ method: 'POST',
52
+ profanity_filter: true,
53
+ speech_timeout: 'auto'
54
+ }
55
+ end
56
+
57
+ def voice_response
58
+ @voice_response ||= Twilio::TwiML::VoiceResponse.new
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module DialogFlow
5
+ module Client
6
+ def init(access_token:, api_session_id:, api_version: DialogFlow::VERSION)
7
+ klass = Object.const_get(
8
+ "DvelpFlow::DialogFlow::V#{api_version}"
9
+ )
10
+
11
+ klass.new(
12
+ access_token: access_token,
13
+ api_session_id: api_session_id
14
+ )
15
+ end
16
+ module_function :init
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module DialogFlow
5
+ class V1
6
+ attr_accessor :client
7
+
8
+ def initialize(access_token:, api_session_id:)
9
+ self.client = ::ApiAiRuby::Client.new(
10
+ client_access_token: access_token,
11
+ api_lang: ENV['APIAI_LANGUAGE'],
12
+ api_session_id: api_session_id
13
+ )
14
+ end
15
+
16
+ def event_request
17
+ client.event_request
18
+ end
19
+
20
+ def text_request
21
+ client.text_request
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module DialogFlow
5
+ class V2
6
+ def initialize(access_token:, api_session_id:)
7
+ self.access_token = access_token
8
+ self.api_session_id = api_session_id
9
+ self.api_version = api_version
10
+ end
11
+
12
+ def event_request(event)
13
+ data = {
14
+ query_input: {
15
+ event: {
16
+ name: event,
17
+ language_code: ENV['APIAI_LANGUAGE']
18
+ }
19
+ }
20
+ }
21
+
22
+ call(data: data, endpoint: "sessions/#{api_session_id}:detectIntent")
23
+ end
24
+
25
+ def text_request(text)
26
+ data = {
27
+ query_input: {
28
+ text: {
29
+ text: text,
30
+ language_code: ENV['APIAI_LANGUAGE']
31
+ }
32
+ }
33
+ }
34
+
35
+ call(data: data, endpoint: "sessions/#{api_session_id}:detectIntent")
36
+ end
37
+
38
+ private
39
+
40
+ attr_accessor :access_token, :api_session_id, :api_version
41
+
42
+ def call(data:, endpoint:)
43
+ url = base_url + endpoint
44
+ response = RestClient.post(url, data.to_json, headers)
45
+
46
+ JSON.parse(response).deep_symbolize_keys!
47
+ end
48
+
49
+ def base_url
50
+ "https://dialogflow.googleapis.com/v2beta1/projects/#{project}/agent/"
51
+ end
52
+
53
+ def project
54
+ ENV['GCP_PROJECT_NAME']
55
+ end
56
+
57
+ def headers
58
+ {
59
+ accept: :json,
60
+ authorization: "Bearer #{access_token}",
61
+ content_type: :json
62
+ }
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Google
5
+ module Auth
6
+ module Token
7
+ def fetch!
8
+ cache_key = 'google/auth/token/fetch'
9
+ DvelpFlow.cache.fetch(cache_key, expires_in: 30.minutes) do
10
+ scope = 'https://www.googleapis.com/auth/cloud-platform'
11
+
12
+ keyfile = ENV.fetch('GOOGLE_CLOUD_KEYFILE_JSON', '')
13
+ authorizer = ::Google::Auth::ServiceAccountCredentials.make_creds(
14
+ json_key_io: StringIO.new(keyfile),
15
+ scope: scope
16
+ )
17
+ authorizer.fetch_access_token!['access_token']
18
+ end
19
+ end
20
+ module_function :fetch!
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Parsers
5
+ class Base
6
+ def initialize(response:)
7
+ self.response = response.symbolize_keys!
8
+ end
9
+
10
+ def intent_uuid
11
+ response[:queryResult][:intent][:name]
12
+ end
13
+
14
+ def intent
15
+ response[:queryResult][:intent][:displayName]
16
+ end
17
+
18
+ def parameters
19
+ response[:queryResult][:parameters]
20
+ end
21
+
22
+ def score
23
+ response[:queryResult][:intentDetectionConfidence] * 100
24
+ end
25
+
26
+ def speech
27
+ response[:queryResult][:fulfillmentText]
28
+ end
29
+
30
+ private
31
+
32
+ attr_accessor :response
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Parsers
5
+ class Error < Base
6
+ def initialize(response:)
7
+ self.response = response
8
+ end
9
+
10
+ def intent_uuid
11
+ ''
12
+ end
13
+
14
+ def intent
15
+ ''
16
+ end
17
+
18
+ def parameters
19
+ {}
20
+ end
21
+
22
+ def score
23
+ 0
24
+ end
25
+
26
+ def speech
27
+ "Error. #{response.message}"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Parsers
5
+ class V1 < Base
6
+ def intent_uuid
7
+ response[:result][:metadata][:intentId]
8
+ end
9
+
10
+ def intent
11
+ response[:result][:metadata][:intentName]
12
+ end
13
+
14
+ def parameters
15
+ response[:result][:parameters]
16
+ end
17
+
18
+ def score
19
+ response[:result][:score] * 100
20
+ end
21
+
22
+ def speech
23
+ response[:result][:speech]
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Parsers
5
+ class V2 < Base
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DvelpFlow
4
+ module Responders
5
+ class Base
6
+ def initialize(params: {})
7
+ self.content = params[:content]
8
+ self.session_uuid = params[:session_uuid]
9
+ end
10
+
11
+ def to_s
12
+ create_interaction
13
+ intent_parser.speech
14
+ end
15
+
16
+ def final?
17
+ !intent_parser.parameters.include? :continue
18
+ end
19
+
20
+ def content_chain
21
+ DvelpFlow.cache.read("contents/#{session_uuid}") || []
22
+ end
23
+
24
+ private
25
+
26
+ attr_accessor :content, :session_uuid
27
+
28
+ def create_interaction
29
+ DvelpFlow.cache.write(
30
+ "contents/#{session_uuid}",
31
+ content_chain << content,
32
+ expires_in: 5.minutes
33
+ )
34
+ end
35
+
36
+ def intent_parser
37
+ klass = Object.const_get(
38
+ "::DvelpFlow::Parsers::V#{DialogFlow::VERSION}"
39
+ )
40
+ @intent_parser ||= klass.new(
41
+ response: api_intent_response
42
+ )
43
+ end
44
+
45
+ def api_intent_response
46
+ intent_ai_client.text_request(content)
47
+ end
48
+
49
+ def intent_ai_client
50
+ @intent_ai_client ||= DvelpFlow::DialogFlow::Client.init(
51
+ access_token: DvelpFlow::Google::Auth::Token.fetch!,
52
+ api_session_id: session_uuid
53
+ )
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module DvelpFlow
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,231 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dvelp_flow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tom-mullen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: api-ai-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: googleauth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.6.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: twilio-ruby
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 5.2.3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 5.2.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.16'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.16'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: shoulda-matchers
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.1'
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: 3.1.1
149
+ type: :development
150
+ prerelease: false
151
+ version_requirements: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - "~>"
154
+ - !ruby/object:Gem::Version
155
+ version: '3.1'
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: 3.1.1
159
+ - !ruby/object:Gem::Dependency
160
+ name: webmock
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - "~>"
164
+ - !ruby/object:Gem::Version
165
+ version: 3.3.0
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - "~>"
171
+ - !ruby/object:Gem::Version
172
+ version: 3.3.0
173
+ description: Build conversations across multiple channels on top of dialogflow
174
+ email:
175
+ - tom@dvelp.co.uk
176
+ executables: []
177
+ extensions: []
178
+ extra_rdoc_files: []
179
+ files:
180
+ - ".circle.yml"
181
+ - ".gitignore"
182
+ - ".rspec"
183
+ - ".rubocop.yml"
184
+ - CODE_OF_CONDUCT.md
185
+ - Gemfile
186
+ - Gemfile.lock
187
+ - LICENSE.txt
188
+ - README.md
189
+ - Rakefile
190
+ - bin/console
191
+ - bin/setup
192
+ - dvelp_flow.gemspec
193
+ - lib/dvelp_flow.rb
194
+ - lib/dvelp_flow/channels/base.rb
195
+ - lib/dvelp_flow/channels/chat.rb
196
+ - lib/dvelp_flow/channels/voice.rb
197
+ - lib/dvelp_flow/dialog_flow/client.rb
198
+ - lib/dvelp_flow/dialog_flow/v1.rb
199
+ - lib/dvelp_flow/dialog_flow/v2.rb
200
+ - lib/dvelp_flow/google/auth/token.rb
201
+ - lib/dvelp_flow/parsers/base.rb
202
+ - lib/dvelp_flow/parsers/error.rb
203
+ - lib/dvelp_flow/parsers/v1.rb
204
+ - lib/dvelp_flow/parsers/v2.rb
205
+ - lib/dvelp_flow/responders/base.rb
206
+ - lib/dvelp_flow/version.rb
207
+ homepage: https://dvelp.co.uk
208
+ licenses:
209
+ - MIT
210
+ metadata: {}
211
+ post_install_message:
212
+ rdoc_options: []
213
+ require_paths:
214
+ - lib
215
+ required_ruby_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ required_rubygems_version: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ requirements: []
226
+ rubyforge_project:
227
+ rubygems_version: 2.6.14
228
+ signing_key:
229
+ specification_version: 4
230
+ summary: Helper library to build out communication on top of DialogFlow
231
+ test_files: []