-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
209 lines (149 loc) · 4.44 KB
/
app.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
160
161
162
163
164
165
166
167
168
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
199
200
201
202
203
204
205
206
207
208
209
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var twilio = require('twilio');
var client = require('twilio')('AC8071f4e79c9ec1e28ac712b696634652', 'e13e174fbee885c77d5a039a1d372356');
var Canvas = require('canvas');
var fs = require('fs');
var request = require('request');
var app = express();
// all environments
app.set('port', process.env.PORT || 5000);
app.use(express.cookieParser('S3CRE7'));
app.use(express.cookieSession());
app.use(express.logger('dev'));
app.use(express.urlencoded());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
/**
* Receives GET request from Twilio with a message.
* Determines which route to use based off values stored in a session cookie.
*/
app.get('/tee-text', function(req, res) {
// Options
var colors = ['Green', 'Blue', 'Red', 'Yellow', 'Purple'];
var sizes = ['sml', 'med', 'lrg'];
var apiKey = '03ae700441d285b1937cda732868b74180d12fbd';
// Controller
if(!req.session.message)
askSize();
else if(!req.session.size)
askColor();
else if(!req.session.color)
askQuantity();
else
sendQuote();
// Routes
function askSize() {
req.session.message = req.query.Body;
sendMessage('What size tshirt? Options: sml, med, lrg');
}
function askColor() {
var size = req.query.Body;
size = size.toLowerCase();
// Check and see if the size is valid
if(sizes.indexOf(size) == -1)
sendMessage('Invalid size. Try again.');
else
{
req.session.size = size;
sendMessage('What color tshirt? Options: red, blue, yellow, purple, green');
}
}
function askQuantity() {
var color = req.query.Body;
color = color.toLowerCase();
color = color.charAt(0).toUpperCase() + color.slice(1);
// Check and see if the color is valid
if(colors.indexOf(color) == -1)
sendMessage('Invalid color. Try again.');
else
{
// Color fix
if(color == 'Blue')
color = 'Royal+Blue';
else if(color == 'Yellow')
color = 'Yellow+Haze';
else if(color == 'Green')
color = 'Turf+Green';
req.session.color = color;
sendMessage('How many tshirts do you want?');
}
}
function sendQuote() {
var quantity = req.query.Body;
quantity = parseInt(quantity, 10);
// Check and see if quantity is a number
if(!isInt(quantity))
sendMessage('Invalid quantity. Try again.');
else
{
var url = 'https://www.shirts.io/api/v1/quote/?api_key=' + apiKey + '&garment[0][product_id]=1&garment[0][color]=' + req.session.color + '&garment[0][sizes][' + req.session.size + ']=' + quantity + '&print[front][color_count]=1';
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
makeImage();
// Parse JSON
body = JSON.parse(body);
// Reset session
req.session = null;
sendMessage('Your total comes to $' + body.result.total + ' | Check out the design http://glacial-headland-8432.herokuapp.com/images/' + req.query.From + '.png');
}
else
{
console.log(error);
console.log(response.statusCode);
req.session = null;
sendMessage('Sorry. Something blew up. Please start over.');
}
});
}
}
// Helpers
function sendMessage(message) {
var resp = new twilio.TwimlResponse();
resp.message(message);
res.type('text/xml');
res.send(resp.toString());
}
function makeImage() {
// Canvas
var canvas = new Canvas(546, 596);
var ctx = canvas.getContext('2d');
// T-shirt image template
var Image = Canvas.Image;
var img = new Image();
img.onload = function(){
ctx.drawImage(img,0,0);
// T-shirt text
var message = req.session.message;
ctx.font = '30px Impact';
var te = ctx.measureText(message);
ctx.fillStyle = '#ffffff';
ctx.fillText(message, 273 - 0.5 * te.width, 198);
};
img.src = __dirname + '/public/images/' + req.session.color + '.png';
// Convert canvas to .png
var imageName = req.query.From + '.png';
var out = fs.createWriteStream(__dirname + '/public/images/' + imageName);
var stream = canvas.pngStream();
stream.on('data', function(chunk){
out.write(chunk);
});
stream.on('end', function(){
return;
});
}
function isInt(n) {
return typeof n === 'number' && n % 1 === 0;
}
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});