Documentation

Do you use documentation to use NewBot Framework

Using NewBot Cloud SDK

NewBot uses WebSockets. You can load the Newbot SDK:

<div class="newbot-box"></div>
<script src="https://app.newbot.io/sdk/newbot-sdk.min.js"></script>

The <div> must be present in the page

In your Javascript script, initialize a variable:

const bot = new NewBot(KEY)

KEY is the key of the bot (or identifier) ​​that you can recover on the application. They are public

Load the chatbox to integrate it on a site

Use the loadBox() method. Returns an Promise when loading is complete

const bot = new NewBot(KEY)
bot.loadBox()

The loadBox() method can have an object in parameter: it's the options:

{
  facebook : <boolean>
} 
  • facebook: Displays the Facebook plugin Customer Chat (https://developers.facebook.com/docs/messenger-platform/discovery/customer-chat-plugin). NewBot's SDK encapsulates Facebook's SDK. This makes it possible to link chatbot data directly into the Facebook SDK.

Open / Close the chatbox

Use the open property on the chatbox to open or close it

const bot = new NewBot(KEY)
bot.loadBox().then(chatbox => {
  chatbox.open = true // Ouvrir
  chatbox.open = false // Fermer
})

The other way to access the chatbox instance is: bot.chatbox. Example:

const bot = new NewBot(KEY)
bot.loadBox().then(() => {
  bot.chatbox.open = true
})

This can be useful if you do not have the chatbox property available. Warning, this only works if you have already loaded the chatbox with the loadBox() method

Listen to an event in the chatbox

You may want a button in the chatbox to trigger an action. For example, by clicking a button, a video in the page is launched.

const bot = new NewBot(KEY)
bot.loadBox()
bot.onEvent('<nom du trigger>', (data) => {

})

Below, you will notice that a button can send the following object:

event: {
  name: <string>
  data: <Object>
} 

This triggers a function defined in the onEvent() method.

Example:

The JSON object sent by the chatbot is:

{
  "text": "Voici une vidéo",
  "buttons": [
    {
      "title": "Lancer la vidéo",
      "event": {
        "name": "play.video",
        "data": {"id": 1337}
      } 
    }
  ]
}

When the user clicks the Lancer la vidéo button, the play.video event will be fired:

const bot = new NewBot(KEY)
bot.loadBox()
bot.onEvent('play.video', (data) => {
    console.log(data.id) // affiche "1337"
})

Use the SDK to create its own conversational interface

Load the chatbot data with the load() method. This returns an Promise

const bot = new NewBot(KEY)
bot.load().then(() => {
  // ... next
})

Know the data of the chatbot

After loading the chatbot, you can recover the public data of the chatbot:

const bot = new NewBot(KEY)
bot.load().then(() => {
   console.log(bot.data)
})

Also works with the loadBox() method

Know the history of the user's messages

After loading the chatbot, you can retrieve the user's message history:

const bot = new NewBot(KEY)
bot.load().then(() => {
   console.log(bot.userHistory)
})

Also works with the loadBox() method

This returns an array of messages. A message is an object with the following structure:

{
  body: <structure du message>,
  date: <date du message>,
  user: 'bot' ou 'myself'
  _id: <identifiant du message>
}

The structure of the message is defined below under the heading Structure de l'objet reçu

To send

send(<object>) method Object with the following properties:

  • lang (optional): The language of the user in ISO-639 + _ + ISO-3166 format (Language Code + "_" + Country Code). Example: fr_FR, en_EN, en_US, etc.
  • text, geo or event: see below

A text

  • text: The text
bot.send({text: 'Mon Message'})

Geographical position

  • geo: Geolocation object: * latitude: Latitude * longitude: Longitude

The chatbot scenario must accept geolocation support.

bot.send({
   geo: {
     latitude: 1.3,
     longitude: 4.89
   }
})

Trigger an event

  • event: Object * name: Name of the event (inquire to know the events of the scenario of your chatbot) * data: Data to send
bot.send({
   event: {
     name: 'myEventName'
     data: {

     }
   }
})

Receive a message

onFunction(obj)) Method Parameter:

  • Function: Recall function with the received object.
bot.on((obj) => {
  console.log(obj) // Objet reçu
});

Below, the structure of the received object

Structure of the received object.

Simple text:

    {
      text: <string>
    }

Display a picture:

    {
      text: <string>
      image: <string>
    }
  • image: Image URL

Articles:

    {
      text: <string>
      cards: [
        {
          title: <string>
          subtitle: <string>
          image: <string>
          buttons: [<buttons>]
        }
      ]
    }

Buttons:

  {
    text: <string>
    buttons: [
      {
        type: <string>
        url: <string>
        msg: <string>
        event: {
          name: <string>
          data: <Object>
        } 
      }
    ]
  }
© 2019 NewBot