inline_query

This commit is contained in:
Flummi 2023-04-23 17:55:53 +02:00
parent 1713148f27
commit 1b77a21274
2 changed files with 62 additions and 37 deletions

View File

@ -1,6 +1,6 @@
{
"name": "cuffeo",
"version": "1.2.1",
"version": "1.2.2",
"description": "A multi-protocol chatbot library with nearly zero dependencies.",
"main": "src/index.mjs",
"scripts": {},

View File

@ -55,38 +55,49 @@ export default class tg extends EventEmitter {
if(!_res.ok)
throw { type: "tg", message: _res.description};
if(_res.result.length === 0)
return;
return
this.lastUpdate = _res.result[_res.result.length - 1].update_id + 1;
_res.result.forEach(async res => {
if(res.message?.date >= ~~(Date.now() / 1000) - 10 && res.message?.message_id !== this.lastMessage) {
this.lastMessage = res.message.message_id;
if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
this.server.user.set(res.message.from.username || res.message.from.first_name, {
nick: res.message.from.first_name,
username: res.message.from.username,
account: res.message.from.id.toString(),
prefix: `${res.message.from.username}!${res.message.from.id.toString()}@${this.network}`,
id: res.message.from.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;
delete res.message[key];
if(res.hasOwnProperty("message")) {
if(res.message?.date >= ~~(Date.now() / 1000) - 10 && res.message?.message_id !== this.lastMessage) {
this.lastMessage = res.message.message_id;
if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
this.server.user.set(res.message.from.username || res.message.from.first_name, {
nick: res.message.from.first_name,
username: res.message.from.username,
account: res.message.from.id.toString(),
prefix: `${res.message.from.username}!${res.message.from.id.toString()}@${this.network}`,
id: res.message.from.id
});
}
} catch {
// no media files
}
this.emit("data", ["message", this.reply(res.message)]);
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;
delete res.message[key];
}
} catch {
// no media files
}
this.emit("data", ["message", this.reply(res.message)]);
}
}
else if(res.hasOwnProperty("callback_query")) {
this.emit("data", ["callback_query", {
...res.callback_query,
editMessageText: this.editMessageText.bind(this)
}]);
}
else if(res.hasOwnProperty("inline_query")) {
this.emit("data", ["inline_query", res.inline_query]);
}
});
}
@ -103,7 +114,19 @@ export default class tg extends EventEmitter {
}, this.options.pollrate);
}
}
async send(chatid, msg, reply = null) {
async editMessageText(chat_id, message_id, text, opt = {}) {
const opts = {
method: "POST",
body: {
chat_id: chat_id,
message_id: message_id,
text: text,
...opt
}
};
await fetch(`${this.api}/editMessageText`, opts);
}
async send(chatid, msg, reply = null, opt = {}) {
msg = Array.isArray(msg) ? msg.join("\n") : msg;
if (msg.length === 0 || msg.length > 2048)
return this.emit("data", ["error", "msg to short or to long lol"]);
@ -112,7 +135,8 @@ export default class tg extends EventEmitter {
body: {
chat_id: chatid,
text: this.format(msg),
parse_mode: "HTML"
parse_mode: "HTML",
...opt
}
};
if (reply)
@ -138,8 +162,8 @@ export default class tg extends EventEmitter {
message: tmp.text,
time: tmp.date,
raw: tmp,
media: tmp.media || null,
reply: msg => this.send(tmp.chat.id, msg, tmp.message_id),
photo: tmp.media || null,
reply: (msg, opt = {}) => this.send(tmp.chat.id, msg, tmp.message_id, opt),
replyAction: msg => this.send(tmp.chat.id, `Uwe ${msg}`, tmp.message_id),
replyNotice: msg => this.send(tmp.chat.id, msg, tmp.message_id),
_user: this.server.user
@ -150,9 +174,10 @@ export default class tg extends EventEmitter {
.split("&").join("&")
.split("<").join("&lt;")
.split(">").join("&gt;")
.replace(/\[b\](.*?)\[\/b\]/g, "<b>$1</b>") // bold
.replace(/\[i\](.*?)\[\/i\]/g, "<i>$1</i>") // italic
.replace(/\[color=(.*?)](.*?)\[\/color\]/g, "$2")
.replace(/\[b\](.*?)\[\/b\]/gsm, "<b>$1</b>") // bold
.replace(/\[i\](.*?)\[\/i\]/gsm, "<i>$1</i>") // italic
.replace(/\[color=(.*?)](.*?)\[\/color\]/gsm, "$2")
.replace(/\[pre\](.*?)\[\/pre\]/gsm, "<pre>$1</pre>")
;
}
}