forked from estacionckweb/Telegram2Icecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t2i.js
166 lines (142 loc) · 5.76 KB
/
t2i.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const TelegramBot = require('node-telegram-bot-api');
var request = require('request');
const fs = require('fs');
const download = require('download');
const { spawn } = require('child_process');
const ip = require('ip');
const serveIndex = require('serve-index');
require('./secret');
var url = "https://api.telegram.org/bot" + TOKEN + "/getFile?file_id=";
var urlFile = "https://api.telegram.org/file/bot" + TOKEN + "/";
var express = require('express')
var app = express()
var reproducidos = [];
var pendientes = [];
var sonando = false;
app.set('port', (process.env.PORT || 5000))
app.use(express.static(__dirname))
app.use('/video', serveIndex(__dirname + '/video'));
app.use('/audio', serveIndex(__dirname + '/audio'));
app.use('/texto', serveIndex(__dirname + '/texto'));
app.use('/foto', serveIndex(__dirname + '/foto'));
app.use('/documentos', serveIndex(__dirname + '/documentos'));
app.get('/', function(request, response) {
response.sendFile(__dirname + '/index.html');
})
app.listen(app.get('port'), function() {
console.log("corriendo en el puerto:" + app.get('port'))
})
const bot = new TelegramBot(TOKEN, {polling: true});
// Maximo de tamaño de 20 megas
bot.on('message', (msg) => {
const chatId = msg.chat.id;
let rawjson = fs.readFileSync('datos.json');
let parsedjson = JSON.parse(rawjson);
let obj = {
message_id: msg.message_id,
from_id: msg.from.id,
from_name: msg.from.first_name + ' ' + msg.from.last_name,
date: msg.date,
type: '',
file: ''
};
console.log(msg);
if(msg.voice != null){
obj.type = 'audio';
descargaMedia(msg.voice, 'audio', 'oga', 'audio descargado');
} else if(msg.photo != null){
obj.type = 'foto';
descargaMedia(msg.photo, 'foto', 'jpg', 'foto descargada');
} else if(msg.video != null){
obj.type = 'video';
descargaMedia(msg.video, 'video', 'mp4', 'video descargado');
} else if(msg.document != null){
descargaDocumento(msg.document, 'documentos', '');
} else if(msg.text != null){
obj.type = 'texto';
obj.file = msg.text;
fs.writeFileSync( darStringArchivo('texto', 'txt'), msg.text);
parsedjson.push(obj);
fs.writeFileSync('datos.json', JSON.stringify(parsedjson));
bot.sendMessage(chatId, 'ip local para revisar el historial: ' + ip.address() + ':' + app.get('port'));
}
function darStringArchivo(carpeta, ext) {
return carpeta + '/' + msg.from.id + '_' + msg.from.first_name + '_' + msg.date + '_' + msg.message_id + '.' + ext;
}
function descargaMedia(paquete, carpeta, ext, msj_confirmacion){
var newUrl;
if(carpeta == 'foto') newUrl = url + paquete[2].file_id;
else newUrl = url + paquete.file_id;
request.get({
url: newUrl,
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
var fileUrl = urlFile + data.result.file_path;
download(fileUrl).then(data => {
fs.writeFileSync(darStringArchivo(carpeta, ext), data);
if(carpeta == 'audio' && msg.voice.file_size < 57000){
pendientes.push('audio/off/' + darStringArchivo(carpeta, ext));
if(!sonando){
// reproducirStream();
}
bot.sendMessage(chatId, 'se fue al directorio audio');
}
obj.file = darStringArchivo(carpeta, ext);
parsedjson.push(obj);
fs.writeFileSync('datos.json', JSON.stringify(parsedjson));
bot.sendMessage(chatId, 'ip local para revisar el historial: ' + ip.address() + ':' + app.get('port'));
}).catch((err) => {console.log(err)});
}
});
}
function descargaDocumento(paquete, carpeta, msj_confirmacion){
var newUrl;
newUrl = url + paquete.file_id;
request.get({
url: newUrl,
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
var fileUrl = urlFile + data.result.file_path;
download(fileUrl).then(data => {
fs.writeFileSync(darStringArchivo(carpeta, paquete.file_name), data);
obj.file = darStringArchivo(carpeta, paquete.file_name);
parsedjson.push(obj);
fs.writeFileSync('datos.json', JSON.stringify(parsedjson));
bot.sendMessage(chatId, 'ip local para revisar el historial: ' + ip.address() + ':' + app.get('port'));
}).catch((err) => {console.log(err)});
}
});
}
});
function reproducirStream() {
if(pendientes.length > 0){
sonando = true;
var play = spawn('cvlc', ['--no-video', '--play-and-exit' , pendientes[0]]);
play.on('exit', function() {
reproducidos.push(pendientes[0]);
pendientes = pendientes.slice(1);
sonando = false;
reproducirStream();
});
} else if(reproducidos.length > 0){
sonando = true;
var random = Math.floor(Math.random() * reproducidos.length);
var play = spawn('cvlc', ['--no-video', '--play-and-exit' , reproducidos[random]]);
play.on('exit', function() {
sonando = false;
reproducirStream();
});
}
}