2020-03-13 22:02:41 +01:00
|
|
|
// @ts-check
|
2017-07-24 17:12:38 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-11-20 11:47:30 +05:30
|
|
|
const millisecond = require('millisecond');
|
2017-10-31 22:20:49 +01:00
|
|
|
const { telegram } = require('../bot');
|
|
|
|
|
2020-06-15 14:45:55 +02:00
|
|
|
const { html, lrm } = require('./html');
|
2018-05-20 12:07:12 +02:00
|
|
|
const R = require('ramda');
|
|
|
|
|
2020-02-19 14:29:05 +01:00
|
|
|
const replyId = R.path([ 'reply_to_message', 'message_id' ]);
|
|
|
|
|
2020-06-06 09:20:15 +02:00
|
|
|
const { isCommand } = require('../utils/cmd');
|
2018-05-20 12:07:12 +02:00
|
|
|
|
2020-02-19 08:11:32 +01:00
|
|
|
const inlineKeyboard = (...inline_keyboard) =>
|
|
|
|
({ reply_markup: { inline_keyboard } });
|
|
|
|
|
2019-04-06 15:39:40 +02:00
|
|
|
const msgLink = msg =>
|
2023-04-14 02:42:45 +02:00
|
|
|
msg.chat.username
|
|
|
|
? `https://t.me/${msg.chat.username}/${msg.message_id}`
|
|
|
|
: `https://t.me/c/${msg.chat.id.toString().slice(4)}/${msg.message_id}`;
|
2019-04-06 15:39:40 +02:00
|
|
|
|
2017-09-24 14:55:26 +02:00
|
|
|
const link = ({ id, first_name }) =>
|
2020-06-15 14:45:55 +02:00
|
|
|
html`${lrm}<a href="tg://user?id=${id}">${first_name}</a> [<code>${id}</code>]`;
|
2017-07-24 17:12:38 +02:00
|
|
|
|
2017-10-22 17:33:21 +02:00
|
|
|
const quietLink = (user) =>
|
|
|
|
user.username
|
2020-02-12 17:25:29 +01:00
|
|
|
? html`<a href="t.me/${user.username}">${user.first_name}</a>`
|
2020-05-18 20:43:28 +02:00
|
|
|
: html`<a href="tg://user?id=${user.id}">${user.first_name}</a>`;
|
2017-10-22 17:33:21 +02:00
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
const displayUser = user =>
|
|
|
|
user.first_name
|
|
|
|
? link(user)
|
2020-05-08 14:08:11 +02:00
|
|
|
: html`[<code>${user.id}</code>]`;
|
2019-01-28 21:46:29 +01:00
|
|
|
|
2020-03-13 22:02:41 +01:00
|
|
|
/** @param {number | string | false} ms */
|
2017-11-24 15:20:08 +01:00
|
|
|
const deleteAfter = ms => (ctx, next) => {
|
2019-07-06 10:10:41 -04:00
|
|
|
if (ms !== false) {
|
2021-05-07 22:46:11 +02:00
|
|
|
setTimeout(ctx.deleteMessage.bind(ctx), millisecond(ms));
|
2019-07-06 10:10:41 -04:00
|
|
|
}
|
2017-11-24 15:20:08 +01:00
|
|
|
next();
|
|
|
|
};
|
2017-07-24 18:08:31 +02:00
|
|
|
|
2020-03-13 22:02:41 +01:00
|
|
|
/** @param {number | string | false} ms */
|
2018-11-20 11:47:30 +05:30
|
|
|
const scheduleDeletion = (ms = 5 * 60 * 1000) => message => {
|
|
|
|
const { chat, message_id } = message;
|
|
|
|
|
|
|
|
if (chat.type !== 'private' && ms !== false) {
|
|
|
|
message.timeout = setTimeout(
|
|
|
|
() => telegram.deleteMessage(chat.id, message_id),
|
2020-05-08 14:08:11 +02:00
|
|
|
millisecond(ms),
|
2018-11-20 11:47:30 +05:30
|
|
|
);
|
2017-10-31 22:20:49 +01:00
|
|
|
}
|
2018-11-20 11:47:30 +05:30
|
|
|
|
|
|
|
return message;
|
2017-10-31 22:20:49 +01:00
|
|
|
};
|
|
|
|
|
2017-07-24 17:12:38 +02:00
|
|
|
module.exports = {
|
2017-07-24 18:08:31 +02:00
|
|
|
deleteAfter,
|
2019-01-28 21:46:29 +01:00
|
|
|
displayUser,
|
2020-02-19 08:11:32 +01:00
|
|
|
inlineKeyboard,
|
2018-05-20 12:07:12 +02:00
|
|
|
isCommand,
|
2017-10-22 17:33:21 +02:00
|
|
|
link,
|
2019-04-06 15:39:40 +02:00
|
|
|
msgLink,
|
2017-10-22 17:33:21 +02:00
|
|
|
quietLink,
|
2020-02-19 14:29:05 +01:00
|
|
|
replyId,
|
2017-10-31 22:20:49 +01:00
|
|
|
scheduleDeletion,
|
2017-07-24 17:12:38 +02:00
|
|
|
};
|