-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (65 loc) · 2.29 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
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require("express")
const cp = require("child_process")
const TelegramBot = require("node-telegram-bot-api")
const bodyParser = require("body-parser")
const fetch = require("node-fetch")
const KB = require("./utils/inline-keyboard-generator.js")
const QProcessor = require("./utils/query-processor.js")
const MProcessor = require("./utils/message-processor.js")
const PORT = process.env.PORT || 5000
const TOKEN = process.env.TOKEN || "0"
const HOOK_URL = process.env.DOMAIN_URL +"/" + TOKEN
console.log("updating ytdl")
let upd = cp.spawn("node",["./node_modules/youtube-dl/scripts/download.js"],{stdio:"pipe"})
upd.stdout.on("data", data => {console.log(data.toString());setup()})
console.log(PORT,TOKEN)
let bot = 0
let app = express()
app.use(bodyParser.json())
//Newest API stuff
bot.editMessageMedia = async function(chat_id,message_id, media_id){
let url = `https://api.telegram.org/bot${TOKEN}/editMessageMedia` +
`?chat_id=${chat_id}` +
`&message_id=${message_id}`+
"&media=" + JSON.stringify({type:"audio",media:media_id})
await fetch(url).catch(err => console.log(err))
//console.log(res)
bot.editMessageReplyMarkup(new KB().getDefault(),{chat_id:chat_id, message_id:message_id})
}
function setup(){
console.log("Setting up handlers")
if(!("BOT_FORCE_POLLING" in process.env && process.env.BOT_FORCE_POLLING == 1)){
console.log("USING WEBHOOKS ON "+HOOK_URL)
bot = new TelegramBot(TOKEN)
bot.setWebHook(HOOK_URL,{allowed_updates:["message","callback_query"]})
}else{
console.log("USING LONG POLLING")
bot = new TelegramBot(TOKEN,{polling:true})
bot.deleteWebHook().then(val => console.log("webhook killed:",val))
bot.on("polling_error", (error) => {
console.log(error)
})
}
bot.on("message", async mes =>{
new MProcessor().do(mes, bot)
})
bot.on("callback_query",mes=>{
//console.log("query "+JSON.stringify(mes))
QProcessor.new(bot,mes)
bot.answerCallbackQuery(mes.id)
})
app.all("/",(req,res)=>{
res.end("nothing to see here")
})
.all(`/${TOKEN}`,(req,res)=>{
res.end("ok")
//console.log(req.body)
if("message" in req.body){
new MProcessor().do(req.body.message, bot)
}else if("callback_query" in req.body){
QProcessor.new(bot,req.body.callback_query)
bot.answerCallbackQuery(req.body.callback_query.id)
}
})
.listen(PORT)
}