autopilot-github

How can we help?

fluent-robot
 

Automation Cloud

Robot API quickstart (cloud version)

Robot API

The Robot API is an open source node.js library which facilitates running automations.

Prerequisites

  1. an Automation Cloud account.

Create a free acount here: TODO

  1. a working script in the dashboard

Make sure the Automation script is uploaded here https://dashboard.automationcloud.net/services (e.g. sample duckduckgo script).

The serviceId will be needed, that is the uuid in here:

  1. a client with a known SECRET_KEY https://dashboard.automationcloud.net/clients

Immediately after the client is created, the secret key is returned. This is typically used as an environment variable named SECRET_KEY.

  1. client authorisation to run the service

Documentation

All the available methods and sample code are available in the project’s readme https://github.com/automationcloud/robot. This guide uses a relevant subset of functionality that Robot API makes available to users.

Running in the cloud

The following example runs a search on https://duckduckgo.com/, clicks on a search result and returns the first heading found, all from the command line interface.

Setting things up

The library is first initialised with the serviceId and auth

const { CloudRobot } = require('@automationcloud/cloud-robot');

const robot = new CloudRobot({
    serviceId: '0fdb77e6-2871-4b13-9e98-cc62a60e4af7',
    auth: 'SECRET_KEY'
});

Creating a job

Call createJob with the initial set of data:

const job = await robot.createJob({ input: { options: {} });

This call returns a Promise. In a synchronous function, however, it may be chained:

robot.createJob().then(async job => {
  console.log('Job created', job);
})
  .catch(err => {
    console.log('error processing job', err);
    process.exit(1);
  });

Submitting an input

Once a job has been created with its initial set of inputs, additional inputs can be supplied using job.submitInput. Using inquirer, the user needs to supply the search string:

    const { search } = await inquirer.prompt([{
        type: 'input',
        name: 'search',
        message: 'What should your job search for?'
    }]);

    await job.submitInput('search', search);

Waiting for an output

Jobs are interactive, and some inputs depend on outputs. In this case, the user needs to be presented with the search results in order to pick one:

    const [ availableSearchResults ] = await job.waitForOutputs('availableSearchResults');
    const { selectedSearchResult } = await inquirer.prompt([{
        type: 'list',
        name: 'selectedSearchResult',
        message: 'Pick a search result to visit',
        choices: availableSearchResults
    }]);

Waiting for an input

Inputs can be submitted directly, as shown above. However, to have users supply inputs only once they are needed, first wait for the input to be required:

    job.onAwaitingInput('selectedSearchResult', async () => {
        const [ availableSearchResults ] = await job.waitForOutputs('availableSearchResults');
        const { selectedSearchResult } = await inquirer.prompt([{
            type: 'list',
            name: 'selectedSearchResult',
            message: 'Pick a search result to visit',
            choices: availableSearchResults
        }]);
        return selectedSearchResult;
    });

The code, put together

'use strict';

const { CloudRobot } = require('@automationcloud/cloud-robot');
const inquirer = require('inquirer');

const robot = new CloudRobot({
    serviceId: '0fdb77e6-2871-4b13-9e98-cc62a60e4af7',
    auth: SECRET_KEY
});

robot.createJob().then(async job => {
    const { search } = await inquirer.prompt([{
        type: 'input',
        name: 'search',
        message: 'What should your job search for?'
    }]);

    await job.submitInput('search', search);

    job.onAwaitingInput('selectedSearchResult', async () => {
        const [ availableSearchResults ] = await job.waitForOutputs('availableSearchResults');
        const { selectedSearchResult } = await inquirer.prompt([{
            type: 'list',
            name: 'selectedSearchResult',
            message: 'Pick a search result to visit',
            choices: availableSearchResults
        }]);
        return selectedSearchResult;
    });

    const [ title ] = await job.waitForOutputs('title');
    console.log(title);
})
    .catch(err => {
        console.log('error processing job', err);
        process.exit(1);
    });

The source code can also be cloned from github https://github.com/randunel/d2go-robot-api.