Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/images/botpick
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scripts": {
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
"start": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && node ./dist/main.js"
"start": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && node ./dist/index.js"
},
"dependencies": {
"@types/node": "^18.7.16",
Expand Down
18 changes: 18 additions & 0 deletions src/hears.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Markup, Context } from "telegraf";

export const Start = (ctx: any) => {
ctx.replyWithPhoto(
{source: "./assets/images/hello.jpg"},
{caption: "Привет\nЯ Mashrek!\nНажми на кнопочки!",
...Markup.keyboard([["Хочу", "Не хочу",]]).resize().oneTime()});
};
export const Help = (ctx : Context) => ctx.reply("'Хочу' - получить картинку");
export const Want = (ctx: any) => {
ctx.reply("Выберите взаймодействие",
Markup.inlineKeyboard([
Markup.button.callback("Фото", "photo"),
Markup.button.callback("Музыка", "audio"),
Markup.button.callback("Текст", "text"),
]));
};
export const DontWant = (ctx: Context) => ctx.reply("Ну ладно (:\nЕсли захочешь, нажми на кнопочку 'Хочу' !");
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Start, DontWant, Help, Want } from "hears";
import { Telegraf, Scenes, session } from 'telegraf';
import { botToken } from "token";
import { audioWizard, photoWizard, textWizard} from "wizards";

const bot = new Telegraf<Scenes.WizardContext>(botToken);
const stage = new Scenes.Stage<Scenes.WizardContext>([textWizard, photoWizard, audioWizard]);

bot.use(session());
bot.use(stage.middleware());
bot.launch();

bot.start(Start);
bot.help(Help);
bot.hears('Хочу', Want);
bot.hears('Не хочу', DontWant);
bot.action("photo", ctx => ctx.scene.enter('photo-wizard'));
bot.action("audio", ctx => ctx.scene.enter('audio-wizard'));
bot.action("text", ctx => ctx.scene.enter('text-wizard'));

// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));

25 changes: 0 additions & 25 deletions src/main.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { config } from "dotenv"
config()

const getBotToken = (): string => {
if (process.env.BOT_TOKEN) {
return process.env.BOT_TOKEN;
}
throw new Error('BOT_TOKEN must be provided!');
}

export const botToken = getBotToken();
86 changes: 86 additions & 0 deletions src/wizards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { Scenes, Composer} from 'telegraf'

const textHandler = new Composer<Scenes.WizardContext>()
textHandler.on('text', async (ctx) => {
const message = getMessage(ctx);

await ctx.copyMessage(message);
return await ctx.scene.leave();
});
textHandler.on('audio', async (ctx) => {
await ctx.reply("Введите текст, а не аудио!");
await ctx.scene.enter("text-wizard");
});
textHandler.on('photo', async (ctx) => {
await ctx.reply("Введите текст, а не фото!");
await ctx.scene.enter("text-wizard");
});

const photoHandler = new Composer<Scenes.WizardContext>()
photoHandler.on('photo', async (ctx) => {
const message = getMessage(ctx);

await ctx.copyMessage(message);
return await ctx.scene.leave();
});
photoHandler.on('audio', async (ctx) => {
await ctx.reply("Пришлите фото, а не аудио!");
await ctx.scene.enter("photo-wizard");
});
photoHandler.on('text', async (ctx) => {
await ctx.reply("Пришлите фото, а не текст!");
await ctx.scene.enter("photo-wizard");
});

const audioHandler = new Composer<Scenes.WizardContext>()
audioHandler.on('audio', async (ctx) => {
const message = getMessage(ctx);

await ctx.copyMessage(message);
return await ctx.scene.leave();
});
audioHandler.on('text', async (ctx) => {
await ctx.reply("Пришлите аудио, а не текст!");
await ctx.scene.enter("audio-wizard");
});
audioHandler.on('photo', async (ctx) => {
await ctx.reply("Пришлите аудио, а не фото!");
await ctx.scene.enter("audio-wizard");
});

export const textWizard = new Scenes.WizardScene(
'text-wizard',
async (ctx: any) => {
await ctx.reply('Пришлите текст');
return ctx.wizard.next();
},
textHandler
);

export const photoWizard = new Scenes.WizardScene(
'photo-wizard',
async (ctx: any) => {
await ctx.reply('Пришлите фото');
return ctx.wizard.next();
},
photoHandler
);

export const audioWizard = new Scenes.WizardScene(
'audio-wizard',
async (ctx: any) => {
await ctx.reply('Пришлите аудио');
return ctx.wizard.next();
},
audioHandler
);

const getMessage = (ctx: any): number => {
if (ctx.message?.chat.id && 'text') {
return ctx.message.chat.id;
}

throw new Error('This is no message!');
};