-
Notifications
You must be signed in to change notification settings - Fork 4
Permanent Cache Manager
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.
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);
});
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.
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
}
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 }));
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()
});
Parameters:
-
key
: The key of the cache value to remove.
Example:
ctx.pCache.removeValue('lastVisited');
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.
- Telesun
- Getting Started - Apps script
- Getting Started with the Telesun Ready Template
- Import Telesun
- Telesun-Handlers
- Context Object
- Next Middleware
- Stages
- Manager Classes:
- Mail:
- Google Spreadsheet:
- Best Practice:
- Getting Updates:
- Example Types:
- Example Bots: