mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Fix add annotations split
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sentry/node": "^6.16.1",
|
"@sentry/node": "^6.16.1",
|
||||||
|
"chunk-text": "^2.0.1",
|
||||||
"docker-ip-get": "^1.1.5",
|
"docker-ip-get": "^1.1.5",
|
||||||
"envalid": "^7.2.2",
|
"envalid": "^7.2.2",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
@@ -25,6 +26,7 @@
|
|||||||
"telegraf": "^4.4.2"
|
"telegraf": "^4.4.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/chunk-text": "^1.0.0",
|
||||||
"@types/express": "^4.17.13",
|
"@types/express": "^4.17.13",
|
||||||
"@types/node": "^16.11.9",
|
"@types/node": "^16.11.9",
|
||||||
"@types/safe-compare": "^1.1.0",
|
"@types/safe-compare": "^1.1.0",
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ export const AUTHOR_BOOKS_PREFIX = 'ba_';
|
|||||||
export const TRANSLATOR_BOOKS_PREFIX = 'bt_';
|
export const TRANSLATOR_BOOKS_PREFIX = 'bt_';
|
||||||
export const SEQUENCE_BOOKS_PREFIX = 'bs_';
|
export const SEQUENCE_BOOKS_PREFIX = 'bs_';
|
||||||
|
|
||||||
|
export const BOOK_ANNOTATION_PREFIX = 'a_info_';
|
||||||
|
export const AUTHOR_ANNOTATION_PREFIX = 'b_info_';
|
||||||
|
|
||||||
export const RANDOM_BOOK = 'random_book';
|
export const RANDOM_BOOK = 'random_book';
|
||||||
export const RANDOM_AUTHOR = 'random_author';
|
export const RANDOM_AUTHOR = 'random_author';
|
||||||
export const RANDOM_SEQUENCE = 'random_sequence';
|
export const RANDOM_SEQUENCE = 'random_sequence';
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import * as BookLibrary from "./services/book_library";
|
|||||||
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
import { createOrUpdateUserSettings, getUserSettings } from './services/user_settings';
|
||||||
import { formatBook, formatAuthor, formatSequence, formatTranslator } from './format';
|
import { formatBook, formatAuthor, formatSequence, formatTranslator } from './format';
|
||||||
import { getPaginatedMessage, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils';
|
import { getPaginatedMessage, registerLanguageSettingsCallback, registerPaginationCommand, registerRandomItemCallback } from './utils';
|
||||||
import { getRandomKeyboard, getUpdateLogKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
import { getRandomKeyboard, getTextPaginationData, getUpdateLogKeyboard, getUserAllowedLangsKeyboard } from './keyboard';
|
||||||
import { sendFile } from './hooks/downloading';
|
import { sendFile } from './hooks/downloading';
|
||||||
import { setCommands } from './hooks/setCommands';
|
import { setCommands } from './hooks/setCommands';
|
||||||
|
|
||||||
@@ -139,11 +139,34 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
|
|
||||||
const annotation = await BookLibrary.getBookAnnotation(parseInt(bookId));
|
const annotation = await BookLibrary.getBookAnnotation(parseInt(bookId));
|
||||||
|
|
||||||
ctx.reply(annotation.text, {
|
const data = getTextPaginationData(`${CallbackData.BOOK_ANNOTATION_PREFIX}${bookId}`, annotation.text, 0);
|
||||||
|
|
||||||
|
ctx.reply(data.current, {
|
||||||
parse_mode: "HTML",
|
parse_mode: "HTML",
|
||||||
|
reply_markup: data.keyboard.reply_markup,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
bot.action(new RegExp(CallbackData.BOOK_ANNOTATION_PREFIX), async (ctx: Context) => {
|
||||||
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
|
const queryData = ctx.callbackQuery.data.split("_");
|
||||||
|
|
||||||
|
const bookId = queryData[2];
|
||||||
|
const page = queryData[3];
|
||||||
|
|
||||||
|
const annotation = await BookLibrary.getBookAnnotation(parseInt(bookId));
|
||||||
|
|
||||||
|
const data = getTextPaginationData(`${CallbackData.BOOK_ANNOTATION_PREFIX}${bookId}`, annotation.text, parseInt(page));
|
||||||
|
|
||||||
|
ctx.editMessageText(
|
||||||
|
data.current, {
|
||||||
|
parse_mode: "HTML",
|
||||||
|
reply_markup: data.keyboard.reply_markup,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
bot.hears(/^\/a_info_[\d]+$/gm, async (ctx: Context) => {
|
bot.hears(/^\/a_info_[\d]+$/gm, async (ctx: Context) => {
|
||||||
if (!ctx.message || !('text' in ctx.message)) {
|
if (!ctx.message || !('text' in ctx.message)) {
|
||||||
return;
|
return;
|
||||||
@@ -153,11 +176,34 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
|
|
||||||
const annotation = await BookLibrary.getAuthorAnnotation(parseInt(authorId));
|
const annotation = await BookLibrary.getAuthorAnnotation(parseInt(authorId));
|
||||||
|
|
||||||
ctx.reply(annotation.text, {
|
const data = getTextPaginationData(`${CallbackData.AUTHOR_ANNOTATION_PREFIX}${authorId}`, annotation.text, 0);
|
||||||
|
|
||||||
|
ctx.reply(data.current, {
|
||||||
parse_mode: "HTML",
|
parse_mode: "HTML",
|
||||||
|
reply_markup: data.keyboard.reply_markup,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
bot.action(new RegExp(CallbackData.AUTHOR_ANNOTATION_PREFIX), async (ctx: Context) => {
|
||||||
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
|
const queryData = ctx.callbackQuery.data.split("_");
|
||||||
|
|
||||||
|
const authorId = queryData[2];
|
||||||
|
const page = queryData[3];
|
||||||
|
|
||||||
|
const annotation = await BookLibrary.getAuthorAnnotation(parseInt(authorId));
|
||||||
|
|
||||||
|
const data = getTextPaginationData(`${CallbackData.AUTHOR_ANNOTATION_PREFIX}${authorId}`, annotation.text, parseInt(page));
|
||||||
|
|
||||||
|
ctx.editMessageText(
|
||||||
|
data.current, {
|
||||||
|
parse_mode: "HTML",
|
||||||
|
reply_markup: data.keyboard.reply_markup,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
bot.hears(/^\/a_[\d]+$/gm, async (ctx: Context) => {
|
bot.hears(/^\/a_[\d]+$/gm, async (ctx: Context) => {
|
||||||
if (!ctx.message || !('text' in ctx.message)) {
|
if (!ctx.message || !('text' in ctx.message)) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Markup } from 'telegraf';
|
import { Markup } from 'telegraf';
|
||||||
import { InlineKeyboardMarkup } from 'typegram';
|
import { InlineKeyboardMarkup } from 'typegram';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
import chunkText from 'chunk-text';
|
||||||
|
|
||||||
import { RANDOM_BOOK, RANDOM_AUTHOR, RANDOM_SEQUENCE, ENABLE_LANG_PREFIX, DISABLE_LANG_PREFIX, UPDATE_LOG_PREFIX } from './callback_data';
|
import { RANDOM_BOOK, RANDOM_AUTHOR, RANDOM_SEQUENCE, ENABLE_LANG_PREFIX, DISABLE_LANG_PREFIX, UPDATE_LOG_PREFIX } from './callback_data';
|
||||||
import { getUserSettings, getLanguages } from './services/user_settings';
|
import { getUserSettings, getLanguages } from './services/user_settings';
|
||||||
@@ -45,6 +46,30 @@ export function getPaginationKeyboard(prefix: string, query: string | number, pa
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function getTextPaginationData(prefix: string, text: string, currentPage: number): {current: string, keyboard: Markup.Markup<InlineKeyboardMarkup>} {
|
||||||
|
const chunks = chunkText(text, 512);
|
||||||
|
|
||||||
|
const current = chunks[currentPage];
|
||||||
|
|
||||||
|
const row = [];
|
||||||
|
|
||||||
|
if (currentPage - 1 >= 0) {
|
||||||
|
row.push(Markup.button.callback("<", `${prefix}_${currentPage - 1}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentPage + 1 < chunks.length) {
|
||||||
|
row.push(Markup.button.callback(">", `${prefix}_${currentPage + 1}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
const keyboard = Markup.inlineKeyboard([row]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
current,
|
||||||
|
keyboard,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export function getRandomKeyboard(): Markup.Markup<InlineKeyboardMarkup> {
|
export function getRandomKeyboard(): Markup.Markup<InlineKeyboardMarkup> {
|
||||||
return Markup.inlineKeyboard([
|
return Markup.inlineKeyboard([
|
||||||
[Markup.button.callback('Книгу', RANDOM_BOOK)],
|
[Markup.button.callback('Книгу', RANDOM_BOOK)],
|
||||||
|
|||||||
Reference in New Issue
Block a user