@riseonly/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,535 @@
1
+ declare const DEFAULT_API_BASE = "https://api.riseonly.net";
2
+ declare const CANONICAL_API_PREFIX = "/api/bot/v1";
3
+ declare const WEBHOOK_SECRET_HEADER = "x-riseonly-bot-api-secret-token";
4
+ declare const IDEMPOTENCY_HEADER = "x-request-id";
5
+ declare const SUPPORTED_METHODS: readonly ["getMe", "sendMessage", "sendPhoto", "sendVideo", "sendDocument", "sendAudio", "sendVoice", "sendAnimation", "editMessageText", "deleteMessage", "sendChatAction", "getUpdates", "setWebhook", "deleteWebhook", "getWebhookInfo", "setMyCommands", "getMyCommands", "deleteMyCommands", "answerCallbackQuery", "getChat"];
6
+ type BotApiMethod = (typeof SUPPORTED_METHODS)[number];
7
+ declare const UPDATE_TYPES: readonly ["message", "edited_message", "callback_query", "chat_member", "my_chat_member"];
8
+ type UpdateType = (typeof UPDATE_TYPES)[number];
9
+ declare const CHAT_ACTIONS: readonly ["typing", "upload_photo", "record_video", "upload_video", "record_voice", "upload_voice", "upload_document", "choose_sticker", "find_location", "record_video_note", "upload_video_note"];
10
+ type ChatAction = (typeof CHAT_ACTIONS)[number];
11
+ declare const COMMAND_SCOPES: readonly ["default", "all_private_chats", "all_group_chats", "all_chat_administrators"];
12
+ type CommandScope = (typeof COMMAND_SCOPES)[number] | `chat:${string}` | `chat_administrators:${string}`;
13
+
14
+ interface ApiResponse<T> {
15
+ ok: true;
16
+ result: T;
17
+ }
18
+ interface ApiErrorResponse {
19
+ ok: false;
20
+ error_code: number;
21
+ description: string;
22
+ parameters?: {
23
+ retry_after?: number;
24
+ };
25
+ }
26
+ type ApiEnvelope<T> = ApiResponse<T> | ApiErrorResponse;
27
+ interface Capabilities {
28
+ platform: string;
29
+ api_version: string;
30
+ canonical_endpoint: string;
31
+ compatible_endpoint: string;
32
+ authorization: string;
33
+ idempotency_header: string;
34
+ webhook_secret_header: string;
35
+ update_delivery: string[];
36
+ mini_app_production_scheme: string;
37
+ methods: BotApiMethod[];
38
+ }
39
+ interface ClientOptions {
40
+ baseUrl?: string;
41
+ fetch?: typeof fetch;
42
+ requestTimeoutMs?: number;
43
+ useCompatibleEndpoint?: boolean;
44
+ }
45
+ interface RequestOptions {
46
+ requestId?: string;
47
+ signal?: AbortSignal;
48
+ }
49
+ interface User {
50
+ id: string;
51
+ is_bot: boolean;
52
+ first_name: string;
53
+ name?: string;
54
+ username?: string;
55
+ description?: string;
56
+ avatar_url?: string;
57
+ banner_url?: string;
58
+ is_premium?: boolean;
59
+ is_official?: boolean;
60
+ is_online?: boolean;
61
+ last_seen?: string;
62
+ bot_owner_id?: string;
63
+ can_join_groups?: boolean;
64
+ supports_inline_queries?: boolean;
65
+ privacy_mode?: boolean;
66
+ }
67
+ interface Chat {
68
+ id: string;
69
+ type: 'private' | 'group' | 'channel' | string;
70
+ title?: string;
71
+ username?: string;
72
+ description?: string;
73
+ avatar_url?: string;
74
+ member_count?: number;
75
+ }
76
+ interface MediaVariant {
77
+ variant_type?: string;
78
+ width?: number;
79
+ height?: number;
80
+ size?: number;
81
+ file_url?: string;
82
+ file_id?: string;
83
+ bitrate?: number;
84
+ quality?: string;
85
+ }
86
+ interface MediaFile {
87
+ type?: string;
88
+ media_type?: string;
89
+ file_id?: string;
90
+ file_url?: string;
91
+ thumbnail_url?: string;
92
+ duration?: number;
93
+ width?: number;
94
+ height?: number;
95
+ file_name?: string;
96
+ file_size?: number;
97
+ mime_type?: string;
98
+ bitrate?: number;
99
+ fps?: number;
100
+ codec?: string;
101
+ sticker_id?: string;
102
+ pack_id?: string;
103
+ sticker_type?: string;
104
+ associated_emojis?: string[];
105
+ variants?: MediaVariant[];
106
+ }
107
+ interface MessageEntity {
108
+ type: string;
109
+ offset: number;
110
+ length: number;
111
+ url?: string;
112
+ user?: User;
113
+ }
114
+ interface WebAppInfo {
115
+ url: string;
116
+ bot_id?: string;
117
+ mini_app_id?: string;
118
+ }
119
+ interface InlineKeyboardButton {
120
+ text: string;
121
+ url?: string;
122
+ callback_data?: string;
123
+ web_app?: WebAppInfo;
124
+ }
125
+ interface ReplyKeyboardButton {
126
+ text: string;
127
+ request_contact?: boolean;
128
+ request_location?: boolean;
129
+ web_app?: WebAppInfo;
130
+ }
131
+ interface InlineKeyboardMarkup {
132
+ inline_keyboard: InlineKeyboardButton[][];
133
+ }
134
+ interface ReplyKeyboardMarkup {
135
+ keyboard: ReplyKeyboardButton[][];
136
+ resize_keyboard?: boolean;
137
+ one_time_keyboard?: boolean;
138
+ selective?: boolean;
139
+ }
140
+ interface ReplyKeyboardRemove {
141
+ remove_keyboard: true;
142
+ selective?: boolean;
143
+ }
144
+ interface ForceReply {
145
+ force_reply: true;
146
+ selective?: boolean;
147
+ }
148
+ type ReplyMarkup = InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
149
+ interface WebAppData {
150
+ data: string;
151
+ button_text: string;
152
+ }
153
+ interface ForwardOrigin {
154
+ chat_id: string;
155
+ message_id: string;
156
+ sender_id?: string;
157
+ sender_name?: string;
158
+ sender_logo?: string;
159
+ date?: number;
160
+ forward_chain_count?: number;
161
+ original_message_id?: string;
162
+ }
163
+ interface ReactionCount {
164
+ reaction: string;
165
+ count: number;
166
+ reacted_by_you?: boolean;
167
+ }
168
+ interface Message {
169
+ message_id: string;
170
+ date: number;
171
+ timestamp?: number;
172
+ chat: Chat;
173
+ from?: User;
174
+ text?: string;
175
+ content?: string;
176
+ original_content?: string;
177
+ content_type?: string;
178
+ is_edited?: boolean;
179
+ is_bot_message?: boolean;
180
+ edit_date?: number;
181
+ caption?: string;
182
+ entities?: MessageEntity[];
183
+ reply_markup?: ReplyMarkup;
184
+ web_app_data?: WebAppData;
185
+ reply_to_message?: Message;
186
+ forward_origin?: ForwardOrigin;
187
+ via_bot?: User;
188
+ message_thread_id?: string;
189
+ temp_id?: string;
190
+ media_items?: MediaFile[];
191
+ photo?: MediaFile[];
192
+ video?: MediaFile;
193
+ video_note?: MediaFile;
194
+ audio?: MediaFile;
195
+ voice?: MediaFile;
196
+ document?: MediaFile;
197
+ animation?: MediaFile;
198
+ sticker?: MediaFile;
199
+ reactions?: ReactionCount[];
200
+ }
201
+ interface CallbackQuery {
202
+ id: string;
203
+ from: User;
204
+ message?: Message;
205
+ inline_message_id?: string;
206
+ chat_instance?: string;
207
+ data?: string;
208
+ game_short_name?: string;
209
+ }
210
+ interface Update {
211
+ update_id: number;
212
+ message?: Message;
213
+ edited_message?: Message;
214
+ callback_query?: CallbackQuery;
215
+ chat_member?: unknown;
216
+ my_chat_member?: unknown;
217
+ }
218
+ interface BotCommand {
219
+ command: string;
220
+ description: string;
221
+ }
222
+ interface WebhookInfo {
223
+ url: string;
224
+ has_custom_certificate: boolean;
225
+ pending_update_count: number;
226
+ last_error_date?: number;
227
+ last_error_message?: string;
228
+ max_connections?: number;
229
+ allowed_updates?: UpdateType[];
230
+ enabled?: boolean;
231
+ }
232
+ interface SetWebhookResult {
233
+ url: string;
234
+ enabled: boolean;
235
+ }
236
+ interface SendMessageOptions {
237
+ chat_id: string;
238
+ text?: string;
239
+ caption?: string;
240
+ parse_mode?: string;
241
+ disable_notification?: boolean;
242
+ reply_to_message_id?: string;
243
+ reply_markup?: ReplyMarkup;
244
+ requestId?: string;
245
+ }
246
+ interface SendMediaOptions {
247
+ chat_id: string;
248
+ caption?: string;
249
+ disable_notification?: boolean;
250
+ reply_to_message_id?: string;
251
+ reply_markup?: ReplyMarkup;
252
+ requestId?: string;
253
+ }
254
+ interface EditMessageTextOptions {
255
+ chat_id?: string;
256
+ message_id?: string;
257
+ inline_message_id?: string;
258
+ text: string;
259
+ parse_mode?: string;
260
+ reply_markup?: ReplyMarkup;
261
+ requestId?: string;
262
+ }
263
+ interface DeleteMessageOptions {
264
+ chat_id: string;
265
+ message_id: string;
266
+ requestId?: string;
267
+ }
268
+ interface SendChatActionOptions {
269
+ chat_id: string;
270
+ action: ChatAction;
271
+ requestId?: string;
272
+ }
273
+ interface GetUpdatesOptions {
274
+ offset?: number;
275
+ limit?: number;
276
+ timeout?: number;
277
+ allowed_updates?: UpdateType[];
278
+ requestId?: string;
279
+ signal?: AbortSignal;
280
+ }
281
+ interface SetWebhookOptions {
282
+ url: string;
283
+ secret_token?: string;
284
+ max_connections?: number;
285
+ allowed_updates?: UpdateType[];
286
+ drop_pending_updates?: boolean;
287
+ requestId?: string;
288
+ }
289
+ interface DeleteWebhookOptions {
290
+ drop_pending_updates?: boolean;
291
+ requestId?: string;
292
+ }
293
+ interface SetMyCommandsOptions {
294
+ commands: BotCommand[];
295
+ scope?: CommandScope;
296
+ language_code?: string;
297
+ requestId?: string;
298
+ }
299
+ interface GetMyCommandsOptions {
300
+ scope?: CommandScope;
301
+ language_code?: string;
302
+ requestId?: string;
303
+ }
304
+ interface DeleteMyCommandsOptions {
305
+ scope?: CommandScope;
306
+ language_code?: string;
307
+ requestId?: string;
308
+ }
309
+ interface AnswerCallbackQueryOptions {
310
+ callback_query_id: string;
311
+ text?: string;
312
+ show_alert?: boolean;
313
+ url?: string;
314
+ requestId?: string;
315
+ }
316
+ interface GetChatOptions {
317
+ chat_id: string;
318
+ requestId?: string;
319
+ }
320
+ type MediaInput = string | {
321
+ file_id?: string;
322
+ url?: string;
323
+ };
324
+ interface PollingOptions {
325
+ interval?: number;
326
+ timeout?: number;
327
+ limit?: number;
328
+ allowed_updates?: UpdateType[];
329
+ autoStart?: boolean;
330
+ }
331
+ interface WebhookServerOptions {
332
+ path?: string;
333
+ host?: string;
334
+ port?: number;
335
+ secretToken?: string;
336
+ }
337
+ interface BotOptions extends ClientOptions {
338
+ polling?: boolean | PollingOptions;
339
+ webhook?: false | WebhookServerOptions;
340
+ allowed_updates?: UpdateType[];
341
+ }
342
+ type MessageHandler = (message: Message, update: Update) => void | Promise<void>;
343
+ type EditedMessageHandler = (message: Message, update: Update) => void | Promise<void>;
344
+ type CallbackQueryHandler = (query: CallbackQuery, update: Update) => void | Promise<void>;
345
+ type UpdateHandler = (update: Update) => void | Promise<void>;
346
+ type ErrorHandler = (error: unknown) => void | Promise<void>;
347
+ type PollingErrorHandler = (error: unknown) => void | Promise<void>;
348
+ type BotEventMap = {
349
+ message: MessageHandler;
350
+ edited_message: EditedMessageHandler;
351
+ callback_query: CallbackQueryHandler;
352
+ update: UpdateHandler;
353
+ polling_error: PollingErrorHandler;
354
+ error: ErrorHandler;
355
+ };
356
+ type BotEvent = keyof BotEventMap;
357
+
358
+ declare class ApiClient {
359
+ readonly token: string;
360
+ readonly baseUrl: string;
361
+ readonly fetchImpl: typeof fetch;
362
+ readonly requestTimeoutMs: number;
363
+ readonly useCompatibleEndpoint: boolean;
364
+ constructor(token: string, options?: ClientOptions);
365
+ /**
366
+ * Returns the platform capabilities document.
367
+ */
368
+ getCapabilities(signal?: AbortSignal): Promise<Capabilities>;
369
+ /**
370
+ * Calls a Bot API method and returns the typed result.
371
+ */
372
+ callMethod<T>(method: string, payload?: Record<string, unknown>, options?: RequestOptions): Promise<T>;
373
+ /**
374
+ * Performs a raw Bot API request and returns the full envelope.
375
+ */
376
+ request<T>(method: string, payload?: Record<string, unknown>, options?: RequestOptions): Promise<ApiEnvelope<T> & {
377
+ ok: true;
378
+ result: T;
379
+ }>;
380
+ getMe(options?: RequestOptions): Promise<User>;
381
+ sendMessage(options: SendMessageOptions): Promise<Message>;
382
+ sendPhoto(photo: MediaInput, options: SendMediaOptions): Promise<Message>;
383
+ sendVideo(video: MediaInput, options: SendMediaOptions): Promise<Message>;
384
+ sendDocument(document: MediaInput, options: SendMediaOptions): Promise<Message>;
385
+ sendAudio(audio: MediaInput, options: SendMediaOptions): Promise<Message>;
386
+ sendVoice(voice: MediaInput, options: SendMediaOptions): Promise<Message>;
387
+ sendAnimation(animation: MediaInput, options: SendMediaOptions): Promise<Message>;
388
+ editMessageText(options: EditMessageTextOptions): Promise<Message>;
389
+ deleteMessage(options: DeleteMessageOptions): Promise<boolean>;
390
+ sendChatAction(options: SendChatActionOptions): Promise<boolean>;
391
+ getUpdates(options?: GetUpdatesOptions): Promise<Update[]>;
392
+ setWebhook(options: SetWebhookOptions): Promise<SetWebhookResult>;
393
+ deleteWebhook(options?: DeleteWebhookOptions): Promise<boolean>;
394
+ getWebhookInfo(options?: RequestOptions): Promise<WebhookInfo>;
395
+ setMyCommands(options: SetMyCommandsOptions): Promise<boolean>;
396
+ getMyCommands(options?: GetMyCommandsOptions): Promise<BotCommand[]>;
397
+ deleteMyCommands(options?: DeleteMyCommandsOptions): Promise<boolean>;
398
+ answerCallbackQuery(options: AnswerCallbackQueryOptions): Promise<boolean>;
399
+ getChat(options: GetChatOptions): Promise<Chat>;
400
+ private sendMedia;
401
+ private normalizeMedia;
402
+ private buildMethodUrl;
403
+ }
404
+
405
+ declare class RiseonlyBot extends ApiClient {
406
+ private readonly listeners;
407
+ private pollingOptions;
408
+ private webhookOptions?;
409
+ private polling;
410
+ private pollingAbort?;
411
+ private offset;
412
+ private webhookServer?;
413
+ constructor(token: string, options?: BotOptions);
414
+ /**
415
+ * Registers an event listener.
416
+ */
417
+ on<T extends BotEvent>(event: T, handler: BotEventMap[T]): this;
418
+ /**
419
+ * Removes a previously registered listener.
420
+ */
421
+ off<T extends BotEvent>(event: T, handler: BotEventMap[T]): this;
422
+ /**
423
+ * Registers a one-time event listener.
424
+ */
425
+ once<T extends BotEvent>(event: T, handler: BotEventMap[T]): this;
426
+ /**
427
+ * Starts long polling for bot updates.
428
+ */
429
+ startPolling(options?: PollingOptions): Promise<void>;
430
+ /**
431
+ * Stops long polling.
432
+ */
433
+ stopPolling(): Promise<void>;
434
+ /**
435
+ * Starts a built-in HTTP server that receives webhook updates.
436
+ */
437
+ startWebhook(options?: WebhookServerOptions): Promise<void>;
438
+ /**
439
+ * Stops the built-in webhook server.
440
+ */
441
+ stopWebhook(): Promise<void>;
442
+ /**
443
+ * Processes a single update and dispatches events.
444
+ */
445
+ processUpdate(update: Update): Promise<void>;
446
+ /**
447
+ * Sends a text message to a chat.
448
+ */
449
+ reply(message: Message, text: string, extra?: Record<string, unknown>): Promise<Message>;
450
+ /**
451
+ * Answers a callback query and optionally sends a toast.
452
+ */
453
+ answer(query: CallbackQuery, text?: string, showAlert?: boolean): Promise<boolean>;
454
+ private pollLoop;
455
+ private emit;
456
+ }
457
+
458
+ declare class RiseonlyError extends Error {
459
+ readonly code: number;
460
+ readonly retryAfter?: number;
461
+ readonly response?: unknown;
462
+ constructor(message: string, code: number, retryAfter?: number, response?: unknown);
463
+ static fromApiResponse(body: {
464
+ error_code: number;
465
+ description: string;
466
+ parameters?: {
467
+ retry_after?: number;
468
+ };
469
+ }): RiseonlyError;
470
+ }
471
+ declare class RiseonlyNetworkError extends RiseonlyError {
472
+ readonly cause?: unknown;
473
+ constructor(message: string, cause?: unknown, code?: number);
474
+ }
475
+ declare class RiseonlyTimeoutError extends RiseonlyNetworkError {
476
+ constructor(message?: string);
477
+ }
478
+
479
+ interface WebhookRequest {
480
+ headers: Record<string, string | string[] | undefined>;
481
+ body: unknown;
482
+ }
483
+ /**
484
+ * Parses a webhook request body into an Update object.
485
+ */
486
+ declare function parseWebhookUpdate(body: unknown): Update;
487
+ /**
488
+ * Verifies the webhook secret header sent by Riseonly.
489
+ */
490
+ declare function verifyWebhookSecret(headers: Record<string, string | string[] | undefined>, secretToken?: string): boolean;
491
+ /**
492
+ * Creates a minimal request handler for frameworks like Express or Fastify.
493
+ */
494
+ declare function createWebhookHandler(options: {
495
+ secretToken?: string;
496
+ onUpdate: (update: Update) => void | Promise<void>;
497
+ onError?: (error: unknown) => void | Promise<void>;
498
+ }): (request: WebhookRequest) => Promise<{
499
+ status: number;
500
+ body?: unknown;
501
+ }>;
502
+
503
+ interface InitDataFields {
504
+ [key: string]: string;
505
+ }
506
+ /**
507
+ * Parses URL-encoded init data into key/value fields.
508
+ */
509
+ declare function parseInitData(encoded: string): InitDataFields;
510
+ /**
511
+ * Builds the data check string used for init data signature verification.
512
+ */
513
+ declare function buildInitDataCheckString(fields: InitDataFields): string;
514
+ /**
515
+ * Derives the HMAC secret from a bot token.
516
+ */
517
+ declare function deriveInitDataSecret(botToken: string): Buffer;
518
+ /**
519
+ * Computes the expected init data hash for a bot token.
520
+ */
521
+ declare function signInitData(botToken: string, dataCheckString: string): string;
522
+ /**
523
+ * Verifies signed mini app init data.
524
+ */
525
+ declare function verifyInitData(botToken: string, encoded: string): InitDataFields;
526
+ /**
527
+ * Extracts init data from a launch URL hash fragment.
528
+ */
529
+ declare function extractInitDataFromLaunchUrl(launchUrl: string): string | null;
530
+ /**
531
+ * Creates a random idempotency key for Bot API requests.
532
+ */
533
+ declare function createRequestId(): string;
534
+
535
+ export { type AnswerCallbackQueryOptions, ApiClient, type ApiEnvelope, type ApiErrorResponse, type ApiResponse, type BotCommand, type BotEvent, type BotEventMap, type BotOptions, CANONICAL_API_PREFIX, CHAT_ACTIONS, COMMAND_SCOPES, type CallbackQuery, type CallbackQueryHandler, type Capabilities, type Chat, type ClientOptions, DEFAULT_API_BASE, type DeleteMessageOptions, type DeleteMyCommandsOptions, type DeleteWebhookOptions, type EditMessageTextOptions, type EditedMessageHandler, type ErrorHandler, type ForceReply, type ForwardOrigin, type GetChatOptions, type GetMyCommandsOptions, type GetUpdatesOptions, IDEMPOTENCY_HEADER, type InlineKeyboardButton, type InlineKeyboardMarkup, type MediaFile, type MediaInput, type MediaVariant, type Message, type MessageEntity, type MessageHandler, type PollingErrorHandler, type PollingOptions, type ReactionCount, type ReplyKeyboardButton, type ReplyKeyboardMarkup, type ReplyKeyboardRemove, type ReplyMarkup, type RequestOptions, RiseonlyBot, RiseonlyError, RiseonlyNetworkError, RiseonlyTimeoutError, SUPPORTED_METHODS, type SendChatActionOptions, type SendMediaOptions, type SendMessageOptions, type SetMyCommandsOptions, type SetWebhookOptions, type SetWebhookResult, UPDATE_TYPES, type Update, type UpdateHandler, type User, WEBHOOK_SECRET_HEADER, type WebAppData, type WebAppInfo, type WebhookInfo, type WebhookServerOptions, buildInitDataCheckString, createRequestId, createWebhookHandler, deriveInitDataSecret, extractInitDataFromLaunchUrl, parseInitData, parseWebhookUpdate, signInitData, verifyInitData, verifyWebhookSecret };