media support

This commit is contained in:
Flummi 2022-05-07 10:26:20 +02:00
parent 701ac51a4e
commit 678bc1192c

View File

@ -1,6 +1,8 @@
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch-cookies";
import EventEmitter from "events"; import EventEmitter from "events";
const allowedFiles = [ 'audio', 'video', 'photo', 'document' ];
export default class tg extends EventEmitter { export default class tg extends EventEmitter {
constructor(options) { constructor(options) {
super(); super();
@ -40,7 +42,7 @@ export default class tg extends EventEmitter {
id: res.result.id.toString() id: res.result.id.toString()
}; };
} }
async getPhoto(file_id) { async getFile(file_id) {
const res = await (await fetch(`${this.api}/getFile?file_id=${file_id}`)).json(); const res = await (await fetch(`${this.api}/getFile?file_id=${file_id}`)).json();
if(!res.ok) if(!res.ok)
return false; return false;
@ -50,10 +52,10 @@ export default class tg extends EventEmitter {
try { try {
let res = await (await fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)).json(); let res = await (await fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)).json();
if (!res.ok) if(!res.ok)
throw { type: "tg", message: res.description}; throw { type: "tg", message: res.description};
if (res.result.length === 0) if(res.result.length === 0)
throw { type: "generic", message: "empty response" }; return;
res = res.result[res.result.length - 1]; res = res.result[res.result.length - 1];
this.lastUpdate = res.update_id + 1; this.lastUpdate = res.update_id + 1;
@ -69,14 +71,19 @@ export default class tg extends EventEmitter {
id: res.message.from.id id: res.message.from.id
}); });
} }
if(res.message.photo) {
const photo_path = await this.getPhoto(res.message.photo[res.message.photo.length - 1].file_id); try {
let key;
if(key = Object.keys(res.message).filter(t => allowedFiles.includes(t))?.[0]) {
let media = res.message[key];
if(key === 'photo')
media = res.message[key][res.message[key].length - 1];
res.message.media = await this.getFile(media.file_id);
res.message.text = res.message.caption; res.message.text = res.message.caption;
res.message.photo = photo_path; delete res.message[key];
/*this.emit("data", ["photo", { }
photo: photo_path, } catch {
message: this.reply(res.message) // no media files
}]);*/
} }
this.emit("data", ["message", this.reply(res.message)]); this.emit("data", ["message", this.reply(res.message)]);