diff --git a/assets/images/botpick b/assets/images/botpick new file mode 100644 index 0000000..3aa4e83 Binary files /dev/null and b/assets/images/botpick differ diff --git a/package.json b/package.json index 1746e38..7d658e8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/hears.ts b/src/hears.ts new file mode 100644 index 0000000..8a713fc --- /dev/null +++ b/src/hears.ts @@ -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Если захочешь, нажми на кнопочку 'Хочу' !"); diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..697a7fb --- /dev/null +++ b/src/index.ts @@ -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(botToken); +const stage = new Scenes.Stage([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')); + diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 4011a6d..0000000 --- a/src/main.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Markup } from "telegraf"; - -const { Telegraf } = require('telegraf'); -require('dotenv').config(); - -const bot = new Telegraf(process.env.BOT_TOKEN); - -bot.start((ctx : any) => { - ctx.replyWithPhoto( - {source: "./assets/images/hello.jpg"}, - {caption: "Привет\nЯ Mashrek!\nНажми на кнопочки!", - ...Markup.keyboard([["Хочу", "Не хочу",]]).resize()}); -}); -bot.help((ctx : any) => ctx.reply("'Хочу' - получить картинку")); -bot.hears('Хочу', (ctx : any) => { - const items = ["./assets/mashrek-images/1.jpg", "./assets/mashrek-images/2.jpg", "./assets/mashrek-images/3.jpg", "./assets/mashrek-images/4.jpg", "./assets/mashrek-images/5.jpg", "./assets/mashrek-images/6.jpg", "./assets/mashrek-images/7.jpg", "./assets/mashrek-images/8.jpg", "./assets/mashrek-images/9.jpg", "./assets/mashrek-images/10.jpg"] - const item = items[Math.floor(Math.random()*items.length)] - ctx.replyWithPhoto({source: item}); -}); -bot.hears('Не хочу', (ctx: any) => ctx.reply("Ну ладно (:\nЕсли захочешь, нажми на кнопочку 'Хочу' !")); -bot.launch(); - -// Enable graceful stop -process.once('SIGINT', () => bot.stop('SIGINT')); -process.once('SIGTERM', () => bot.stop('SIGTERM')); \ No newline at end of file diff --git a/src/token.ts b/src/token.ts new file mode 100644 index 0000000..1ffbc2e --- /dev/null +++ b/src/token.ts @@ -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(); \ No newline at end of file diff --git a/src/wizards.ts b/src/wizards.ts new file mode 100644 index 0000000..1a7a59a --- /dev/null +++ b/src/wizards.ts @@ -0,0 +1,86 @@ +/* eslint-disable @typescript-eslint/no-floating-promises */ +import { Scenes, Composer} from 'telegraf' + +const textHandler = new Composer() +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() +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() +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!'); +}; +