Bots on the lil’bots platform can access users’ connected accounts like Google, Notion, Spotifty Github and others. This is extremly useful when you want your bots to be able to access users’ data or take action on their behalf via APIs.

Whenever a user installs a bot, they can connect their accounts to the bot. The bot can then access the user’s account data via the accounts object that is passed to the bot’s main function, as explained below.

The lil’bots plaform takes care of the OAuth flow and securely stores the user’s access tokens. It also makes sure to refresh the token when necessary so when the bot runs it can just start making API calls, without needing to worry about the OAuth flow.

Specifying Accounts in the Bot Spec

To access user accounts in your bot, you need to specify the accounts that your bot needs access to in the bot spec. This is done by adding an accounts field to the bot spec, with the list of accounts that the bot needs access to. Each account type is specified by the services main domain.

For example, if your bot needs access to the user’s Google account, you would specify the accounts field in the bot spec like this:

{
	"runtime": "deno",
	"main" : "script.mjs",
	"inputs": {},
	"params": {},
	"accounts": [
		{
			"provider": "google.com"
		}
	]
}

When the user installs this bot, they will be prompted to connect their Google account (if they haven’t already).

When the bot is called, the accounts array will be passed to the bot’s main function, with the user’s access token for the Google account. The bot can then use this access token to make API calls on behalf of the user. For example, this is what the accounts array might look like when passed to the bot’s main function:

[
    {
        "provider": "google.com",
        "access_token": "ya29.a0AXooCgsje7i47ScEM5VDI....."
    }
]

Supported Accounts

The following accounts are supported by the lil’bots platform:

Account TypeProviderDescription
Googlegoogle.comAccess the user’s Google account. Built in scope for Gmail, Google Drive, Calendar and other Google services.
Notionnotion.soAccess the user’s Notion account
Spotifyspotify.comAccess the user’s Spotify account
Githubgithub.comAccess the user’s Github account. Full admin access to GitHub accounts

Example - Accessing a User’s Google Calendar Events

Here’s an example of a bot that accesses a user’s Google Calendar events:

import moment from "https://deno.land/x/[email protected]/mod.ts";

export async function main(inputs, params, accounts) {
	let googleAccount = accounts.find(a => a.provider == "google.com");
	let startTime = moment().add(5, 'hours').toISOString() // start 5 hours from now
	let endTime = moment().add(10, 'days').toISOString() // next 10 day window
	let res = await fetch(`https://www.googleapis.com/calendar/v3/calendars/primary/events?timeMin=${startTime}&timeMax=${endTime}`, {
		headers: {
			"Authorization": `Bearer ${googleAccount.access_token}`
		}
	})
	res = await res.json();
	console.log(res)
	const events = res.items.map(ci => pick(ci, 'summary', 'start', 'end'))

	return {
		title: "Upcoming Events",
		rows: events
	}
}

const pick = (obj, ...keys) => Object.fromEntries(
  keys
  .filter(key => key in obj)
  .map(key => [key, obj[key]])
);