-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (153 loc) · 5.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
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
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
'use strict';
const Alexa = require('alexa-sdk');
const APP_ID = undefined;
const SKILL_NAME = 'Chemistry Teacher';
const HELP_MESSAGE = 'You can ask me for the formula or name of a chemical compound, or you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Thank you! Goodbye!';
const ERROR_MESSAGE = 'Sorry, I dont know that. What else can I help you with?'
const compounds = [
{
"name" : "copper sulfate",
"formula" : "CuSO4",
},
{
"name" : "sodium bicarbonate",
"formula" : "NaHCO3"
},
{
"name" : "nitric acid",
"formula" : "HNO3"
},
{
"name" : "sodium hydroxide",
"formula" : "NaOH"
},
{
"name" : "methanol",
"formula" : "CH3OH"
},
{
"name" : "methane",
"formula" : "CH4"
},
{
"name" : "benzene",
"formula" : "C6H6"
},
{
"name" : "glucose",
"formula" : "C6H12O6"
},
{
"name" : "ammonia",
"formula" : "NH3"
},
{
"name" : "ethanol",
"formula" : "C2H5OH"
},
{
"name" : "sodium chloride",
"formula" : "NaCl"
},
{
"name" : "acetic acid",
"formula" : "CH3COOH"
},
{
"name" : "phosphoric acid",
"formula" : "H3PO4"
},
{
"name" : "carbon dioxide",
"formula" : "CO2"
},
{
"name" : "sulfuric acid",
"formula" : "H2SO4"
},
{
"name" : "hydrochloric acid",
"formula" : "HCl"
},
{
"name" : "water",
"formula" : "H2O"
},
{
"name" : "barium sulfate",
"formula" : "BaSO4"
},
];
const handlers = {
'LaunchRequest': function () {
this.response.speak('Hello there! <amazon:effect name="whispered">Chemical compounds sound scary?</amazon:effect> Don\'t worry, I am here to help. I can tell you the ' +
'name and formula of compounds. What do you want to know?').listen('What do you want?');
this.emit(':responseReady');
},
'ChemicalFormulaIntent': function () {
var chemName = null;
if(this.event.request.intent.slots.ChemicalCompoundName.resolutions.resolutionsPerAuthority[0].status.code == 'ER_SUCCESS_MATCH')
chemName = this.event.request.intent.slots.ChemicalCompoundName.resolutions.resolutionsPerAuthority[0].values[0].value.name;
var formula = null;
for(var i = 0; i < compounds.length; i++) {
if (compounds[i].name == chemName)
formula = compounds[i].formula;
}
if(formula != null)
this.response.speak('The formula of ' + chemName + ' is ' + '<say-as interpret-as = "characters">' + formula + '</say-as>. What ' +
'else can I help you with?').listen(HELP_REPROMPT);
else
this.response.speak('Sorry, I dont know this. What else can I help you with?').listen(HELP_REPROMPT);
this.emit(':responseReady');
},
'ChemicalNameIntent' : function() {
var chemFormula = null;
if(this.event.request.intent.slots.ChemicalFormula.resolutions.resolutionsPerAuthority[0].status.code == 'ER_SUCCESS_MATCH')
chemFormula = this.event.request.intent.slots.ChemicalFormula.resolutions.resolutionsPerAuthority[0].values[0].value.name;
var name = null;
for(var i = 0; i < compounds.length; i++) {
if(compounds[i].formula == chemFormula)
name = compounds[i].name;
}
if(name != null)
this.response.speak('The name of ' + '<say-as interpret-as = "characters">' + chemFormula + '</say-as>' + ' is ' + name +
'. What else can I help you with?').listen(HELP_REPROMPT);
else
this.response.speak('Sorry, I dont know this. What else can I help you with?').listen(HELP_REPROMPT);
this.emit(':responseReady');
},
'AMAZON.HelpIntent': function () {
const speechOutput = HELP_MESSAGE;
const reprompt = HELP_REPROMPT;
this.response.speak(speechOutput).listen(reprompt);
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
'AMAZON.StopIntent': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
'AMAZON.FallbackIntent': function () {
this.response.speak(ERROR_MESSAGE).listen(HELP_MESSAGE);
this.emit(':responseReady');
},
'Unhandled': function () {
this.response.speak(ERROR_MESSAGE).listen(HELP_MESSAGE);
this.emit(':responseReady');
},
'SessionEndedRequest': function () {
this.response.speak(STOP_MESSAGE);
this.emit(':responseReady');
},
};
exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};