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", "name": "cuffeo",
"version": "1.2.1", "version": "1.2.2",
"description": "A multi-protocol chatbot library with nearly zero dependencies.", "description": "A multi-protocol chatbot library with nearly zero dependencies.",
"main": "src/index.mjs", "main": "src/index.mjs",
"scripts": {}, "scripts": {},

View File

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