-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleScriptExample.js
More file actions
130 lines (108 loc) · 3.6 KB
/
singleScriptExample.js
File metadata and controls
130 lines (108 loc) · 3.6 KB
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
const express = require('express');
const mongoose = require('mongoose');
const ConversationV1 = require('watson-developer-cloud/conversation/v1');
const app = express();
const mongoURI = process.env.MONGO_URI;
mongoose.connect(mongoURI);
const Order = require('./models/Order');
let contexts = [];
const order = {}; // Setup empty order
const sizeRx = RegExp('size_*', 'g'); // RegEx for size
app.get('/smssent', (req, res) => {
const message = req.query.Body; // Grabs the text message
const number = req.query.From; // Grabs the from cell number
const twilioNumber = req.query.To; // Grabs the Twilio cell number
let context = null; // Sets up the context of Watson convo
let index = 0;
let contextIndex = 0;
contexts.forEach(value => {
// console.log(value.from);
if (value.from == number) {
context = value.context;
contextIndex = index;
}
index = index + 1;
});
// You know, debugging stuff
console.log(`Recieved message from ${number} saying '${message}'`);
// Setup the Watson Converstion with your keys. Use .env file, duh!
const conversation = new ConversationV1({
username: process.env.WATSON_KEY,
password: process.env.WATSON_SECRET,
version_date: ConversationV1.VERSION_DATE_2016_09_20
});
console.log(JSON.stringify(context));
console.log(contexts.length);
conversation.message(
{
input: { text: message },
workspace_id: process.env.WATSON_WORKSPACE_ID,
context
},
(err, response) => {
if (err) {
console.error(err);
} else {
// console.log(response.output.text[0]);
if (context == null) {
order.convo_id = response.context.conversation_id; // attach convo id to the order
order.from = number; // attach the user's phone number
contexts.push({ from: number, context: response.context });
} else {
contexts[contextIndex].context = response.context;
}
const intent = response.intents[0].intent;
console.log(intent);
if (sizeRx.test(intent)) {
order.size = intent.replace('size_', ''); // Catpure selected size
console.log(`They picked a ${order.size} size.`);
}
if (intent == 'flavor') {
order.flavor = response.entities[0].value; // Catpure selected flavor
console.log(`They picked ${order.flavor} flavor.`);
}
// Cherries
if (
(intent == 'no' || intent == 'yes') &&
order.nuts &&
order.cherry == undefined
) {
order.cherry = intent;
console.log('Picking CHERRY! ', order.cherry);
}
// Nuts
if ((intent == 'no' || intent == 'yes') && order.nuts == undefined) {
order.nuts = intent;
console.log('Picking nuts! ', order.nuts);
}
if (intent == 'done') {
const context = contexts.splice(contextIndex, 1);
console.log('Complete! ', order);
const newOrder = new Order(order);
newOrder.save(); // SAVE TO MONGODB!
}
const client = require('twilio')(
process.env.TWILIO_SID,
process.env.TWILIO_TOKEN
);
client.messages.create(
{
from: twilioNumber,
to: number,
body: response.output.text[0]
},
function(err, message) {
if (err) {
console.error(err.message);
}
}
);
}
}
);
res.send('');
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Watson is listening on port ${PORT}!`);
});