21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/dreaming_god/session.rb', line 21
def imagine request
@request = request
case @request.type
when 'LaunchRequest'
card = response_card 'Dreaming God'
ssml_response(speak('Greetings user. Tell me a fact for example, Cat is an animal.'), card, false)
when 'IntentRequest'
@intent = Alexagram::Intent.new(@request)
intent_name = @intent.name.underscore
case intent_name
when 'list_rows'
table_name = @intent.slots['table']['value'] if !table_name
result = "What is the name of the table that you want to list?"
card = response_card result
ssml_response(speak(result), card, false)
else
names = ""
result = "Db#{(table_name.capitalize.singularize)}".constantize.all.limit(3)
result.each do |r|
names << "#{r.label},"
end
names = "#{table_name} is empty" if names.length == 0
res = "Here is the list of #{table_name}, #{names}"
card = response_card res
ssml_response(speak(res), card, true)
end
when 'create_person'
result = ""
table_name = intent_name.split('_').last.pluralize
name = @intent.slots['personname']['value'] person = @intent.slots['person']['value'] if !name
result = "What was the name of the #{person}"
else
dbp = "Db#{(table_name.capitalize.singularize)}".constantize.new
dbp.label = name
dbp.people_type = person
dbp.save
result = "Ok. #{name} is a #{person}"
end
card = response_card result
ssml_response(speak("#{result}"), card, true)
when 'create_place'
result = ""
table_name = intent_name.split('_').last.pluralize
name = @intent.slots['placename']['value'] place = @intent.slots['place']['value'] if !name
result = "What was the name of the #{place}"
else
dbp = "Db#{(table_name.capitalize.singularize)}".constantize.new
dbp.label = name
dbp.place_type = place
dbp.save
result = "Ok. #{name} is a #{place}"
end
card = response_card result
ssml_response(speak("#{result}"), card, true)
when 'create_thing'
result = ""
table_name = intent_name.split('_').last.pluralize
name = @intent.slots['thingname']['value'] thing = @intent.slots['thing']['value'] if !name
result = "What was the name of the #{thing}"
else
dbp = "Db#{(table_name.capitalize.singularize)}".constantize.new
dbp.label = name
dbp.thing_type = thing
dbp.save
result = "Ok. #{name} is a #{thing}"
end
card = response_card result
ssml_response(speak("#{result}"), card, true)
when 'select_row'
column_a = @intent.slots['columna']['value'] case column_a
when 'thing type'
column_a = 'thing_type'
when 'person type'
column_a = 'person_type'
when 'place type'
column_a = 'place_type'
else
column_a = (column_a == 'all' ? '*' : "#{column_a}")
end
table_name = @intent.slots['table']['value'] column_b = @intent.slots['columnb']['value'] compare_value = @intent.slots['comparevalue']['value'] sql_string = "SELECT #{column_a} FROM db_#{table_name} WHERE #{column_b} = '#{compare_value}'"
result = "Db#{(table_name.capitalize.singularize)}".constantize.find_by_sql(sql_string)
names = ""
result.each do |r|
names << "#{r.send(column_a)}, "
end
names = "#{table_name} table is empty" if names.length == 0
card = response_card 'Dreaming God'
ssml_response(speak("I have found the following, #{names}"), card, true)
when 'update_row'
table_name = @intent.slots['table']['value'] column_a = @intent.slots['columna']['value'] value_a = @intent.slots['valuea']['value'] column_b = @intent.slots['columnb']['value'] compare_value = @intent.slots['comparevalue']['value']
case column_a
when 'thing type'
column_a = 'thing_type'
when 'person type'
column_a = 'person_type'
when 'place type'
column_a = 'place_type'
else
column_a = (column_a == 'all' ? '*' : "#{column_a}")
end
sql_string = "UPDATE db_#{table_name} SET #{column_a} = '#{value_a}' WHERE #{column_b} = '#{compare_value}'"
result = "Db#{(table_name.capitalize.singularize)}".constantize.find_by_sql(sql_string)
res = "I have updated the database where #{column_b} is equal to #{compare_value} with #{column_a} set to #{value_a}"
card = response_card res
ssml_response(speak(res), card, true)
when 'destroy_row'
table_name = @intent.slots['table']['value'] column = @intent.slots['column']['value'] compare_value = @intent.slots['comparevalue']['value']
case column
when 'thing type'
column = 'thing_type'
when 'person type'
column = 'person_type'
when 'place type'
column = 'place_type'
else
column = (column == 'all' ? '*' : "#{column}")
end
sql_string = "DELETE FROM db_#{table_name} where #{column} = '#{compare_value}'"
"Db#{(table_name.capitalize.singularize)}".constantize.find_by_sql(sql_string)
res = "I have destroyed rows from the database where #{column} is equal to #{compare_value}"
card = response_card res
ssml_response(speak(res), card, true)
else
end
when 'SessionEndedRequest'
DbSession.detroy(@db_session.id.to_i)
result = "End of line"
card = response_card result
ssml_response(speak(result), card, true)
else
end
end
|