mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Add getting random boo by genre
This commit is contained in:
@@ -15,9 +15,13 @@ export const AUTHOR_ANNOTATION_PREFIX = 'b_an_';
|
|||||||
export const BOOK_INFO_PREFIX = 'b_i_';
|
export const BOOK_INFO_PREFIX = 'b_i_';
|
||||||
|
|
||||||
export const RANDOM_BOOK = 'random_book';
|
export const RANDOM_BOOK = 'random_book';
|
||||||
|
export const RANDOM_BOOK_BY_GENRE_REQUEST = 'random_book_by_genre_request';
|
||||||
|
export const RANDOM_BOOK_BY_GENRE = 'random_book_by_genre_';
|
||||||
export const RANDOM_AUTHOR = 'random_author';
|
export const RANDOM_AUTHOR = 'random_author';
|
||||||
export const RANDOM_SEQUENCE = 'random_sequence';
|
export const RANDOM_SEQUENCE = 'random_sequence';
|
||||||
|
|
||||||
|
export const GENRES_PREFIX = 'genres_';
|
||||||
|
|
||||||
export const LANG_SETTINGS = 'lang_settings';
|
export const LANG_SETTINGS = 'lang_settings';
|
||||||
export const ENABLE_LANG_PREFIX = 'lang_on_';
|
export const ENABLE_LANG_PREFIX = 'lang_on_';
|
||||||
export const DISABLE_LANG_PREFIX = 'lang_off_';
|
export const DISABLE_LANG_PREFIX = 'lang_off_';
|
||||||
|
|||||||
@@ -105,6 +105,65 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
registerRandomItemCallback(bot, CallbackData.RANDOM_AUTHOR, BookLibrary.getRandomAuthor, formatAuthor);
|
registerRandomItemCallback(bot, CallbackData.RANDOM_AUTHOR, BookLibrary.getRandomAuthor, formatAuthor);
|
||||||
registerRandomItemCallback(bot, CallbackData.RANDOM_SEQUENCE, BookLibrary.getRandomSequence, formatSequence);
|
registerRandomItemCallback(bot, CallbackData.RANDOM_SEQUENCE, BookLibrary.getRandomSequence, formatSequence);
|
||||||
|
|
||||||
|
bot.action(CallbackData.RANDOM_BOOK_BY_GENRE_REQUEST, async (ctx: Context) => {
|
||||||
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
|
const metaGenres = await BookLibrary.getGenreMetas();
|
||||||
|
|
||||||
|
const keyboard = Markup.inlineKeyboard(
|
||||||
|
metaGenres.map((meta, index) => {
|
||||||
|
return [Markup.button.callback(meta, `${CallbackData.GENRES_PREFIX}${index}`)];
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
await ctx.editMessageReplyMarkup(keyboard.reply_markup);
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.action(new RegExp(CallbackData.GENRES_PREFIX), async (ctx: Context) => {
|
||||||
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
|
const queryData = ctx.callbackQuery.data.split("_");
|
||||||
|
const metaIndex = parseInt(queryData[1]);
|
||||||
|
|
||||||
|
const metaGenres = await BookLibrary.getGenreMetas();
|
||||||
|
const meta = metaGenres[metaIndex];
|
||||||
|
|
||||||
|
const genres = await BookLibrary.getGenres(meta);
|
||||||
|
|
||||||
|
const buttons = genres.items.map((genre) => {
|
||||||
|
return [Markup.button.callback(genre.description, `${CallbackData.RANDOM_BOOK_BY_GENRE}${genre.id}`)]
|
||||||
|
});
|
||||||
|
buttons.push(
|
||||||
|
[Markup.button.callback("< Назад >", CallbackData.RANDOM_BOOK_BY_GENRE_REQUEST)]
|
||||||
|
);
|
||||||
|
|
||||||
|
const keyboard = Markup.inlineKeyboard(buttons);
|
||||||
|
|
||||||
|
await ctx.editMessageReplyMarkup(keyboard.reply_markup);
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.action(new RegExp(CallbackData.RANDOM_BOOK_BY_GENRE), async (ctx: Context) => {
|
||||||
|
if (!ctx.callbackQuery || !('data' in ctx.callbackQuery)) return;
|
||||||
|
|
||||||
|
const allowedLangs = await getUserOrDefaultLangCodes(ctx.callbackQuery.from.id);
|
||||||
|
|
||||||
|
const queryData = ctx.callbackQuery.data.split("_");
|
||||||
|
const genreId = parseInt(queryData[4]);
|
||||||
|
|
||||||
|
const item = await BookLibrary.getRandomBook(allowedLangs, genreId);
|
||||||
|
const keyboard = Markup.inlineKeyboard([
|
||||||
|
[Markup.button.callback("Повторить?", ctx.callbackQuery.data)]
|
||||||
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await ctx.editMessageReplyMarkup(Markup.inlineKeyboard([]).reply_markup);
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
ctx.reply(formatDetailBook(item), {
|
||||||
|
reply_markup: keyboard.reply_markup,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
bot.command(["update_log", `update_log@${me.username}`], async (ctx: Context) => {
|
bot.command(["update_log", `update_log@${me.username}`], async (ctx: Context) => {
|
||||||
ctx.reply("Обновление каталога: ", {
|
ctx.reply("Обновление каталога: ", {
|
||||||
reply_markup: getUpdateLogKeyboard().reply_markup,
|
reply_markup: getUpdateLogKeyboard().reply_markup,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { InlineKeyboardMarkup } from 'typegram';
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import chunkText from 'chunk-text';
|
import chunkText from 'chunk-text';
|
||||||
|
|
||||||
import { RANDOM_BOOK, RANDOM_AUTHOR, RANDOM_SEQUENCE, ENABLE_LANG_PREFIX, DISABLE_LANG_PREFIX, UPDATE_LOG_PREFIX, RATE_PREFIX } from './callback_data';
|
import { RANDOM_BOOK, RANDOM_AUTHOR, RANDOM_SEQUENCE, ENABLE_LANG_PREFIX, DISABLE_LANG_PREFIX, UPDATE_LOG_PREFIX, RATE_PREFIX, RANDOM_BOOK_BY_GENRE, RANDOM_BOOK_BY_GENRE_REQUEST } from './callback_data';
|
||||||
import { getLanguages, getUserOrDefaultLangCodes } from './services/user_settings';
|
import { getLanguages, getUserOrDefaultLangCodes } from './services/user_settings';
|
||||||
import * as BookRating from "./services/book_ratings";
|
import * as BookRating from "./services/book_ratings";
|
||||||
|
|
||||||
@@ -74,6 +74,7 @@ export function getTextPaginationData(prefix: string, text: string, currentPage:
|
|||||||
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)],
|
||||||
|
[Markup.button.callback('Книгу по жанру', RANDOM_BOOK_BY_GENRE_REQUEST)],
|
||||||
[Markup.button.callback('Автора', RANDOM_AUTHOR)],
|
[Markup.button.callback('Автора', RANDOM_AUTHOR)],
|
||||||
[Markup.button.callback('Серию', RANDOM_SEQUENCE)],
|
[Markup.button.callback('Серию', RANDOM_SEQUENCE)],
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -207,8 +207,14 @@ export async function getSequenceBooks(sequenceId: number | string, page: number
|
|||||||
return _makeRequest<Page<Book>>(`/api/v1/sequences/${sequenceId}/books`, searchParams);
|
return _makeRequest<Page<Book>>(`/api/v1/sequences/${sequenceId}/books`, searchParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRandomBook(allowedLangs: string[]): Promise<DetailBook> {
|
export async function getRandomBook(allowedLangs: string[], genre: number | null = null): Promise<DetailBook> {
|
||||||
return _makeRequest<DetailBook>('/api/v1/books/random', getAllowedLangsSearchParams(allowedLangs));
|
const params = getAllowedLangsSearchParams(allowedLangs);
|
||||||
|
if (genre) params.append("genre", genre.toString());
|
||||||
|
|
||||||
|
return _makeRequest<DetailBook>(
|
||||||
|
'/api/v1/books/random',
|
||||||
|
params,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRandomAuthor(allowedLangs: string[]): Promise<Author> {
|
export async function getRandomAuthor(allowedLangs: string[]): Promise<Author> {
|
||||||
@@ -218,3 +224,11 @@ export async function getRandomAuthor(allowedLangs: string[]): Promise<Author> {
|
|||||||
export async function getRandomSequence(allowedLangs: string[]): Promise<Sequence> {
|
export async function getRandomSequence(allowedLangs: string[]): Promise<Sequence> {
|
||||||
return _makeRequest<Sequence>('/api/v1/sequences/random', getAllowedLangsSearchParams(allowedLangs));
|
return _makeRequest<Sequence>('/api/v1/sequences/random', getAllowedLangsSearchParams(allowedLangs));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getGenreMetas(): Promise<string[]> {
|
||||||
|
return _makeRequest<string[]>('/api/v1/genres/metas');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getGenres(meta: string): Promise<Page<Genre>> {
|
||||||
|
return _makeRequest<Page<Genre>>('/api/v1/genres', {meta});
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user