Botmation Documentation
Cookies
Cookies
These BotActions provide the means to manage cookies from a Puppeteer page.
BotFileOptions
is used to specify the directory for saving and loading cookies, relative to the bot's executing directory.
These functions are compatible with the higher-order files()() BotAction to inject a customized BotFileOptions
into each assembled BotAction. That allows you to standardize the directory for all assembled BotActions that are reading and writing files.
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.
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.
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.
Get Cookies
This BotAction returns all cookies for the inputted URL's. When no URL's are specified, it will return the cookies for the current URL only.
const getCookies = (...urls: string[]): BotAction<Protocol.Network.Cookie[]> => async(page) => page.cookies(...urls)
Delete Cookies
This BotAction deletes all cookies provided.
const deleteCookies = (...cookies: Protocol.Network.Cookie[]): BotAction => async(page, ...injects) => { if (cookies.length === 0) { if (injectsHavePipe(injects)) { const pipeValue = getInjectsPipeValue(injects) if (Array.isArray(pipeValue) && pipeValue.length > 0) { cookies = pipeValue } } }
if (cookies.length > 0) { return page.deleteCookie(...cookies) }}
It's intended to be paired with getCookies to designate which cookies are to be deleted. For example:
pipe()( getCookies(), deleteCookies())(page)
This will grab all cookies, for the current URL, then pipe those cookies into deleteCookies
for deletion. Otherwise, specify the cookies via the higher-order parameter:
chain( deleteCookies(cookie1, cookie2, cookie3))(page)
Edit this page on GitHubIf both higher-order params and pipe values are used, this will, like every other BotAction, use the higher-order param instead