-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (46 loc) · 1.37 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
70
71
const express = require('express')
const expressApp = express()
const path = require("path")
const fs = require('fs');
require('dotenv').config();
const port = process.env.PORT || 3003;
expressApp.use(express.static('static'))
expressApp.use(express.json());
const gTTs = require('gtts');
const {Telegraf} = require('telegraf');
const bot = new Telegraf(process.env.BOT_TOKEN);
expressApp.listen(port, function () {
console.log('Server listening on port ' + port);
})
expressApp.get("/", (req, res) => {
res.json({status:200,message:"Bot is online!"});
});
bot.launch()
bot.command('start', ctx => {
console.log(ctx.from)
return ctx.reply(
'Send me some text to get voice!',
)
})
bot.on('message', ctx => {
let text=ctx.message.text;
let dir = 'voices/'+ctx.from.username+'/';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
console.log(text);
let file=dir+getRandomFileName()+'.mp3';
const gtts = new gTTs(text, 'en');
gtts.save(file, function (err, result){
if(err) { throw new Error(err); }
ctx.replyWithVoice({
source: file
})
});
})
function getRandomFileName() {
let timestamp = new Date().toISOString().replace(/[-:.]/g,"");
let random = ("" + Math.random()).substring(2, 8);
let random_number = timestamp+random;
return random_number;
}