Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('menu.middleware fails with .init() hint', t => {
const menu: any = new TelegrafInlineMenu('yaay')
const bot = new Telegraf('')
// Normally user would use bot.use.
// But telegraf will later use .middleware() on it. in order to check this faster, trigger this directly
t.throws(() => bot.use(menu.middleware()), /bot\.use\(menu\.init/)
})
test('isSetFunc falsy is like false', async t => {
const menu = new TelegrafInlineMenu('yaay')
menu.toggle('toggle me', 'c', {
setFunc: async () => Promise.reject(new Error('Nothing has to be set when only showing the menu')),
// Returning undefined is not possible from TypeScript but from JavaScript
isSetFunc: () => undefined as any
})
const bot = new Telegraf('')
bot.use(menu.init({actionCode: 'a'}))
bot.context.answerCbQuery = async () => true
bot.context.editMessageText = async (_text, extra: InlineExtra) => {
t.deepEqual(extra.reply_markup.inline_keyboard, [[{
text: emojiFalse + ' toggle me',
callback_data: 'a:c-true'
}]])
return true
}
await bot.handleUpdate({callback_query: {data: 'a'}} as Update)
})
test('simple text without buttons', async t => {
const menu = new TelegrafInlineMenu('yaay')
const bot = new Telegraf('')
bot.use(menu.init({actionCode: 'a'}))
bot.context.answerCbQuery = async () => true
bot.context.editMessageText = async (text, extra: InlineExtra) => {
t.is(text, 'yaay')
t.deepEqual(extra.reply_markup.inline_keyboard, [])
return true
}
await bot.handleUpdate({callback_query: {data: 'a'}} as Update)
})
test('submenu without back button', async t => {
const menu = new TelegrafInlineMenu('foo')
menu.submenu('Submenu', 'c', new TelegrafInlineMenu('bar'))
const bot = new Telegraf('')
bot.use(menu.init({actionCode: 'a'}))
bot.context.answerCbQuery = async () => true
bot.context.editMessageText = async (_text, extra: InlineExtra) => {
t.deepEqual(extra.reply_markup.inline_keyboard, [])
return true
}
await bot.handleUpdate({callback_query: {data: 'a:c'}} as Update)
})
function createTestBot(t: ExecutionContext, command: string | string[]): Telegraf {
const menu = new TelegrafInlineMenu('foo')
.manual('bar', 'c')
menu.setCommand(command)
const bot = new Telegraf('')
bot.context.reply = async (text, extra: InlineExtra) => {
t.is(text, 'foo')
t.deepEqual(extra.reply_markup.inline_keyboard, [[{
text: 'bar',
callback_data: 'a:c'
}]])
return DUMMY_MESSAGE
}
bot.use(menu.init({actionCode: 'a'}))
return bot
}
test('something that is not an action in dynamic menu throws error', t => {
const menu = new TelegrafInlineMenu('foo')
const submenu = new TelegrafInlineMenu('bar')
.question('Question', 'q', {
questionText: '42',
setFunc: () => {}
})
menu.select('a', ['a', 'b'], {
submenu
})
const bot = new Telegraf('')
t.throws(() => {
bot.use(menu.init())
}, /dynamic.+question.+menu.+a/)
})
test('option array menu', async t => {
const menu = new TelegrafInlineMenu('foo')
menu.select('c', ['a', 'b'], {
setFunc: () => t.fail()
})
const bot = new Telegraf('')
bot.use(menu.init({actionCode: 'a'}))
bot.context.answerCbQuery = async () => true
bot.context.editMessageText = async (_text, extra: InlineExtra) => {
t.deepEqual(extra.reply_markup.inline_keyboard, [[
{
text: 'a',
callback_data: 'a:c-a'
}, {
text: 'b',
callback_data: 'a:c-b'
}
]])
return true
}
test('button updates menu', async t => {
t.plan(2)
const menu = new TelegrafInlineMenu('yaay')
menu.button('hit me', 'c', {
doFunc: () => t.pass()
})
const bot = new Telegraf('')
bot.use(menu.init({actionCode: 'a'}))
bot.context.answerCbQuery = async () => true
bot.context.editMessageText = async (_text, extra: InlineExtra) => {
t.deepEqual(extra.reply_markup.inline_keyboard, menuKeyboard)
return true
}
await bot.handleUpdate({callback_query: {data: 'a:c'}} as Update)
})
test('joinLastRow', async t => {
const menu = new TelegrafInlineMenu('yaay')
.manual('hit me', 'c')
.manual('hit me hard', 'd', {joinLastRow: true})
const bot = new Telegraf('')
bot.use(menu.init({actionCode: 'a'}))
bot.context.answerCbQuery = async () => true
bot.context.editMessageText = async (_text, extra: InlineExtra) => {
t.deepEqual(extra.reply_markup.inline_keyboard, [[button1, button2]])
return true
}
await bot.handleUpdate({callback_query: {data: 'a'}} as Update)
})
export async function factory (config: IAppConfig) {
const bot = new Telegraf(config.telegram.token, { telegram: { webhookReply: false } })
bot.use((ctx, next: any) => {
Sentry.configureScope(scope => {
setUser(ctx, scope)
scope.setExtra('update', ctx.update)
})
next(ctx)
.catch((err: any) => {
console.error(`Error processing update ${ctx.update.update_id}: ${err.message}`)
Sentry.captureException(err)
if (ctx.chat && ctx.message) {
ctx.telegram.sendMessage(ctx.chat.id, 'Error processing message', { reply_to_message_id: ctx.message.message_id })
.catch(console.error)
}
})