我已经为披萨订单制作了一个机器人,现在我正试图从用户那里获取有关他的订单的信息。为此,我声明了一个函数getName(titles, ctx)
来获取用户想要的披萨名称。
这是我重构之前的代码:
const options = await titles.map(title => `${titles.indexOf(title) + 1}`)
await ctx.reply('Выбирай любую!', {
reply_markup: {
keyboard: new Array(titles.length).fill(0).map((e, i) => [{text: `${i + 1}`}]),
resize_keyboard: true,
one_time_keyboard: true,
remove_keyboard: true,
}
})
await bot.hears(options, async (ctx) => {
const name = titles[parseInt(ctx.match[0]) - 1]
await keyboard.sendMessage(ctx.chat.id, `Значит ${name}. Размер?`)
//here go some action with the name I got
在此变体中,该bot.hears(...)
功能完美运行,您甚至可以在此屏幕截图中看到它:
这是我的代码在重构后的样子:
getName(...)函数:
const getName = async (titles, ctx) => {
const options = await generateKeyboardMarkup('Выбирай любую!', {
keyboard: new Array(titles.length).fill(0).map((e, i) => [{text: `${i + 1}`}]),
resize_keyboard: true,
one_time_keyboard: true,
remove_keyboard: true,
}, ctx)
return new Promise(res => {
bot.hears(options, ctx => {
res(titles[parseInt(ctx.match[0]) - 1])
})
})
}
getUserOrder(...)函数:
const getUserOrder = async ($, ctx) => {
//some code here
const name = await getName(titles, ctx)
//some code here
return {name, size, sauce}
}
generateKeyboardMarkup(...)函数:
const generateKeyboardMarkup = async (reply, markup, ctx) => {
await ctx.reply(reply, {reply_markup: markup})
return markup.keyboard.map((e) => e[0].text)
}
键盘生成完美,但是当我单击任何按钮时,我什么也得不到,90000 毫秒后我得到TimeoutError: Promise timed out after 90000 milliseconds
. return
如果我在and之间插入此代码片段const options = ...
,我在控制台中也一无所获:
bot.hears(options, ctx => {
console.log(titles[parseInt(ctx.match[0]) - 1])
})
也许您知道可能的解决方案(我认为错误不在 Promise 中,而是在 中bot.hears(...)
)?