Skip to content

Permanent Cache Manager

Abdi Urgessa edited this page Apr 21, 2024 · 5 revisions

The Permanent Cache Manager facilitates seamless interaction with Google Apps Script's Cache Service, providing methods to get, set, and remove values in a persistent manner.

Example Usage

Here's how you might initialize your bot and use the PermanentCacheManager within an event listener:

const telesun = new Telesun.config({
      telegram: '7172550832:AAEmrW******1s1P-GsLAVw',
      pmemory: PropertiesService.getScriptProperties(),
    });

telesun.on("message", (ctx) => {
   const welcomeMessage = ctx.pCache.getValue('welcomeMessage');
   ctx.reply(welcomeMessage);
});

Methods Overview

The PermanentCacheManager includes several methods for interacting with the cache:

  • getValue(key): Retrieves a value from the cache using its key.
  • getCache(): Retrieves all key-value pairs stored in the permanent cache.
  • setValue(key, value): Stores a value in the cache under the specified key.
  • setValues(objectOfKeyValuePairs): Stores multiple key-value pairs in the cache at once.
  • removeValue(key): Removes a specific key-value pair from the cache.
  • delete(): Clears all data stored in the permanent cache.

Detailed Method Usage

getValue

Parameters:

  • key: The key for the cache value to retrieve.

Example:

let userSettings = ctx.pCache.getValue('userSettings');
if (!userSettings) {
  // Default settings or prompt user for settings
}
setValue

Parameters:

  • key: The key under which to store the value.
  • value: The value to store in the cache.

Example:

ctx.pCache.setValue('userSettings', JSON.stringify({ theme: 'dark', notifications: true }));
setValues

Parameters:

  • objectOfKeyValuePairs: An object containing keys and values to store in the cache.

Example:

ctx.pCache.setValues({
  userSettings: JSON.stringify({ theme: 'dark', notifications: true }),
  lastVisited: new Date().toISOString()
});
removeValue

Parameters:

  • key: The key of the cache value to remove.

Example:

ctx.pCache.removeValue('lastVisited');

Summary

The PermanentCacheManager is an essential tool for developers working with Telegram bots on the Google Apps Script platform, providing a robust and flexible way to manage persistent data storage. Its suite of methods enables bots to remember information about users, sessions, or any other data that needs to be retained over time, thus enhancing the overall user experience and functionality of the bot.

Clone this wiki locally