google_assistant 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 53b7d9eda7f3958b910ac571b3964ed93ca0f2f7
4
+ data.tar.gz: 6e3a8ca73808327162fa0fa842315128b3f45e84
5
+ SHA512:
6
+ metadata.gz: 31669e912fb57fb1be740489c75136f39a237d22655c63baf290335097014d8b8d6d7fc1d5bc66de4dd5e47186f5377996a24c0235ce5b0def123e346b6176af
7
+ data.tar.gz: 1ec9eb35c84bc48bff2f0167d934f9b2fece72d2986b236ea5538a58cdd77415e1db1e2579cfc52f154541d9d461b984af8e64314f1c9725ff8f9f56b7fbf364
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GoogleAssistant
4
+ class StandardIntents
5
+
6
+ # Assistant fires MAIN intent for queries like [talk to $action].
7
+ MAIN = "assistant.intent.action.MAIN"
8
+
9
+ # Assistant fires TEXT intent when action issues ask intent.
10
+ TEXT = "assistant.intent.action.TEXT"
11
+
12
+ # Assistant fires PERMISSION intent when action invokes askForPermission.
13
+ PERMISSION = "assistant.intent.action.PERMISSION"
14
+ end
15
+
16
+ class Intent
17
+ attr_reader :intent_string
18
+
19
+ def initialize(intent_string)
20
+ @intent_string = intent_string
21
+ end
22
+
23
+ def main(&block)
24
+ intents[StandardIntents::MAIN] = block
25
+ end
26
+
27
+ def text(&block)
28
+ intents[StandardIntents::TEXT] = block
29
+ end
30
+
31
+ def call
32
+ block = intents[intent_string]
33
+ return if block.nil?
34
+
35
+ block.call
36
+ end
37
+
38
+ private
39
+
40
+ def intents
41
+ @_intents ||= {}
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir["#{File.dirname(__FILE__)}/google_assistant/**/*.rb"].each { |file| require file }
4
+
5
+ class GoogleAssistant
6
+ attr_reader :params
7
+
8
+ def initialize(params)
9
+ @params = params
10
+ end
11
+
12
+ def respond_to(&block)
13
+ yield(self)
14
+
15
+ intent.call
16
+ end
17
+
18
+ def intent
19
+ @_intent ||= Intent.new(intent_string)
20
+ end
21
+
22
+ def tell(message)
23
+ final_response = { speech_response: {} }
24
+
25
+ if is_ssml(message)
26
+ final_response[:speech_response][:ssml] = message
27
+ else
28
+ final_response[:speech_response][:text_to_speech] = message
29
+ end
30
+
31
+ build_response(nil, false, nil, final_response)
32
+ end
33
+
34
+ private
35
+
36
+ def build_response(conversation_token, expect_user_response, expected_input, final_response)
37
+ response = {}
38
+
39
+ response[:conversation_token] = conversation_token if conversation_token
40
+ response[:expect_user_response] = expect_user_response
41
+ response[:expected_inputs] = expected_input if expected_input
42
+ response[:final_response] = final_response if !expect_user_response && final_response
43
+
44
+ {
45
+ json: response.as_json
46
+ }
47
+ end
48
+
49
+ def is_ssml(text)
50
+ if text.nil?
51
+ handle_error("Missing text")
52
+ return false
53
+ end
54
+
55
+ text =~ /^<speak\b[^>]*>(.*?)<\/speak>$/
56
+ end
57
+
58
+ def inputs
59
+ params["inputs"] || handle_error("Missing inputs from request body")
60
+ end
61
+
62
+ def intent_string
63
+ inputs[0]["intent"] || handle_error("Missing intent from request body")
64
+ end
65
+
66
+ def handle_error(message)
67
+ raise message
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_assistant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Milam
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby SDK for the Google Assistant API
14
+ email: armilam@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/google_assistant.rb
20
+ - lib/google_assistant/intent.rb
21
+ homepage: https://github.com/armilam/google-assistant-ruby
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Ruby SDK for the Google Assistant API
45
+ test_files: []