mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 07:25:36 +01:00
Add random command
This commit is contained in:
@@ -6,3 +6,7 @@ export const SEARCH_SERIES_PREFIX = 'ss_';
|
|||||||
|
|
||||||
export const AUTHOR_BOOKS_PREFIX = 'ba_';
|
export const AUTHOR_BOOKS_PREFIX = 'ba_';
|
||||||
export const SEQUENCE_BOOKS_PREFIX = 'bs_';
|
export const SEQUENCE_BOOKS_PREFIX = 'bs_';
|
||||||
|
|
||||||
|
export const RANDOM_BOOK = 'random_book';
|
||||||
|
export const RANDOM_AUTHOR = 'random_author';
|
||||||
|
export const RANDOM_SEQUENCE = 'random_sequence';
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import { CachedMessage, getBookCache } from './services/book_cache';
|
|||||||
import { getBookCacheBuffer } from './services/book_cache_buffer';
|
import { getBookCacheBuffer } from './services/book_cache_buffer';
|
||||||
import { formatBook, formatAuthor, formatSequence } from './format';
|
import { formatBook, formatAuthor, formatSequence } from './format';
|
||||||
import { getPaginatedMessage, registerPaginationCommand } from './utils';
|
import { getPaginatedMessage, registerPaginationCommand } from './utils';
|
||||||
|
import { getRandomKeyboard } from './keyboard';
|
||||||
|
|
||||||
|
|
||||||
export async function createApprovedBot(token: string, state: BotState): Promise<Telegraf> {
|
export async function createApprovedBot(token: string, state: BotState): Promise<Telegraf> {
|
||||||
@@ -23,7 +24,7 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
});
|
});
|
||||||
|
|
||||||
await bot.telegram.setMyCommands([
|
await bot.telegram.setMyCommands([
|
||||||
{command: "random_book", description: "Случайная книга"},
|
{command: "random", description: "Попытать удачу"},
|
||||||
{command: "update_log", description: "Информация об обновлении каталога"},
|
{command: "update_log", description: "Информация об обновлении каталога"},
|
||||||
{command: "settings", description: "Настройки"},
|
{command: "settings", description: "Настройки"},
|
||||||
{command: "help", description: "Помощь"},
|
{command: "help", description: "Помощь"},
|
||||||
@@ -47,9 +48,58 @@ export async function createApprovedBot(token: string, state: BotState): Promise
|
|||||||
registerPaginationCommand(bot, CallbackData.SEARCH_BOOK_PREFIX, BookLibrary.searchByBookName, formatBook);
|
registerPaginationCommand(bot, CallbackData.SEARCH_BOOK_PREFIX, BookLibrary.searchByBookName, formatBook);
|
||||||
registerPaginationCommand(bot, CallbackData.SEARCH_AUTHORS_PREFIX, BookLibrary.searchAuthors, formatAuthor);
|
registerPaginationCommand(bot, CallbackData.SEARCH_AUTHORS_PREFIX, BookLibrary.searchAuthors, formatAuthor);
|
||||||
registerPaginationCommand(bot, CallbackData.SEARCH_SERIES_PREFIX, BookLibrary.searchSequences, formatSequence);
|
registerPaginationCommand(bot, CallbackData.SEARCH_SERIES_PREFIX, BookLibrary.searchSequences, formatSequence);
|
||||||
|
|
||||||
registerPaginationCommand(bot, CallbackData.AUTHOR_BOOKS_PREFIX, BookLibrary.getAuthorBooks, formatBook);
|
registerPaginationCommand(bot, CallbackData.AUTHOR_BOOKS_PREFIX, BookLibrary.getAuthorBooks, formatBook);
|
||||||
registerPaginationCommand(bot, CallbackData.SEQUENCE_BOOKS_PREFIX, BookLibrary.getSequenceBooks, formatBook);
|
registerPaginationCommand(bot, CallbackData.SEQUENCE_BOOKS_PREFIX, BookLibrary.getSequenceBooks, formatBook);
|
||||||
|
|
||||||
|
bot.command("random", async (ctx: Context) => {
|
||||||
|
ctx.reply("Что хотим получить?", {
|
||||||
|
reply_markup: getRandomKeyboard().reply_markup
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.action(CallbackData.RANDOM_BOOK, async (ctx: Context) => {
|
||||||
|
const book = await BookLibrary.getRandomBook();
|
||||||
|
|
||||||
|
const keyboard = Markup.inlineKeyboard([
|
||||||
|
[Markup.button.callback("Повторить?", CallbackData.RANDOM_BOOK)]
|
||||||
|
]);
|
||||||
|
|
||||||
|
ctx.editMessageReplyMarkup(undefined);
|
||||||
|
|
||||||
|
ctx.reply(formatBook(book), {
|
||||||
|
reply_markup: keyboard.reply_markup,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.action(CallbackData.RANDOM_AUTHOR, async (ctx: Context) => {
|
||||||
|
const author = await BookLibrary.getRandomAuthor();
|
||||||
|
|
||||||
|
const keyboard = Markup.inlineKeyboard([
|
||||||
|
[Markup.button.callback("Повторить?", CallbackData.RANDOM_AUTHOR)]
|
||||||
|
]);
|
||||||
|
|
||||||
|
ctx.editMessageReplyMarkup(undefined);
|
||||||
|
|
||||||
|
ctx.reply(formatAuthor(author), {
|
||||||
|
reply_markup: keyboard.reply_markup,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.action(CallbackData.RANDOM_SEQUENCE, async (ctx: Context) => {
|
||||||
|
const sequence = await BookLibrary.getRandomSequence();
|
||||||
|
|
||||||
|
const keyboard = Markup.inlineKeyboard([
|
||||||
|
[Markup.button.callback("Повторить?", CallbackData.RANDOM_SEQUENCE)]
|
||||||
|
]);
|
||||||
|
|
||||||
|
ctx.editMessageReplyMarkup(undefined);
|
||||||
|
|
||||||
|
ctx.reply(formatSequence(sequence), {
|
||||||
|
reply_markup: keyboard.reply_markup,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, async (ctx: Context) => {
|
bot.hears(/^\/d_[a-zA-Z0-9]+_[\d]+$/gm, async (ctx: Context) => {
|
||||||
if (!ctx.message || !('text' in ctx.message)) {
|
if (!ctx.message || !('text' in ctx.message)) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { Markup } from 'telegraf';
|
import { Markup } from 'telegraf';
|
||||||
import { InlineKeyboardMarkup } from 'typegram';
|
import { InlineKeyboardMarkup } from 'typegram';
|
||||||
|
|
||||||
|
import { RANDOM_BOOK, RANDOM_AUTHOR, RANDOM_SEQUENCE } from './callback_data';
|
||||||
|
|
||||||
|
|
||||||
export function getPaginationKeyboard(prefix: string, query: string, page: number, totalPages: number): Markup.Markup<InlineKeyboardMarkup> {
|
export function getPaginationKeyboard(prefix: string, query: string, page: number, totalPages: number): Markup.Markup<InlineKeyboardMarkup> {
|
||||||
function getRow(delta: number) {
|
function getRow(delta: number) {
|
||||||
@@ -29,4 +31,13 @@ export function getPaginationKeyboard(prefix: string, query: string, page: numbe
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Markup.inlineKeyboard(rows);
|
return Markup.inlineKeyboard(rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function getRandomKeyboard(): Markup.Markup<InlineKeyboardMarkup> {
|
||||||
|
return Markup.inlineKeyboard([
|
||||||
|
[Markup.button.callback('Книгу', RANDOM_BOOK)],
|
||||||
|
[Markup.button.callback('Автора', RANDOM_AUTHOR)],
|
||||||
|
[Markup.button.callback('Серию', RANDOM_SEQUENCE)],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|||||||
@@ -119,3 +119,15 @@ export async function getSequenceBooks(sequenceId: number, page: number): Promis
|
|||||||
size: PAGE_SIZE,
|
size: PAGE_SIZE,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getRandomBook(): Promise<Book> {
|
||||||
|
return _makeRequest<Book>('/api/v1/books/random');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getRandomAuthor(): Promise<Author> {
|
||||||
|
return _makeRequest<Author>('/api/v1/authors/random');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getRandomSequence(): Promise<Sequence> {
|
||||||
|
return _makeRequest<Sequence>('/api/v1/sequences/random');
|
||||||
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export function registerPaginationCommand<T>(
|
|||||||
reply_markup: pMessage.keyboard.reply_markup
|
reply_markup: pMessage.keyboard.reply_markup
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user