getText = new Scene('getText')
const stage = new Stage();
stage.register(seachText)
stage.register(getText)
stage.register(searchByCat)
bot.use(session());
bot.use(stage.middleware());
bot.command('start', async (ctx) => {
let fUser = await userService.getUser(ctx.from.username)
if (!fUser) {
ctx.reply(`Sorry ${ctx.from.username} you dont access bot` )
} else{
ctx.reply("Search for nursery rhymes\n", Markup.inlineKeyboard([
Markup.callbackButton("⭐️ Random", "random"),
Markup.callbackButton("🔍 Search", "search2"),
Markup.callbackButton("📄 Category list", "category")
]).oneTime()
.resize()
.extra())
}
})
bot.action("search2", async (ctx) => {
await ctx.answerCbQuery()
await ctx.reply(`Dear ${ctx.from.username} enter word for search`)
await ctx.scene.enter("search2")
});
bot.action("category", async (ctx) => {
await ctx.scene.enter('category')
await showPage(ctx)
})
searchByCat.action(/^list(\d+)$/, async ctx => await categoryService.showPage(ctx,+ctx.match[1]))
getText.action(/^list(\d+)$/, async ctx => await textService.showPage(ctx,+ctx.match[1]))
async function showPage(ctx,page=1){
const fCategory = await listCategories()
const psize = 5
const pages = Math.ceil(fCategory.length/psize)
const ptr = page>pages?pages:(page<1?1:page)
const kb = [[],[]]
let arrayCategories = []
await utils.asyncForEach(fCategory, async category => {
arrayCategories.push(category.name)
})
if (ptr>1) kb[0].unshift({text:'👈 Prev', callback_data:'list'+(ptr-1)})
if (ptr<pages) kb[0].push({text:'Next 👉', callback_data:'list'+(ptr+1)})
kb[0].push({text:'🏫 Home', callback_data:'start'})
let list = fCategory.slice((ptr-1)*psize,ptr*psize)
list = list.reverse()
await utils.asyncForEach(list, async el => {
kb.unshift([{text:el.name, callback_data:`text_${el._id}`}])
})
const textHTML = Dear ${ctx.from.username} please select category
if (ctx.callbackQuery) {
if (!info.includes('text_')){
await ctx.answerCbQuery()
} else {
await ctx.scene.leave('category')
await ctx.scene.enter('getText')
}
}
else {
console.log('replyWithHTML');
return ctx.replyWithHTML(textHTML,{
reply_markup:{
inline_keyboard:kb
}
})
}
if (!info.includes('text_')){
await ctx.telegram.editMessageText(ctx.from.id,ctx.callbackQuery.message.message_id,null,textHTML,{reply_markup:{inline_keyboard:kb}})
} else {
await ctx.scene.leave('category')
await ctx.scene.enter('getText')
}
}
searchByCat.on('callback_query', async (ctx) => {
let fCat = ctx.callbackQuery.data
console.log('searchByCat_callback_query');
console.log(fCat);
if (!fCat || fCat == '/search') return await ctx.reply('Sorry not found')
// Рандомный текст
if (fCat == 'random') {
try{
let result = await textService.randomText()
await ctx.answerCbQuery()
await ctx.replyWithHTML(utils.createTextMsg(result[0]),
Extra.HTML().markup(m =>
m.inlineKeyboard([
m.callbackButton("⭐️ Random", "random"),
m.callbackButton("🔍 Search", "search2"),
m.callbackButton("📄 Category list", "category")])
)
)
}catch(error) {
console.log(error);
await ctx.reply('Sorry not found')
}
}
// Возврат в основное меню
if (fCat === 'start'){
await ctx.scene.leave()
let fUser = await userService.getUser(ctx.from.username)
if (!fUser) {
ctx.reply(`Sorry ${ctx.from.username} you dont access bot` )
} else{
ctx.reply("Search for nursery rhymes\n", Markup.inlineKeyboard([
В садике pastebin использовать не научили?
Обсуждают сегодня