forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
135 lines (116 loc) · 4.93 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
/*-----------------------------------------------------------------------------
A speech to text bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
// This loads the environment variables from the .env file
require('dotenv-extended').load();
const builder = require("botbuilder"),
fs = require("fs"),
needle = require("needle"),
restify = require("restify"),
request = require("request"),
speechService = require("./speech-service.js"),
url = require("url");
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3979, () => {
console.log("%s listening to %s", server.name, server.url);
});
// Create chat bot
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
const bot = new builder.UniversalBot(connector);
server.post("/api/messages", connector.listen());
//=========================================================
// Bots Dialogs
//=========================================================
bot.dialog("/", session => {
if (hasAudioAttachment(session)) {
var stream = getAudioStreamFromAttachment(session.message.attachments[0]);
speechService.getTextFromAudioStream(stream)
.then(text => {
session.send(processText(session.message.text, text));
})
.catch(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
//=========================================================
const hasAudioAttachment = session => {
return ((session.message.attachments.length > 0) && ((session.message.attachments[0].contentType === "audio/wav") || (session.message.attachments[0].contentType === "application/octet-stream")));
}
const getAudioStreamFromAttachment = attachment => {
var headers = {};
if (isSkypeAttachment(attachment)) {
// 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((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 });
}
const isSkypeAttachment = attachment => {
if (url.parse(attachment.contentUrl).hostname.substr(-"skype.com".length) == "skype.com") {
return true;
}
return false;
}
const processText = (input, text) => {
var result = "You said: " + text + ".";
if (input.toUpperCase().trim() == "WORD") {
const wordCount = text.split(" ").filter(x => x).length;
result += " Word Count: " + wordCount;
}
else if (input.toUpperCase().trim() == "CHARACTER") {
const characterCount = text.replace(/ /g, "").length;
result += " Character Count: " + characterCount;
}
else if (input.toUpperCase().trim() == "SPACE") {
const spaceCount = (text.split(" ").length - 1);
result += " Space Count: " + spaceCount;
}
else if (input.toUpperCase().trim() == "VOWEL") {
const m = text.match(/[aeiou]/gi);
const vowelCount = m === null ? 0 : m.length;
result += " Vowel Count: " + vowelCount;
}
else if (input) {
const keywordCount = text.toUpperCase().split(" ").filter(x => x == input.toUpperCase()).length;
result += " Keyword " + input + " found " + keywordCount + " times.";
}
return result;
}
//=========================================================
// Bots Events
//=========================================================
// Sends greeting message when the bot is first added to a conversation
bot.on("conversationUpdate", message => {
if (message.membersAdded) {
message.membersAdded.forEach(identity => {
if (identity.id === message.address.bot.id) {
const 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);
return;
}
});
}
});