Class: Dashbot::DashbotSDK

Inherits:
Object
  • Object
show all
Defined in:
lib/dashbot.rb

Instance Method Summary collapse

Constructor Details

#initialize(apiKey, session = nil) ⇒ DashbotSDK

Returns a new instance of DashbotSDK.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dashbot.rb', line 10

def initialize(apiKey, session = nil)

	@apiKey = ''  
	@session = nil
	@debug = false
	@urlRoot = 'https://tracker.dashbot.io/track'
	@platform = 'alexa'
	@source = 'gem'
	@version = Dashbot::VERSION
	    
  if apiKey == nil or apiKey.length == 0
    puts "ERROR: invalid apiKey passed"
    return
  end

  if @session and @session['sessionId'] == session['sessionId']
    puts "Session Exists: " + @session['sessionId']
    return self
  end

  # Instance variables  
  @apiKey = apiKey  
  @session = session 

end

Instance Method Details

#generateResponse(speechText) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dashbot.rb', line 57

def generateResponse(speechText)
     if speechText[0,7]=='<speak>'
         return {
             response:{
                 outputSpeech:{
                     type:'SSML',
                     ssml: speechText
                 }
             }       
         }
     else
         return {
             response:{
                 outputSpeech:{
                     type:'Plaintext',
                     text:speechText
                 }
             }
         }
     end
end

#logIncoming(event, context = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dashbot.rb', line 147

def logIncoming(event, context = nil)
    url = @urlRoot + '?apiKey=' + @apiKey + '&type=incoming&platform='+ @platform + '&v=' + @version + '-' + @source
    
    if @debug
        puts 'Dashbot Incoming:'+url
        puts event
    end

 begin
 	event = JSON.parse(event)
 rescue
  event = event
		end

    data={
        event:event,
        context:context
        }
                    
    makeRequest(url,data)
end

#logOutgoing(event, response, context = nil) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dashbot.rb', line 169

def logOutgoing(event,response,context = nil)
    url = @urlRoot + '?apiKey=' + @apiKey + '&type=outgoing&platform='+ @platform + '&v=' + @version + '-' + @source
    
    if @debug
        puts 'Dashbot Outgoing:'+url
        puts event
        puts response
    end

 begin
 	event = JSON.parse(event)
 rescue
  event = event
		end

 begin
 	response = JSON.parse(response)
 rescue
  response = response
		end

    data={
        event:event,
        response:response,
        context:context
    }
    
    makeRequest(url,data)     
    
end

#makeRequest(sURL, payload) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dashbot.rb', line 132

def makeRequest(sURL,payload)
   begin
  response = RestClient::Request.execute(method: :post, 
                              url: sURL,
                              payload: payload.to_json, 
                              headers: {content_type: :json},
                              timeout: 1)
  
rescue Exception => e
  puts "Exception occurred: msg = " + e.message
  puts e.response.body
  puts e.backtrace.inspect
		end
end

#regenerateEvent(intent, slots) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dashbot.rb', line 36

def regenerateEvent(intent,slots)
 request = {
     type:'IntentRequest',
     intent: {
         name:intent,
         slots:slots
     }
 }
 event = {
     session: @session,
     request:request,
     context:{
         System:{
             application:@session['application'],
             user:@session['user']
         }
     }
 }
 return event
end

#track(intent_name, intent_request, response) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dashbot.rb', line 79

def track(intent_name, intent_request, response)  

  if(!intent_name)
      puts "ERROR: intent_name cannot be null"
      return
  end

  if @session == nil
      puts "ERROR: Dashbot SDK has not been initialized. Initalize() method need to have been invoked before tracking"
      return
  end
        
		event = regenerateEvent(intent_name,intent_request['intent']['slots'])

  begin
  	response = JSON.parse(response)
  rescue
   response = response
		end

  #set data
  if response.is_a? String
      speechText = response

  elsif response and response.key?('response') and response['response'].key?('outputSpeech')

      speechObj = response['response']['outputSpeech']

      if speechObj.key?('type')
          
          if speechObj['type'] == 'SSML'
              speechText = response['response']['outputSpeech']['ssml']

          elsif speechObj['type'] == 'PlainText'
              speechText = response['response']['outputSpeech']['text']

          else
              puts "ERROR: passed a response object with an unknown Type"
          end

      else
          puts "ERROR: passed a response object thats not an Alexa response"
      end

  else
      speechText = nil
  end
  
     responseGenerated = generateResponse(speechText)
     logIncoming(event)
     logOutgoing(event,responseGenerated)
end