forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (100 loc) · 4.34 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
/*-----------------------------------------------------------------------------
A speech to text bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var builder = require('botbuilder'),
fs = require('fs'),
needle = require('needle'),
restify = require('restify'),
request = require('request'),
url = require('url'),
speechService = require('./speech-service.js');
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector, function (session) {
if (hasAudioAttachment(session)) {
var stream = getAudioStreamFromMessage(session.message);
speechService.getTextFromAudioStream(stream)
.then(function (text) {
session.send(processText(text));
})
.catch(function (error) {
session.send('Oops! Something went wrong. Try again later.');
console.error(error);
});
} else {
session.send('Did you upload an audio file? I\'m more of an audible person. Try sending me a wav file');
}
});
//=========================================================
// Utilities
//=========================================================
function hasAudioAttachment(session) {
return session.message.attachments.length > 0 &&
(session.message.attachments[0].contentType === 'audio/wav' ||
session.message.attachments[0].contentType === 'application/octet-stream');
}
function getAudioStreamFromMessage(message) {
var headers = {};
var attachment = message.attachments[0];
if (checkRequiresToken(message)) {
// The Skype attachment URLs are secured by JwtToken,
// you should set the JwtToken of your bot as the authorization header for the GET request your bot initiates to fetch the image.
// https://github.com/Microsoft/BotBuilder/issues/662
connector.getAccessToken(function (error, token) {
var tok = token;
headers['Authorization'] = 'Bearer ' + token;
headers['Content-Type'] = 'application/octet-stream';
return needle.get(attachment.contentUrl, { headers: headers });
});
}
headers['Content-Type'] = attachment.contentType;
return needle.get(attachment.contentUrl, { headers: headers });
}
function checkRequiresToken(message) {
return message.source === 'skype' || message.source === 'msteams';
}
function processText(text) {
var result = 'You said: ' + text + '.';
if (text && text.length > 0) {
var wordCount = text.split(' ').filter(function (x) { return x; }).length;
result += '\n\nWord Count: ' + wordCount;
var characterCount = text.replace(/ /g, '').length;
result += '\n\nCharacter Count: ' + characterCount;
var spaceCount = text.split(' ').length - 1;
result += '\n\nSpace Count: ' + spaceCount;
var m = text.match(/[aeiou]/gi);
var vowelCount = m === null ? 0 : m.length;
result += '\n\nVowel Count: ' + vowelCount;
}
return result;
}
//=========================================================
// Bots Events
//=========================================================
// Sends greeting message when the bot is first added to a conversation
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text('Hi! I am SpeechToText Bot. I can understand the content of any audio and convert it to text. Try sending me a wav file.');
bot.send(reply);
}
});
}
});