-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
69 lines (63 loc) · 2.19 KB
/
index.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
'use strict';
//Config
const config = require('./config');
const telegramToken = config.telegramToken;
const witToken = config.witToken;
const ffmpegPath = config.ffmpegPath;
const fs = require('fs');
const TelegramBot = require('node-telegram-bot-api');
const _ = require('lodash');
const chatSettings = require('./lib/chatSettings');
const ffmpeg = require('./lib/ffmpeg');
const speechToText = require('./lib/speechToText');
const commands = require('./lib/commands');
// Setup TelegramBot
const bot = new TelegramBot(telegramToken, { polling: true });
commands.commandEvent(bot, config.botName);
commands.setLanguage(bot, chatSettings, witToken);
commands.start(bot);
commands.stop(bot);
commands.help(bot);
chatSettings.load().then(() => {
bot.on('message', function (msg) {
if (!msg.voice) {
return;
}
bot.sendChatAction(msg.chat.id, 'typing');
bot.downloadFile(msg.voice.file_id, __dirname + '/tmp').then(filePath => {
const mp3Path = filePath + '.mp3'
return ffmpeg(ffmpegPath, ['-i ' + filePath, '-acodec libmp3lame', mp3Path]).then((stdout) => {
//TODO: Add callback & promisify
fs.unlink(filePath);
return mp3Path;
});
}).then(file => {
return speechToText(witToken, file, chatSettings.get(msg.chat.id).language).then(response => {
fs.unlink(file);
return response;
});
}).then(response => {
return response._text;
}).then(text => {
if(text === null || text === ""){
var transcribingErrorMessage = 'ArrArr ' + msg.from.first_name + ', ';
const language = chatSettings.get(msg.chat.id).language;
if(language === 'DE'){
transcribingErrorMessage += 'das kann doch keiner verstehen!'
}else if(language === 'EN'){
transcribingErrorMessage += 'nobody can understand this!'
}
bot.sendMessage(msg.chat.id, transcribingErrorMessage, {
reply_to_message_id: msg.message_id
});
return;
}
const message = msg.from.first_name + ': ' + text;
bot.sendMessage(msg.chat.id, message, {
reply_to_message_id: msg.message_id
});
}).catch(err => {
console.log('ERROR', err);
});
});
});