Botmation Documentation

Instagram

Instagram

These functions focus on Instagram's web app.

Overview

The current set of BotActions facilitate the login flow. Here is a working example.

These are all included in the @botmation/instagram package.

Install

In addition to installing @botmation/core, install @botmation/instagram:

npm i -s @botmation/instagram

Now you can import any of these BotActions or selectors listed on this page, along with any other BotActions listed.

Auth

These BotAction's facilitate the login process of Instagram's web app.

login()

const login = ({username, password}: {username: string, password: string}): BotAction =>
chain(
errors('Instagram login()')(
goTo(getInstagramLoginUrl()),
click(FORM_AUTH_USERNAME_INPUT_SELECTOR),
type(username),
click(FORM_AUTH_PASSWORD_INPUT_SELECTOR),
type(password),
click(FORM_AUTH_SUBMIT_BUTTON_SELECTOR),
waitForNavigation,
wait(1000),
log('Login Complete')
)
)

This BotAction is a composition that uses errors()() to wrap the main assembled BotAction's, in case something goes wrong here, dev's will have an easier time knowing if errors are thrown here.

The composition is declarative, therefore needs little explaining. login() is a higher-order function that takes a typed object for the username and password.

isGuest

const isGuest: ConditionalBotAction =
indexedDBStore('redux', 'paths')(
getIndexedDBValue('users.viewerId'),
map(viewerId => viewerId ? false : true),
)

This BotAction is a composition that uses indexedDBStore()() to assemble BotAction's with the IndexedDB Database name and Store name injected. This simply grabs the value for the users.viewerId key then maps it to the corresponding boolean value.

isLoggedIn

const isLoggedIn: ConditionalBotAction =
indexedDBStore('redux', 'paths')(
getIndexedDBValue('users.viewerId'),
map(viewerId => viewerId ? true : false)
)

This BotAction is a composition that uses indexedDBStore()() to assemble BotAction's with the IndexedDB Database name and Store name injected. This simply grabs the value for the users.viewerId key then maps it to the corresponding boolean value.

Modals

These BotAction's help handle Instagram's Modals.

isTurnOnNotificationsModalActive

const isTurnOnNotificationsModalActive: ConditionalBotAction = async(page) => {
const modalHeader = await page.$(MAIN_MODAL_HEADER_SELECTOR)
const modalHeaderText = await page.evaluate(el => el === null || el.textContent === null ? '' : el.textContent, modalHeader)
return modalHeader !== null && modalHeaderText === TURN_OFF_NOTIFICATIONS_MODAL_HEADER_TEXT
}

Sometimes after login, Instagram prompts the User with a modal about turning on notifications. This ConditionalBotAction returns true if that modal is open.

closeTurnOnNotificationsModal

const closeTurnOnNotificationsModal: BotAction = async (page) => {
// click button with text "Not Now" inside the dialog
const [button] = await page.$x("//button[contains(., '" + TURN_OFF_NOTIFICATIONS_BUTTON_LABEL + "')]")
if (button) {
await button.click()
}
}

This BotAction will close the "Turn on Notifications" modal. Please use givenThat()() with isTurnOnNotificationsModalActive to run this BotAction only if that modal is open.

Selectors

Helpful HTML element selectors in the Instagram web app, can be imported directly from the main botmation package.

These are the following:

// Auth - Login Page
const FORM_AUTH_USERNAME_INPUT_SELECTOR = 'html body section main form input[name="username"]'
const FORM_AUTH_PASSWORD_INPUT_SELECTOR = 'html body section main form input[name="password"]'
const FORM_AUTH_SUBMIT_BUTTON_SELECTOR = 'html body section main form button[type="submit"]'
// Modals - Home Page
const MAIN_MODAL_SELECTOR = 'html body div[role="dialog"]'
const MAIN_MODAL_HEADER_SELECTOR = 'html body div[role="dialog"] h2'

Helpers

getInstagramBaseUrl()

const getInstagramBaseUrl = () =>
INSTAGRAM_BASE_URL

Returns Instagram's web app's base URL without a trailing slash, ie: https://www.instagram.com

getInstagramLoginUrl()

const getInstagramLoginUrl = () =>
getInstagramBaseUrl() + INSTAGRAM_URL_EXT_LOGIN + '/'

Returns Instagram's web app's URL for its login page

Edit this page on GitHub
Baby Bot