@wasserstoff/mangi-tg-bot
Version:
A powerful Telegram Bot SDK with built-in authentication, session management, and database integration
137 lines • 4.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.requireSessionAndChat = exports.initial = void 0;
exports.default = sessionMiddleware;
const initial = () => {
return {
jwtToken: undefined,
custom: {},
setCustom: () => { },
getCustom: () => undefined,
updateCustom: () => { },
deleteCustom: () => { },
};
};
exports.initial = initial;
//Manage your session data here and what you want your session look when it is created for a user you can add more variable here according to need
// YOU CAN ACCESS THIS SESSION DATA THROUGH REDIS AND ctx.session (telegram context)
async function sessionMiddleware(ctx, next) {
if (!ctx.session) {
ctx.session = {};
}
if (!ctx.session.custom) {
ctx.session.custom = {};
}
// CRUD helpers for session.custom
ctx.session.setCustom = function (key, value) {
if (!this.custom)
this.custom = {};
const keys = key.split(".");
let current = this.custom;
for (let i = 0; i < keys.length - 1; i++) {
const k = keys[i];
if (!current[k] || typeof current[k] !== "object") {
// Overwrite if path doesn't exist or is not an object
current[k] = {};
}
current = current[k];
}
current[keys[keys.length - 1]] = value;
if (typeof this.save === "function") {
this.save(() => { });
}
return true;
};
ctx.session.getCustom = function (key) {
if (!this.custom)
return undefined;
const keys = key.split(".");
let current = this.custom;
for (let i = 0; i < keys.length; i++) {
if (!current || typeof current !== "object")
return undefined;
current = current[keys[i]];
}
return current;
};
ctx.session.updateCustom = function (updates) {
if (!this.custom)
return false;
if (typeof updates !== "object" || updates === null)
return false;
for (const [fullKey, value] of Object.entries(updates)) {
const keys = fullKey.split(".");
let current = this.custom;
for (let i = 0; i < keys.length - 1; i++) {
const k = keys[i];
if (!(k in current) ||
typeof current[k] !== "object" ||
current[k] === null) {
return false; // Intermediate path is missing or invalid
}
current = current[k];
}
const finalKey = keys[keys.length - 1];
if (!(finalKey in current)) {
return false; // Final key does not exist
}
current[finalKey] = value;
}
if (typeof this.save === "function") {
this.save(() => { });
}
return true;
};
ctx.session.deleteCustom = function (key) {
if (!this.custom)
return false;
const keys = key.split(".");
let current = this.custom;
for (let i = 0; i < keys.length - 1; i++) {
if (!current[keys[i]] || typeof current[keys[i]] !== "object") {
return false;
}
current = current[keys[i]];
}
const lastKey = keys[keys.length - 1];
if (!(lastKey in current))
return false;
delete current[lastKey];
if (typeof this.save === "function") {
this.save(() => { });
}
return true;
};
if (typeof ctx.session.save !== "function") {
ctx.session.save = function (callback) {
const storage = ctx.__storageAdapter;
const sessionKey = ctx.__sessionKey;
if (storage && typeof storage.write === "function" && sessionKey) {
storage
.write(sessionKey, ctx.session)
.then(() => {
callback();
})
.catch((err) => {
callback(err);
});
}
else {
// Fallback: just log
callback();
}
};
}
await next();
}
const requireSessionAndChat = async (ctx, next) => {
if (!ctx.session)
throw new Error("Session is not initialized!");
if (!ctx.chat)
throw new Error("Chat is not available!");
if (!ctx.from)
throw new Error("From is not available!");
await next();
};
exports.requireSessionAndChat = requireSessionAndChat;
//# sourceMappingURL=session.js.map