How to create a custom session store ? #340
-
Hello there, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello there, too, @itishermann! Creating a custom session store is as easy as implementing your own session class by extending the abstract Here's how you would implement your own session store: import { Session } from "telegram/sessions";
import { AuthKey } from "telegram/crypto/AuthKey";
export class MySession extends Session {
setDC(dcId: number, serverAddress: string, port: number) {
// save dcId, serverAddress and port in your database
}
get dcId(): number {
// return the saved dcId
}
get serverAddress(): string {
// return the saved serverAddress
}
get port(): number {
// return the saved port
}
get authKey(): AuthKey | undefined {
// return the saved authKey
}
async load(): Promise<void> {
// This is called before your session storage is used/
// You can do any required initialization here.
}
setAuthKey(authKey?: AuthKey, dcId?: number): void {
// Save the authKey in your database.
// You can use authKey?.getKey
}
getAuthKey(dcId?: number): AuthKey | undefined {
// return the saved authKey
}
getInputEntity(key: EntityLike): Api.TypeInputPeer {
// From the JSDoc:
/**
* Turns the given key into an ``InputPeer`` (e.g. ``InputPeerUser``).
* The library uses this method whenever an ``InputPeer`` is needed
* to suit several purposes (e.g. user only provided its ID or wishes
* to use a cached username to avoid extra RPC).
*/
}
close(): void {
// From the JSDoc:
/**
* Called on client disconnection. Should be used to
* free any used resources. Can be left empty if none.
*/
}
save(): void {
// From the JSDoc:
/**
* called whenever important properties change. It should
* make persist the relevant session information to disk.
*/
}
delete(): void {
// From the JSDoc:
/**
* Called upon client.log_out(). Should delete the stored
* information from disk since it's not valid anymore.
*/
}
processEntities(tlo: any): void {
// From the JSDoc:
/**
* Processes the input ``TLObject`` or ``list`` and saves
* whatever information is relevant (e.g., ID or access hash).
* @param tlo
*/
}
} Also note that if you don't want to store some part like the entities, you can simply extend the |
Beta Was this translation helpful? Give feedback.
Hello there, too, @itishermann!
Creating a custom session store is as easy as implementing your own session class by extending the abstract
Session
class which is intelegram/sessions
.Here's how you would implement your own session store: