initial commit

This commit is contained in:
2022-10-20 17:32:46 +03:00
commit 4673348f38
20 changed files with 3275 additions and 0 deletions

58
src/handlers/balabola.ts Normal file
View File

@ -0,0 +1,58 @@
import { Keyboard, MessageContext } from "vk-io";
import { balabola } from "../balabola_api";
import { Composer } from "../composer";
import { isChat, isHasText, not } from "../filters";
import { balabolaQueue } from "../queue";
export const composer = new Composer<MessageContext>();
const filter = composer
.filter(not(isChat), composer.compose())
.filter(!!not(isHasText), composer.compose());
const selectStyleKeyboard = <C extends MessageContext>(ctx: C) => {
return Keyboard.builder()
.textButton({ label: '1', payload: { command: 'пр 0 ' + ctx.text! } })
.textButton({ label: '2', payload: { command: 'пр 24 ' + ctx.text! } })
.textButton({ label: '3', payload: { command: 'пр 25 ' + ctx.text! } })
.textButton({ label: '4', payload: { command: 'пр 11 ' + ctx.text! } })
.row()
.textButton({ label: '5', payload: { command: 'пр 6 ' + ctx.text! } })
.textButton({ label: '6', payload: { command: 'пр 8 ' + ctx.text! } })
.textButton({ label: '7', payload: { command: 'пр 9 ' + ctx.text! } })
.row()
.textButton({ label: 'чзх', payload: { command: 'help' } })
.inline();
};
const balabolaMessage = `Выбери стилизацию ответа:
1: без стилизации
2: инструкция
3: рецепт
4: мудрость
5: история
6: википедия
7: синопсис
более подробно команда help`;
filter.hear(/^(?:продолжи)\s(.*)?$/i, async ctx => {
await ctx.send(balabolaMessage, {
keyboard: selectStyleKeyboard(ctx)
});
});
filter.hear(/^(?:пр)\s(0|24|25|11|6|8|9)\s(.*)?$/i, async ctx => {
const intro = ctx.$match[1];
const query = ctx.$match[2];
await ctx.send('генерирую!!');
const result = await balabolaQueue.add(() => balabola(query, +intro));
await ctx.send(result);
});
filter.use(async ctx => {
await ctx.send('генерирую!!');
const result = await balabolaQueue.add(() => balabola(ctx.text!, 6));
await ctx.send(result);
});

27
src/handlers/error.ts Normal file
View File

@ -0,0 +1,27 @@
import { APIError, MessageContext } from "vk-io";
import { Composer } from "../composer";
import { logger } from "../logger";
export const composer = new Composer<MessageContext>();
composer.use(async (ctx, next) => {
try {
await next();
} catch (err) {
logger.error(err);
}
});
composer.use(async (ctx, next) => {
try {
await next();
} catch (err) {
if (err instanceof APIError && err.code === 917) {
logger.error('no access to the chat');
await ctx.send('хачю доступ к чату :=(');
return;
}
throw err;
}
});

27
src/handlers/help.ts Normal file
View File

@ -0,0 +1,27 @@
import { Composer } from "../composer";
import { MessageContext } from "vk-io";
export const composer = new Composer<MessageContext>();
const helpMessage = `эта штука работает через балабола (https://yandex.ru/lab/yalm)
настройки стилизации:
1: без стиля
- ну тут все и так понятно
2: инструкции по применению
- Перечислите несколько предметов, а Балабоба придумает, как их использовать
3: рецепты
- Перечислите съедобные ингредиенты, а Балабоба придумает рецепт с ними
4: мудрости
- Напишите что-нибудь и получите народную мудрость
5: истории
- Начните писать историю, а Балабобы продолжит — иногда страшно, но чаще смешно
6: википедия
- Напишите какое-нибудь слово, а Балабоба даст этому определение
7: синопсис
- Напишите название фильма (существующего или нет), а Балабоба расскажет вам, о чем он`;
composer.hear(/^(?:start|помощь|старт|начать|help)$/i, async ctx => {
await ctx.send(helpMessage, {
dont_parse_links: true
});
});

14
src/handlers/index.ts Normal file
View File

@ -0,0 +1,14 @@
import { Composer } from "../composer";
import { MessageContext } from "vk-io";
import { composer as updatesHandler } from "./updates";
import { composer as helpHandler } from "./help";
import { composer as balabolaHandler } from "./balabola";
import { composer as errorHandler } from "./error";
const handlers = new Composer<MessageContext>();
export default handlers;
handlers.use(errorHandler.compose());
handlers.use(updatesHandler.compose());
handlers.use(helpHandler.compose());
handlers.use(balabolaHandler.compose());

25
src/handlers/updates.ts Normal file
View File

@ -0,0 +1,25 @@
import { MessageContext } from "vk-io";
import { Composer } from "../composer";
import { isHasText, not } from "../filters";
import { logger } from "../logger";
export const composer = new Composer<MessageContext>();
const filter = composer.filter(!!not(isHasText), composer.compose());
filter.use(async (ctx, next) => {
const { messagePayload } = ctx;
ctx.state.command = messagePayload && messagePayload.command
? messagePayload.command
: null;
logger.debug({
payload: messagePayload,
state: ctx.state,
text: ctx.text
});
if (ctx.state.command) ctx.text = ctx.state.command;
return next();
});