Created
March 31, 2022 17:11
-
-
Save nugmanoff/037860f3ed27e8959e7fdc8bd1220ef4 to your computer and use it in GitHub Desktop.
Grammyjs Stepping Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Composer } from "grammy"; | |
import { Router } from "@grammyjs/router"; | |
import { isPrivate } from "grammy-guard"; | |
import { Context } from "@bot/types"; | |
import { selectSpecializationKeyboard } from "@bot/keyboards"; | |
import tryToFindMentors from "@bot/helpers/try-to-find-mentors"; | |
import paginate from "@bot/helpers/pagination"; | |
import { Menu } from "@grammyjs/menu"; | |
import { logger } from "@bot/logger"; | |
export const composer = new Composer<Context>().filter(isPrivate); | |
const router = new Router<Context>((ctx) => { | |
logger.info({ | |
msg: "logging routing", | |
session_step: ctx.session.step, | |
}); | |
return ctx.session.step; | |
}); | |
const displayMentorsPageSize = 5; | |
composer.command("find_mentors", async (ctx) => { | |
await ctx.reply(ctx.t("FILL_NAME")); | |
ctx.session.step = "gotName"; | |
}); | |
composer.use(selectSpecializationKeyboard); | |
router.route("gotName", async (ctx) => { | |
await ctx.reply(ctx.t("CHOOSE_SPECIALIZATION"), { | |
reply_markup: selectSpecializationKeyboard, | |
}); | |
ctx.session.step = "gotSpecialization"; | |
}); | |
router.route("gotSpecialization", async (ctx) => { | |
ctx.session.step = "gotYearsOfExperience"; | |
await ctx.reply(ctx.t("YEARS_OF_EXPERIENCE")); | |
return; | |
}); | |
router.route("gotYearsOfExperience", async (ctx) => { | |
if (!validateYearsOfExperience(ctx)) { | |
logger.info({ | |
msg: "gotYearsOfExperienceHandler If", | |
}); | |
await ctx.reply(ctx.t("YEARS_OF_EXPERIENCE_WRONG")); | |
return; | |
} else { | |
logger.info({ | |
msg: "gotYearsOfExperienceHandler Else", | |
}); | |
ctx.session.step = "displayMentors"; | |
return; | |
} | |
}); | |
const displayMentorsMenu = new Menu<Context>("display-mentors") | |
.text("Показать ещё", (ctx) => { | |
ctx.session.step = "displayMentors"; | |
}) | |
.text("Я нашел", (ctx) => ctx.reply("До новых встреч!")) | |
.row(); | |
router.route("displayMentors", async (ctx) => { | |
if (!ctx.session.mentors || ctx.session.mentors.length === 0) { | |
const mentors = await tryToFindMentors(); | |
ctx.session.mentors = mentors; | |
ctx.session.mentorsPage = 1; | |
} | |
if (ctx.session.mentors.length > 0) { | |
await ctx.reply(ctx.t("BOT_FOUND_MENTORS")); | |
const pagination = paginate( | |
ctx.session.mentors.length, | |
ctx.session.mentorsPage, | |
displayMentorsPageSize | |
); | |
if (ctx.session.mentorsPage > pagination.endPage) { | |
await ctx.reply(ctx.t("NO_MENTORS_FOUND")); | |
} | |
const replies = ctx.session.mentors | |
.slice(pagination.startIndex, pagination.endIndex + 1) | |
.map((mentor) => { | |
return ctx.reply( | |
`👤 ${mentor.name}\nОпыт: ${mentor.yearsOfExperience} года` | |
); | |
}); | |
ctx.session.mentorsPage += 1; | |
await Promise.all(replies); | |
await ctx.reply(ctx.t("MENTORS_FOUND"), { | |
reply_markup: displayMentorsMenu, | |
}); | |
} else { | |
await ctx.reply(ctx.t("NO_MENTORS_FOUND")); | |
} | |
}); | |
const validateYearsOfExperience = (ctx: Context) => { | |
if (ctx.msg !== undefined && ctx.msg.text !== undefined) { | |
return ( | |
!isNaN(parseFloat(String(ctx.msg.text!))) && | |
isFinite(Number(ctx.msg.text!)) | |
); | |
} else { | |
return false; | |
} | |
}; | |
composer.use(displayMentorsMenu); | |
composer.use(router); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment