-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
82 lines (56 loc) · 1.96 KB
/
bot.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
require('dotenv').config()
const gTTS = require("gtts")
const { Bot } = require("grammy");
const { InputFile } = require("grammy");
const isValidURL = require("./utils/url_validator")
const BOT_TOKEN = process.env.BOT_TOKEN;
const bot = new Bot(BOT_TOKEN);
bot.command("start", (ctx) => {
ctx.reply("Hi, Thanks for starting me :)", {
reply_to_message_id: ctx.msg.message_id,
});
});
bot.command("tts",(ctx)=> {
const msg = ctx.msg.text;
//console.log(msg);
if (msg=="/tts"){
ctx.reply("Please enter a website url!");
}
else{
const url = msg.slice(5);
console.log(url);
if (isValidURL(url)== true){
ctx.reply("Processing... might take sometime", {
reply_to_message_id: ctx.msg.message_id,
});
//HANDLE THE TEXT
const Build = require('newspaperjs').Build;
const Article = require('newspaperjs').Article;
Article(url)
.then(result=>{
const title = result.title;
const text = result.text;
const wholeText = title + ".\n " + text;
//console.log(wholeText);
console.log('content scraped');
const gtts = new gTTS(wholeText, 'en');
const fileName = "audio" + ".mp3";
//TTS
gtts.save(fileName, function () {
console.log('Success! TTS...');
ctx.replyWithAudio(new InputFile(fileName), {
title: title,
performer : "Article Reader"
});
console.log("audio is being sent...");
});
})
.catch(reason=>{
//return false
console.log(reason);
ctx.reply(reason);
});
}
}
})
bot.start();