UNPKG

@humanwhocodes/crosspost

Version:

A utility to post across multiple social networks.

131 lines (130 loc) 3.17 kB
/** * A strategy for posting messages to Telegram. */ export class TelegramStrategy { /** * Creates a new instance. * @param {TelegramOptions} options Options for the instance. * @throws {Error} When options are missing. */ constructor(options: TelegramOptions); /** * The ID of the strategy. * @type {string} * @readonly */ readonly id: string; /** * The display name of the strategy. * @type {string} * @readonly */ readonly name: string; /** * Maximum length of a Telegram message in characters. * @type {number} * @const */ MAX_MESSAGE_LENGTH: number; /** * Posts a message to Telegram. * @param {string} message The message to post. * @param {PostOptions} [postOptions] Additional options for the post. * @returns {Promise<TelegramMessageResponse>} A promise that resolves with the message data. * @throws {Error} When the message fails to post. */ post(message: string, postOptions?: PostOptions): Promise<TelegramMessageResponse>; /** * Extracts a URL from a Telegram API response. * @param {TelegramMessageResponse} response The response from the Telegram API post request. * @returns {string} The URL for the Telegram message. */ getUrlFromResponse(response: TelegramMessageResponse): string; /** * Calculates the length of a message according to Telegram's algorithm. * All Unicode characters are counted as is. * @param {string} message The message to calculate the length of. * @returns {number} The calculated length of the message. */ calculateMessageLength(message: string): number; #private; } export type PostOptions = import("../types.js").PostOptions; export type TelegramOptions = { /** * The Telegram bot token. */ botToken: string; /** * The Telegram chat ID to post to. */ chatId: string; }; export type TelegramMessageResponse = { /** * Whether the request was successful. */ ok: boolean; /** * The message data. */ result: TelegramMessage; }; export type TelegramMessage = { /** * The message ID. */ message_id: number; /** * The chat the message was sent to. */ chat: TelegramChat; /** * The text of the message. */ text: string; }; export type TelegramChat = { /** * The chat ID. */ id: number; /** * The type of chat. */ type: string; }; export type TelegramErrorResponse = { /** * Whether the request was successful. */ ok: boolean; /** * The error code. */ error_code: number; /** * The error description. */ description: string; }; export type TelegramUpdateResponse = { /** * Whether the request was successful. */ ok: boolean; /** * The array of updates. */ result: Array<TelegramUpdate>; }; export type TelegramUpdate = { /** * The update ID. */ update_id: number; /** * The message data. */ message?: TelegramMessage | undefined; };