UNPKG

1.66 kBJavaScriptView Raw
1/*!
2 * apiai
3 * Copyright(c) 2015 http://api.ai/
4 * Apache 2.0 Licensed
5 */
6
7'use strict';
8
9var QueryRequest = require('./query_request').QueryRequest;
10var util = require('util');
11
12exports.VoiceRequest = module.exports.VoiceRequest = VoiceRequest;
13
14var CRLF = '\r\n';
15
16util.inherits(VoiceRequest, QueryRequest);
17
18function VoiceRequest (application, options) {
19 var self = this;
20 self.boundary = self._generateBoundary();
21
22 VoiceRequest.super_.apply(this, [application, options]);
23
24 self._sendMetaData();
25}
26
27VoiceRequest.prototype._generateBoundary = function() {
28 return (new Date()).getTime().toString();
29};
30
31VoiceRequest.prototype._headers = function() {
32 var self = this;
33
34 var headers = VoiceRequest.super_.prototype._headers.apply(this, arguments);
35
36 headers['Content-Type'] = 'multipart/form-data; boundary=' + self.boundary;
37 headers['Transfer-Encoding'] = 'chunked';
38
39 return headers;
40};
41
42VoiceRequest.prototype._sendMetaData = function() {
43 var self = this;
44
45 var data = '--' + self.boundary + CRLF;
46 data += 'Content-Disposition: form-data; name="request"' + CRLF;
47 data += "Content-Type: application/json" + CRLF + CRLF;
48
49 data += JSON.stringify(self._jsonRequestParameters());
50
51 data += CRLF + '--' + self.boundary + CRLF;
52 data += 'Content-Disposition: form-data; name="voiceData"' + CRLF;
53 data += 'Content-Type: audio/wav' + CRLF + CRLF;
54
55 self.write(data);
56};
57
58VoiceRequest.prototype.end = function() {
59 var self = this;
60
61 var lastDataChunk = CRLF + '--' + self.boundary + '--' + CRLF;
62
63 self.write(lastDataChunk);
64
65 VoiceRequest.super_.prototype.end.apply(this, arguments);
66};