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 EventEmitter from "events";
const allowedFiles = [ 'audio', 'video', 'photo', 'document' ];
export default class tg extends EventEmitter {
constructor(options) {
super();
@ -40,7 +42,7 @@ export default class tg extends EventEmitter {
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();
if(!res.ok)
return false;
@ -50,10 +52,10 @@ export default class tg extends EventEmitter {
try {
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};
if (res.result.length === 0)
throw { type: "generic", message: "empty response" };
if(res.result.length === 0)
return;
res = res.result[res.result.length - 1];
this.lastUpdate = res.update_id + 1;
@ -69,14 +71,19 @@ export default class tg extends EventEmitter {
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.photo = photo_path;
/*this.emit("data", ["photo", {
photo: photo_path,
message: this.reply(res.message)
}]);*/
delete res.message[key];
}
} catch {
// no media files
}
this.emit("data", ["message", this.reply(res.message)]);