tdlib-native
Version:
🚀 Telegram TDLib native nodejs wrapper
258 lines (257 loc) • 5.65 kB
JavaScript
import { typename } from "./src/generated/types.mjs";
function forceReply({
placeholder = "",
personal = false
} = {}) {
return {
[typename]: "replyMarkupForceReply",
input_field_placeholder: placeholder,
is_personal: personal
};
}
function removeKeyboard({
personal = false
} = {}) {
return {
[typename]: "replyMarkupRemoveKeyboard",
is_personal: personal
};
}
class KeyboardBuilder {
_options;
_row = 0;
_buttons = [];
/**
*
*
* @readonly
* @private
* @memberof KeyboardBuilder
*/
get _currentRow() {
return this._buttons[+this._row] ??= [];
}
/**
* Creates an instance of KeyboardBuilder.
* @param {Partial<KeyboardOptions>} options
* @memberof KeyboardBuilder
*/
constructor(options) {
this._options = options;
}
/**
* Adds text button
*
* @param {string} text
* @returns {this}
* @memberof KeyboardBuilder
*/
textButton(text) {
return this.button(text, { [typename]: "keyboardButtonTypeText" });
}
/**
* Adds button to current row
*
* @param {string} text
* @param {KeyboardButtonType} [type]
* @returns {this}
* @memberof KeyboardBuilder
*/
button(text, type) {
this._currentRow.push({ [typename]: "keyboardButton", text, type });
return this;
}
/**
* Creates new row
*
* @returns {this}
* @memberof KeyboardBuilder
*/
row() {
if (this._currentRow.length > 0) {
this._buttons[++this._row] ??= [];
}
return this;
}
/**
* Sets {@link replyMarkupShowKeyboard$Input.is_persistent}
*
* @param {boolean} [isPersistent=true]
* @returns {this}
* @memberof KeyboardBuilder
*/
persistent(isPersistent = true) {
this._options.persistent = isPersistent;
return this;
}
/**
* Sets {@link replyMarkupShowKeyboard$Input.resize_keyboard}
*
* @param {boolean} [doResize=true]
* @returns {this}
* @memberof KeyboardBuilder
*/
resize(doResize = true) {
this._options.resize = doResize;
return this;
}
/**
* Sets {@link replyMarkupShowKeyboard$Input.is_personal}
*
* @param {boolean} [isPersonal=true]
* @returns {this}
* @memberof KeyboardBuilder
*/
personal(isPersonal = true) {
this._options.personal = isPersonal;
return this;
}
/**
* Sets {@link replyMarkupShowKeyboard$Input.one_time}
*
* @param {boolean} [isOneTime=true]
* @returns {this}
* @memberof KeyboardBuilder
*/
oneTime(isOneTime = true) {
this._options.oneTime = isOneTime;
return this;
}
/**
* Sets {@link replyMarkupShowKeyboard$Input.input_field_placeholder}
*
* @param {string} value
* @returns {this}
* @memberof KeyboardBuilder
*/
placeholder(value) {
this._options.placeholder = value;
return this;
}
/**
* Generates keyboard
*
* @returns {replyMarkupShowKeyboard$Input} {@link replyMarkupShowKeyboard$Input}
* @memberof KeyboardBuilder
*/
build() {
return {
[typename]: "replyMarkupShowKeyboard",
is_persistent: this._options.persistent || false,
resize_keyboard: this._options.resize || false,
is_personal: this._options.personal || false,
one_time: this._options.oneTime || false,
input_field_placeholder: this._options.placeholder || void 0,
rows: this._buttons
};
}
/**
* Generates keyboard
*
* @returns {replyMarkupShowKeyboard} {@link replyMarkupShowKeyboard$Input}
* @memberof KeyboardBuilder
*/
toJSON() {
return this.build();
}
}
function keyboard(options = {}) {
return new KeyboardBuilder(options);
}
const encoder = new TextEncoder();
class InlineKeyboardBuilder {
_buttons = [];
_row = 0;
/**
*
*
* @readonly
* @private
* @memberof InlineKeyboardBuilder
*/
get _currentRow() {
return this._buttons[+this._row] ??= [];
}
/**
* Adds callback button
*
* @param {string} text
* @param {string} data
* @returns {this}
* @memberof InlineKeyboardBuilder
*/
callbackButton(text, data) {
return this.button(text, {
[typename]: "inlineKeyboardButtonTypeCallback",
data: typeof data === "string" ? encoder.encode(data) : new Uint8Array(data)
});
}
/**
* Adds url button
*
* @param {string} text
* @param {string|URL} url
* @returns {this}
* @memberof InlineKeyboardBuilder
*/
urlButton(text, url) {
return this.button(text, {
[typename]: "inlineKeyboardButtonTypeUrl",
url: url.toString()
});
}
/**
*
*
* @param {string} text
* @param {InlineKeyboardButtonType$Input} type
* @returns {this}
* @memberof InlineKeyboardBuilder
*/
button(text, type) {
this._currentRow.push({ [typename]: "inlineKeyboardButton", text, type });
return this;
}
/**
* Creates new row
*
* @returns {this}
* @memberof KeyboardBuilder
*/
row() {
if (this._currentRow.length > 0) {
this._buttons[++this._row] ??= [];
}
return this;
}
/**
* Generates keyboard
*
* @returns {replyMarkupInlineKeyboard$Input} {@link replyMarkupInlineKeyboard$Input}
* @memberof InlineKeyboardBuilder
*/
build() {
return {
[typename]: "replyMarkupInlineKeyboard",
rows: this._buttons
};
}
/**
* Generates keyboard
*
* @returns {replyMarkupInlineKeyboard$Input} {@link replyMarkupInlineKeyboard$Input}
* @memberof InlineKeyboardBuilder
*/
toJSON() {
return this.build();
}
}
function inlineKeyboard() {
return new InlineKeyboardBuilder();
}
export {
forceReply,
inlineKeyboard,
keyboard,
removeKeyboard
};