@stack.thefennec.dev/telegram-export-parser
Version:
TypeScript library for parsing Telegram Desktop's data export with full type safety
251 lines • 9.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GROUP_CHANGE_ACTIONS = exports.CREATION_ACTIONS = exports.MEMBERSHIP_ACTIONS = exports.CALL_ACTIONS = exports.CALL_DISCARD_REASONS = exports.EVENT_ACTIONS = void 0;
exports.isEvent = isEvent;
exports.isCallEvent = isCallEvent;
exports.isCallScheduleEvent = isCallScheduleEvent;
exports.isMembershipEvent = isMembershipEvent;
exports.isCreationEvent = isCreationEvent;
exports.isEditGroupEvent = isEditGroupEvent;
exports.isMessagePinEvent = isMessagePinEvent;
exports.isSendPremiumGiftEvent = isSendPremiumGiftEvent;
exports.isPaidMessagesRefundEvent = isPaidMessagesRefundEvent;
exports.isScoreInGameEvent = isScoreInGameEvent;
exports.isClearHistoryEvent = isClearHistoryEvent;
exports.isScreenshotEvent = isScreenshotEvent;
// =====================================================
// EVENT ACTION CONSTANTS
// =====================================================
/**
* All possible Telegram service event action types.
*
* These constants represent the different types of service messages
* that Telegram can generate (non-user messages that indicate system events).
*
* @example
* ```typescript
* if (event.action === EVENT_ACTIONS.PHONE_CALL) {
* // Handle phone call event
* }
* ```
*/
exports.EVENT_ACTIONS = {
/** Phone call initiated between users */
PHONE_CALL: "phone_call",
/** Group voice/video call started */
GROUP_CALL: "group_call",
/** Group call scheduled for future time */
GROUP_CALL_SCHEDULED: "group_call_scheduled",
/** Users invited to group/channel */
INVITE_MEMBERS: "invite_members",
/** Users removed from group/channel */
REMOVE_MEMBERS: "remove_members",
/** Users invited to ongoing group call */
INVITE_TO_GROUP_CALL: "invite_to_group_call",
/** User joined group via invite link */
JOIN_GROUP_BY_LINK: "join_group_by_link",
/** User joined group via admin approval */
JOIN_GROUP_BY_REQUEST: "join_group_by_request",
/** New group chat created */
CREATE_GROUP: "create_group",
/** New channel created */
CREATE_CHANNEL: "create_channel",
/** Group title/name changed */
EDIT_GROUP_TITLE: "edit_group_title",
/** Group photo/avatar changed */
EDIT_GROUP_PHOTO: "edit_group_photo",
/** Group migrated from basic to supergroup */
MIGRATE_FROM_GROUP: "migrate_from_group",
/** Group converted to supergroup */
MIGRATE_TO_SUPERGROUP: "migrate_to_supergroup",
/** Message pinned in chat */
PIN_MESSAGE: "pin_message",
/** Premium subscription gifted to user */
SEND_PREMIUM_GIFT: "send_premium_gift",
/** Paid message refund processed */
PAID_MESSAGES_REFUND: "paid_messages_refund",
/** Game score achieved */
SCORE_IN_GAME: "score_in_game",
/** Chat history cleared */
CLEAR_HISTORY: "clear_history",
/** Screenshot taken (secret chat) */
TAKE_SCREENSHOT: "take_screenshot"
};
/**
* Possible reasons why a call was terminated.
* Used with phone calls and group calls to indicate how they ended.
*/
exports.CALL_DISCARD_REASONS = [
/** Call ended normally by participant */
"hangup",
/** Call was not answered */
"missed",
/** Recipient was busy */
"busy"
];
// =====================================================
// GROUPED ACTION CONSTANTS
// =====================================================
/**
* Actions related to voice/video calls.
* Convenience array for type checking and filtering call-related events.
*/
exports.CALL_ACTIONS = [
exports.EVENT_ACTIONS.PHONE_CALL,
exports.EVENT_ACTIONS.GROUP_CALL
];
/**
* Actions related to group/channel membership changes.
* Includes invites, removals, and different ways users can join.
*/
exports.MEMBERSHIP_ACTIONS = [
exports.EVENT_ACTIONS.INVITE_MEMBERS,
exports.EVENT_ACTIONS.REMOVE_MEMBERS,
exports.EVENT_ACTIONS.INVITE_TO_GROUP_CALL,
exports.EVENT_ACTIONS.JOIN_GROUP_BY_LINK,
exports.EVENT_ACTIONS.JOIN_GROUP_BY_REQUEST
];
/**
* Actions related to creating new groups or channels.
* Used when new chat entities are established.
*/
exports.CREATION_ACTIONS = [
exports.EVENT_ACTIONS.CREATE_GROUP,
exports.EVENT_ACTIONS.CREATE_CHANNEL
];
/**
* Actions related to modifying existing group properties.
* Includes title changes, photo updates, and migration events.
*/
exports.GROUP_CHANGE_ACTIONS = [
exports.EVENT_ACTIONS.EDIT_GROUP_TITLE,
exports.EVENT_ACTIONS.EDIT_GROUP_PHOTO,
exports.EVENT_ACTIONS.MIGRATE_FROM_GROUP,
exports.EVENT_ACTIONS.MIGRATE_TO_SUPERGROUP
];
// =====================================================
// TYPE GUARD FUNCTIONS
// =====================================================
/**
* Type guard to check if a message or event is a TelegramEvent (service message).
*
* Service events represent system actions rather than user messages.
* This is the primary way to distinguish between regular messages and events.
*
* @param msg - The message or event to check
* @returns True if the input is a TelegramEvent, false otherwise
*
* @example
* ```typescript
* if (isEvent(item)) {
* // Handle service event
* console.log(`Service event: ${item.action}`)
* } else {
* // Handle regular message
* console.log(`User message: ${item.text}`)
* }
* ```
*/
function isEvent(msg) {
return msg.type === "service";
}
/**
* Type guard to check if an event is a CallEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a CallEvent, false otherwise
*/
function isCallEvent(msg) {
return msg.type === "service" && "action" in msg && (msg.action === "phone_call" || msg.action === "group_call");
}
/**
* Type guard to check if an event is a CallScheduleEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a CallScheduleEvent, false otherwise
*/
function isCallScheduleEvent(msg) {
return msg.type === "service" && "action" in msg && msg.action === "group_call_scheduled";
}
/**
* Type guard to check if an event is a MembershipEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a MembershipEvent, false otherwise
*/
function isMembershipEvent(msg) {
return msg.type === "service" && "action" in msg && (msg.action === "invite_members" || msg.action === "remove_members" || msg.action === "invite_to_group_call");
}
/**
* Type guard to check if an event is a CreationEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a CreationEvent, false otherwise
*/
function isCreationEvent(msg) {
return msg.type === "service" && "action" in msg && (msg.action === "create_group" || msg.action === "create_channel");
}
/**
* Type guard to check if an event is a GroupChangeEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a GroupChangeEvent, false otherwise
*/
function isEditGroupEvent(msg) {
return msg.type === "service" && "action" in msg && (msg.action === "edit_group_title" || msg.action === "edit_group_photo");
}
/**
* Type guard to check if an event is a MessagePinEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a MessagePinEvent, false otherwise
*/
function isMessagePinEvent(msg) {
return msg.type === "service" && "action" in msg && msg.action === "pin_message";
}
/**
* Type guard to check if an event is a SendPremiumGiftEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a SendPremiumGiftEvent, false otherwise
*/
function isSendPremiumGiftEvent(msg) {
return msg.type === "service" && "action" in msg && msg.action === "send_premium_gift";
}
/**
* Type guard to check if an event is a PaidMessagesRefundEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a PaidMessagesRefundEvent, false otherwise
*/
function isPaidMessagesRefundEvent(msg) {
return msg.type === "service" && "action" in msg && msg.action === "paid_messages_refund";
}
/**
* Type guard to check if an event is a GameScoreEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a GameScoreEvent, false otherwise
*/
function isScoreInGameEvent(msg) {
return msg.type === "service" && "action" in msg && msg.action === "score_in_game";
}
/**
* Type guard to check if an event is a ClearHistoryEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a ClearHistoryEvent, false otherwise
*/
function isClearHistoryEvent(msg) {
return msg.type === "service" && "action" in msg && msg.action === "clear_history";
}
/**
* Type guard to check if an event is a ScreenshotEvent.
*
* @param msg - The message or event to check
* @returns True if the input is a ScreenshotEvent, false otherwise
*/
function isScreenshotEvent(msg) {
return msg.type === "service" && "action" in msg && msg.action === "take_screenshot";
}
//# sourceMappingURL=events.js.map