Quick Start

This is a function reference for mailbots-sdk-js. View the readme.md file in the the repo that corresponds with this version. Also, view MailBots REST API and the MailBots API Docs for more information.

makeRequest

Low-level function to make authenticated request to MailBots API

makeRequest(requestOptions: object, cb: function?): Promise
Parameters
requestOptions (object) Axiox-compatible request ooptions
cb (function?) Optional callback.
Returns
Promise:

fromBot

Factory method to return a new, fully authenticated MailBots client based on the webhook

fromBot(bot: object)
Parameters
bot (object) MailBots bot object
Example
mailbot.onCommand('foo', bot => {
    const mbClient = MailBotsClient.fromBot(bot);
  })

mailbotGetSelf

Get information about the bot that corresponds with user's Bearer token (ie, most likely your bot).

mailbotGetSelf(cb: function?): Promise
Parameters
cb (function?) Optional callback
Returns
Promise:
Example
const res = await mbClient.mailbotGetSelf();

sendEvent

Send an Event to the bot. This does not require and auth token because the endpoint is meant for 3rd party services. Ex: issue created in Github, or an email response or support ticket received. The MailBots bot can listen for events, act on tasks or create or delete tasks based on events.

sendEvent(params: object, cb: any): Promise
Parameters
params (object) params
cb (any)
Returns
Promise:
Example
const res = await mbClient.sendEvent({type: 'event.type', payload: {"foo", "bar"}, event_url: "[unique_event_url]"});

sendInterbotEvent

An "Interbot Event" allows bots to send messages to each other. This allows a MailBot to expose actions that can be utilized by other MailBots (for example, creating an Evernote note) In this way, MailBots can become composable. Requires elevated oauth scope.

sendInterbotEvent(data: any, cb: any, params: object): Promise
Parameters
data (any)
cb (any)
params (object) params
Returns
Promise:
Example
const res = await mbClient.sendInterbotEvent({subdomain: 'git', payload: {"foo", "bar"}});

saveBotData

Save MailBot data which is sent with every webhook related to that bot. This is how a bot persist's user settings specific to that bot. For params and details, see bot saving data API docs

saveBotData(data: object, cb: any): Promise
Parameters
data (object) Nestable key value value pairs
cb (any)
Returns
Promise:
Example
const res = await mbClient.saveBotData({ foo: "bar" });

getBotData

Get saved MailBot data For params and details, see mailbots get data API docs

getBotData(cb: any)
Parameters
cb (any)
Example
const res = await mbClient.getBotData();

searchPeople

Get a filtered list of People

searchPeople(params: object, cb: function?): Promise
Parameters
params (object) Arguments for API call
cb (function?) Optional callback function
Returns
Promise:
Example
// Get all people
const res = await mbClient.searchPeople();
console.log(res.people);
// With a callback
mbClient.searchPeople({ limit: 1 }, (err, res) => {
    if (err) done(err);
    console.log(res.people);
  });

createPerson

Create a new person.

createPerson(params: any, cb: function?): Promise
Parameters
params (any)
cb (function?) Optional callback function
Returns
Promise:

getPerson

Get a person by it's ID.

getPerson(params: any, cb: function?, id: number): Promise
Parameters
params (any)
cb (function?) Optional callback function
id (number) The person ID
Returns
Promise:
Example
// Get person with id 3
const res = await mbClient.getPersonById(3);
console.log(res.person);

updatePerson

Update a person by id.

updatePerson(params: object, cb: function?): Promise
Parameters
params (object) Arguments for API call
cb (function?) Optional callback function
Returns
Promise:

batchUpdatePeople

Update multiple people with a single batch operation.

batchUpdatePeople(params: object, cb: function?): Promise
Parameters
params (object) Arguments for API call
cb (function?) Optional callback function
Returns
Promise:

createPersonEvent

Create a new person event.

createPersonEvent(params: object, cb: any): Promise
Parameters
params (object)
Name Description
params.type string The type of this event
params.title string Title of the event
params.body string Event content
params.links array Event content
cb (any)
Returns
Promise:

updatePersonTags

Update a person's tags attribute by id.

updatePersonTags(params: object, cb: function?): Promise
Parameters
params (object) Arguments for API call
Name Description
params.newTags Array<string>
params.replace boolean keep exising tags or replace them
cb (function?) Optional callback function
Returns
Promise:

mergePeople

Merge one or more people into a base person.

mergePeople(params: object, cb: any): Promise
Parameters
params (object)
Name Description
params.basePersonId number Id of the person to merge into
params.mergePersonIds Array<number> Ids of people to merge with
cb (any)
Returns
Promise:

getTasks

Get a filtered list of MailBots tasks

getTasks(params: object, cb: function?): Promise
Parameters
params (object) Arguments for API call
Name Description
params.suppress_webhook boolean Prevent MailBots from firing the task.viewed webhook
params.status boolean Retrieve completed or open tasks
cb (function?) Optional callback function
Returns
Promise:
Example
// Get all open tasks, sorted by due date
const res = await mbClient.getTasks();
console.log(res.tasks);
// With a callback
mbClient.getTasks({ limit: 1 }, (err, res) => {
    if (err) done(err);
    console.log(res.tasks);
  });

getTask

Get a MailBots task Passing ?verbose=1 fires a webhook to the extnesion and fetches a rendered HTML email preview of the task

getTask(params: object, cb: any): Promise
Parameters
params (object) request params
Name Description
params.id number taskid
params.verbose boolean Fires webhook to bot, returns rendered HTML emails in a "messages" array if the bot makes these available in the task.viewed webhook resopnse.
cb (any)
Returns
Promise:
Example
const res = await getTask({id: 123});

createTask

Create a new MailBots Task.

createTask(params: any, cb: any, object: any): Promise
Parameters
params (any)
cb (any)
object (any)
Returns
Promise:
Example
const res = await mbClient.createTask(
    {
      webhook: false,
      suppress_email: true,
      verbose: 1,
      task: {
        command: process.env.EXAMPLE_COMMAND
      },
      send_messages: [
        {
          type: "email",
          subject: "A test email message",
          to: "test@example.com",
          body: [
            {
              type: "html",
              html: "<h1>This is a test</h1>"
            }
          ]
        }
      ]
    });

sendEmail

Send email and automatically create a Task This creates a MailBots task and sends an email. It's a wrapper for createTask with opinionated settings for just sending email.

sendEmail(email: object, cb: any): Promise
Parameters
email (object) Email object. command and one recipient is required.
cb (any)
Returns
Promise:
Example
const res = await mbClient.sendEmail({
    command: "command@my-ext.eml.bot", // must be your bot's domain
    to: "test@exampletask.com",
    cc: [],
    bcc: [],
    from: "test@example.com",
    subject: "Test1",
    body: [
      {
        type: "html",
        text: "<h1>This is a test</h1>"
      }
    ]
  });

sendMessages

Send emails relating to an existing Task. Does not mutate task. NOTE: This may be deprecated in favor of sending messages via the update task endpoint.

sendMessages(params: object, cb: any)
Parameters
params (object)
Name Description
params.messages Array
cb (any)
Example
const res = await mbClient.sendMessages({
  task: {
    id: 123
  },
  send_messages: [{
    to: "test@exampletask.com",
    cc: [],
    bcc: [],
    from: "test@example.com",
    subject: "Test1",
    body: [
      {
        type: "html",
        text: "<h1>This is a test</h1>"
      }
    ]
  }]
 });

updateTask

Update A MailBots Task Used to save data against the task, update content, followup time and more

updateTask(params: any, cb: any, object: any): Promise
Parameters
params (any)
cb (any)
object (any)
Returns
Promise:
Example
const res = await mbClient.updateTask({
   task: {
    id: 1234,
    reference_email: {
      to: "newRecipient@exampletask.com"
    }
  }});

completeTask

Archive A MailBots Task

completeTask(params: number, cb: any): Promise
Parameters
params (number) Arguments
Name Description
params.task number Task object
params.task.id number Task id to complete
cb (any)
Returns
Promise:

deleteTask

Permanently Delete A MailBots Task

deleteTask(params: object, cb: any): Promise
Parameters
params (object) Task to delete
Name Description
params.task object Task object
params.task.id number Task id to delete
cb (any)
Returns
Promise:

triggerTask

Trigger a MailBots Task

triggerTask(params: object, cb: any): Promise
Parameters
params (object)
Name Description
params.trigger_url boolean Trigger URL of the task to trigger (get from task object)
params.verbose boolean? Fire webhook and render HTML email response
cb (any)
Returns
Promise:

naturalTime

Resolve Natural Time Format (ex: {naturaltime}@ext.eml.bot)

naturalTime(params: object, cb: any): Promise
Parameters
params (object) params object
Name Description
params.format string Time format to check (ex: 3days)
params.timezone string IANA timezone designation ( https://www.wikiwand.com/en/List_of_tz_database_time_zones ) ex: "America/Los_Angeles"
cb (any)
Returns
Promise:

sendAction

Dispatch an email-based action for a task. (Equivalent to sending an action email.)

sendAction(params: object, cb: any): Promise
Parameters
params (object)
Name Description
params.action string The action string
params.reference_email object The email that would be sent as the action email
params.verbose boolean? Include rendered HTML email contents in API response
cb (any)
Returns
Promise:

createEvent

Create a new task event.

createEvent(params: object, cb: any): Promise
Parameters
params (object)
Name Description
params.type string The type of this event
params.data object Data associated with this event.
cb (any)
Returns
Promise:

getLoggedInUser

Get information about the currently logged in user

getLoggedInUser(cb: function?)
Parameters
cb (function?) Optional callback

validateWebhook

Validate webhook signature. Set verifyAge to false when testing / mocking HTTP requests. (Server side only)

validateWebhook(webhookSignature: string, webhookTimestamp: number, rawBody: string, verifyAge: boolean?): boolean
Parameters
webhookSignature (string)
webhookTimestamp (number) Unix Timestamp of webhook. Used to prevent reply attack
rawBody (string) Unprocessed http post body
verifyAge (boolean? = true) Use for automated testing
Returns
boolean: Pass / fail webhook validation