Skip to content

Instantly share code, notes, and snippets.

@LuisMayo
Created June 28, 2020 17:08
Show Gist options
  • Save LuisMayo/1ad83afaed476b0b05ba69f9d3f0dec3 to your computer and use it in GitHub Desktop.
Save LuisMayo/1ad83afaed476b0b05ba69f9d3f0dec3 to your computer and use it in GitHub Desktop.
Keyboard helper for Telegraf
import * as Telegraf from 'telegraf';
import { CallbackButton, Button } from 'telegraf/typings/markup';
export class ButtonKeyBoardHelper {
buttons: (CallbackButton | Button)[][] = [];
lastArr = [];
charactersInCurrentLine = 0;
constructor() {
this.buttons.push(this.lastArr);
}
addNewButton(text: string, data?: string | boolean) {
const callback = !(data == null || typeof data === 'boolean');
if (this.charactersInCurrentLine + text.length >= (callback ? 30 : 50)) {
this.newLine();
}
this.lastArr.push(callback ? Telegraf.Markup.callbackButton(text, data as string) : Telegraf.Markup.button(text, data as boolean));
this.charactersInCurrentLine+=text.length;
}
private newLine() {
this.lastArr = [];
this.buttons.push(this.lastArr);
this.charactersInCurrentLine = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment