Created
August 25, 2021 01:34
-
-
Save BlueSlimee/cfcbed06d0db97bbfa322db8ad603fdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('@lastgram/core') | |
const marked = require('marked') | |
const request = (res, method, data) => { | |
const payload = JSON.stringify({ method, ...data }) | |
res.setHeader('Content-Length', Buffer.byteLength(payload)) | |
res.setHeader('Content-Type', 'application/json') | |
res.send(payload) | |
} | |
const reply = (res, ctx, data, others = {}, replying = false) => { | |
let content = data.content || data | |
if (data.image) content = content.trim() + `[\u200B](${data.image})` | |
let z = {} | |
if (replying) z.reply_to_message_id = ctx.id | |
return request(res, 'sendMessage', { | |
parse_mode: 'HTML', | |
text: marked.parseInline(content).trim(), | |
chat_id: ctx.channel, | |
...others, | |
...(replying ? { reply_to_message_id: ctx.id } : {}) | |
}) | |
} | |
const replyInline = (res, results, id) => { | |
return request(res, 'answerInlineQuery', { | |
inline_query_id: id, | |
cache_time: 10, | |
results: results | |
}) | |
} | |
const parseText = (text) => { | |
if (!text.startsWith('/')) return [] | |
const [rawCmd, ...args] = text.trim().split(' ') | |
const command = (rawCmd.split('/')[1] || '').replace(process.env.LGSTABLE ? '@lastgramrobot' : '@lastgrameapbot', '').toLowerCase() | |
return [command, args] | |
} | |
const generateCtx = (m, args) => { | |
return { | |
args, | |
platform: 'telegram', | |
lang: m.from.language_code?.split?.('-')?.[0], | |
channel: m.chat.id, | |
mentionedUsers: [], | |
id: m.message_id, | |
replyingTo: m.reply_to_message?.from?.username | |
? { | |
content: m.reply_to_message.text, | |
user: { | |
id: m.reply_to_message.from.id, | |
name: m.reply_to_message.from.first_name | |
} | |
} | |
: undefined, | |
sender: { | |
name: m.from.first_name, | |
id: m.from.id, | |
isBot: m.from.is_bot | |
} | |
} | |
} | |
const execute = (commandName, ...args) => { | |
return __lgCore.getCommand(commandName)?.(...args) | |
} | |
module.exports = async (req, res) => { | |
if (req.query?.token !== process.env.TELEGRAM_TOKEN) return res.status(401).send('You thought this would work you hoe?') | |
if (process.env.BLACKLIST && process.env.BLACKLIST.split(',').includes(`${req.body?.message?.from?.id}`)) return res.status(200).json({}) | |
if (req.body.message || req.body.edited_message) { | |
const message = req.body.message || req.body.edited_message | |
if (message.from.is_bot || !message.text || !message.text.startsWith('/')) return res.status(200).json({}) | |
const [cmd, args] = parseText(message.text) | |
const ctx = generateCtx(message, args) | |
try { | |
const a = await execute(cmd, ctx) | |
if (a) return reply(res, ctx, a, {}, ['you', 'lnn', 'ltn', 'match', 'me', 'reg'].includes(cmd)) | |
} catch (e) { | |
return reply(res, ctx, 'Servidor de farelo, voltamos logo', true) | |
} | |
return res.status(200).json({}) | |
} else if (req.body.inline_query) { | |
const q = req.body.inline_query | |
try { | |
const rst = await execute('INTERNAL_query', | |
q.from.id, | |
q.from.first_name, | |
q.query.length > 1 ? q.query : null, | |
q.from.language_code?.split?.('-')?.[0] | |
) | |
rst?.map(z => { | |
let rendered = false | |
if (z.content) { | |
z.content = marked.parseInline(z.content).trim() | |
if (z.type === 'photo') { | |
z.caption = z.content | |
z.parse_mode = 'HTML' | |
} else { | |
z.input_message_content = { | |
message_text: z.content, | |
parse_mode: 'HTML' | |
} | |
} | |
delete z.content | |
} | |
return z | |
}) | |
return replyInline(res, rst || [], q.id) | |
} catch (e) {}} | |
return res.status(200).json({}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment