Botmation Documentation
Cookies
Cookies
These higher order functions provide a simple way to load and save cookies from and to a Puppeteer page to a local JSON file.
You can configure where the cookie JSON files are saved & loaded by using the BotFileOptions
param as either an inject or as a higher-order param. Higher-order BotFileOptions
values override injected BotFileOptions
values.
These functions are compatible with the higher-order files()() BotAction to inject a customized BotFileOptions
into each assembled BotAction.
Save Cookies
Saves the cookies from the Puppeteer page in a JSON file with the name provided.
const saveCookies = (fileName: string, botFileOptions?: Partial<BotFileOptions>): BotFilesAction => async(page, options: Partial<BotFileOptions>) => { const hydratedOptions = enrichBotFileOptionsWithDefaults({...options, ...botFileOptions}) const cookies = await page.cookies() await fs.writeFile(getFileUrl(hydratedOptions.cookies_directory, hydratedOptions, fileName) + '.json', JSON.stringify(cookies, null, 2))}
BotFileOptions
is optional, in all cases (higher-order param, as an inject). If nothing is provided, the default is the local directory.
TODO: link vv See enrichBotFileOptionsWithDefaults for details on setting the directory for files read and created.
For an usage example, see the Instagram example, where saving and loading cookies enables the Bot to skip the login flow on subsequent runs, as long as the cookies saved, have not yet expired.
Load Cookies
Loads the cookies from the file name specified into the Puppeteer page.
const loadCookies = (fileName: string, botFileOptions?: Partial<BotFileOptions>): BotFilesAction => async(page, options) => { const hydratedOptions = enrichBotFileOptionsWithDefaults({...options, ...botFileOptions}) const file = await fs.readFile(getFileUrl(hydratedOptions.cookies_directory, hydratedOptions, fileName) + '.json') const cookies = JSON.parse(file.toString()) for (const cookie of cookies) { await page.setCookie(cookie) }}
BotFileOptions
is optional, in all cases (higher-order param, as an inject). If nothing is provided, the default is the local directory.
TODO: link vv See enrichBotFileOptionsWithDefaults for details on setting the directory for files read and created.
For an usage example, see the Instagram example, where saving and loading cookies enables the Bot to skip the login flow on subsequent runs, as long as the cookies saved, have not yet expired.
Edit this page on GitHub