Botmation Documentation

Examples

Examples

In the apps directory of Botmation's repo, exists a small collection of copypasta bots to help you bootstrap faster 🚀

Example

The simplest one is the Example app that takes a screenshot:

import puppeteer from 'puppeteer'
import {
chain,
goTo,
screenshot,
log,
logError
} from '@botmation/core'
(async () => {
let browser: puppeteer.Browser
let page: puppeteer.Page
try {
browser = await puppeteer.launch({headless: false})
const pages = await browser.pages()
page = pages.length === 0 ? await browser.newPage() : pages[0]
await chain(
log('Bot is running'),
goTo('https://www.duckduckgo.com/'),
screenshot('duckduckgo-homepage'),
log('Screenshot taken')
)(page)
await page.close()
} catch (error) {
logError(error)
setTimeout(async() => {
if (page) await page.close()
})
}
})()

PDF Bot

The PDF app is built with only functions from the Core package:

import puppeteer from 'puppeteer'
import {
chain,
log,
goTo,
savePDF,
logError
} from '@botmation/core'
(async () => {
try {
let browser: puppeteer.Browser
// headless: false breaks PDF saving; therefore it MUST be `true`
// see https://github.com/puppeteer/puppeteer/issues/1829
browser = await puppeteer.launch({headless: true})
const page = await browser.newPage()
await chain(
log('Lets save a PDF of DuckDuckGo Homepage'),
goTo('http://duckduckgo.com/'),
savePDF('duckduckgo-homepage'),
log('PDF saved')
)(page)
await browser.close()
} catch(error) {
logError(error)
}
})();

Object-Oriented Bot

Classes are great for encapsulating functionality. It's possible to use Botmation with classes. For example, here we compose an instance method that operates on the bot's page:

import puppeteer from 'puppeteer'
import {
chain,
goTo,
screenshot,
logError
} from '@botmation/core'
//
// Bot that takes screenshots
class PhotoBot {
private page: puppeteer.Page;
constructor(page: puppeteer.Page) {
this.page = page;
}
public async closePage() {
if (this.page) {
await this.page.close()
this.page = undefined
}
}
public async takeAPhoto(url: string) {
const fileName = new Date().getTime().toString();
await chain(
goTo(url),
screenshot(fileName)
)(this.page)
}
}
//
// Code to initialize the bots, then take screenshots
(async () => {
let browser: puppeteer.Browser
try {
browser = await puppeteer.launch({ headless: false })
const page = await browser.newPage()
const bot = new PhotoBot(page)
await bot.takeAPhoto('http://duckduckgo.com')
await bot.takeAPhoto('http://google.com')
await bot.takeAPhoto('http://bing.com')
await bot.closePage()
await browser.close()
} catch (error) {
logError(error)
setTimeout(async() => {
if (browser) await browser.close()
})
}
})()
Edit this page on GitHub
Baby Bot