Skip to content
Nirmal Lanka edited this page Mar 20, 2018 · 6 revisions

Evilbot library documentation

Requirements

The .env file contains the app's PAGE_ACCESS_TOKEN given by Messenger Platform and VERIFY_TOKEN contains a token of your choice that is reflected in the App's Messenger integration config.

The dependencies for the current version are as follows:

{
  ...,
  "dependencies": {
    "express": "4.16.2",
    "body-parser": "1.18.2",
    "request": "2.83.0",
    "rxjs": "5.5.6"
  },
  ...
}

Basic bot

const app = require('./evil/core')

const SendService = {
  send: function (userId, msg) { 
    return this.$.send.text(userId, msg)
  }
}

const EchoHandler = {
  setup: function () { 
    this.$.listenTo.text(_ => {
      SendService.send(_.userId, _.message).subscribe()
    })
  }
}

app({
  interactions: [
    EchoHandler,
  ],
  services: [
    SendService,
  ]
})

Concepts in EvilBot

Interaction

Also called a handler, is the primary component type of Evilbot. An interaction is the starting point of a conversation flow.

It must contain a setup() that is the primary entry point of that interaction chain.

const EchoHandler = {
  setup: function () { 
    this.$.listenTo.text(_ => {
      SendService.send(_.userId, _.message).subscribe()
    })
  }
}

Then, the interaction must be registered at the app object (require("./evil/core")) like this:

app({
  interactions: [
    EchoHandler,
  ],
})

In the above example, the setup function uses the Evilbot listener API method .listenTo.text(__) exposed through through this.$.

Other listeners include getStarted(f), postback(f) and attachment(f) that will be triggered when relevant types of messages are sent by the developer. Then the callback function is called with an object containing the message context as a parameter. It contains information such as userId and message if it's a text message.

The SendService is a service abstraction written by the developer. See Service section below.

Service

Services should contain the domain logic of the bot. They can be used to abstract API calls as well as rich response construction.

Evilbot APIs such as .send and .factories are exposed in a service's this.$.

const SendService = {
  sendText: function (userId, msg) { 
    return this.$.send.text(userId, msg)
  }
}

In the above example, the SendService utilizes the .send API to send a text message to the user.

The factories API will contain constructors for rich responses like buttons and tiles (or "templates", as the Facebook platform calls them).

Clone this wiki locally