Introduction
Lightweight and simple library that helps manage callback data
when using inline buttons in messages
Installation
$ npm install telegraf-callback-data
or using yarn
:
$ yarn add telegraf-callback-data
Example
import { Telegraf, Markup, Context } from 'telegraf';
import { CallbackData } from 'telegraf-callback-data';
const bot = new Telegraf(process.env.BOT_TOKEN);
const greetingData = new CallbackData<{ type: string; }>(
'greeting',
['type']
);
bot.start((ctx: Context) =>
ctx.reply('How to greet?', {
...Markup.inlineKeyboard([
Markup.button.callback(
'oldschool',
greetingData.create({
type: 'oldschool',
})
),
Markup.button.callback(
'modern',
greetingData.create({
type: 'modern',
})
),
]),
})
);
bot.action(
greetingData.filter({
type: 'modern',
}),
(ctx) => ctx.answerCbQuery('Yo')
);
bot.action(
greetingData.filter({
type: 'oldschool',
}),
(ctx) => ctx.answerCbQuery('Hello')
);
bot.launch();
There's some more complex examples: counter and menu.
API Usage Example
exampleCallbackData = new CallbackData(
'namespace-prefix',
['id', 'action' ]
);
const callbackData = exampleCallbackData.create({
id: '1337',
action: 'show',
});
console.log(callbackData);
const parsedCallbackData = exampleCallbackData.parse(callbackData);
console.log(parsedCallbackData);
const regexpCallbackDataFilter = exampleCallbackData.filter({
action: 'show',
});
console.log(regexpCallbackDataFilter);
Credits
Thanks to @JrooTJunior, author of AIOGram! This library highly inspired by alternative feature in aiogram framework.