1.11.0 • Published 5 years ago

botmeter-logger v1.11.0

Weekly downloads
17
License
Apache-2.0
Repository
github
Last release
5 years ago

botmeter-logger

This library allows to log your bot conversations in the Botmeter analytics platform.

It is currently designed to be used with:

  • Messenger bots (doc here),
  • Microsoft Botbuilder bots (doc here) or,
  • Botfuel bots (included in the Botfuel bot SDK).

How to setup

Install:

npm install --save botmeter-logger

How to use

There are 3 ways you can use botmeter-logger:

  • generic
  • messenger
  • botbuilder

Using the API

const BotmeterLogger = require('botmeter-logger');

const genericLogger = new BotmeterLogger({
  appId: 'id',
  appKey: 'key',
}).generic;

const document = {
  bot_version: '1.0.2',
  timestamp: 1491475013000,
  channel: 'Webchat',
  user_id: 'JohnDoe',
  user: {
    first_name: 'John',
    last_name: 'Doe',
  },
  body: 'Hello',
  body_type: 'text',
  responses: ['Hi !', 'How are you ?'],
  state_in: 'Root',
  state_out: 'Root',
  intent: {
    name: 'Greetings',
    score: 0.8,
  }
};

genericLogger.indexDocument(document, function(error, body) {
  // Body will equal 'ok' if logging is successful
  ...
});

With Messenger

Log the message inside the POST request callback made when the bot replies (the user message and bot response(s) must be logged in the same document).

const BotmeterLogger = require('botmeter-logger');
const request = require('request');

const messengerLogger = new BotmeterLogger({
  appId: '<APP_ID>',
  appKey: '<APP_KEY>',
}).messenger;

// User message
const requestBody = 'Hello';

// Bot response
const responseJson = {
    recipient: {
      id: senderId,
    },
    message: {
      text: 'Hello World',
    },
  };


// POST bot reponse to facebook
request({
  uri: 'https://graph.facebook.com/v2.6/me/messages',
  qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
  method: 'POST',
  json: responseJson
}, () => {
    // The user message and bot response(s) must be logged in the same document
    messengerLogger.logDocument(requestBody, responseJson, (e, r) => {
        if (e) {
          console.log('Botmeter error: ', e);
        } else {
          console.log('Botmeter logging: ', r);
        }
    })
});

With Bot builder

We provide a middleware that you can easily plug to Microsoft bot builder (see right pane).

const BotmeterLogger = require('botmeter-logger');
const builder = require('botbuilder');
const restify = require('restify');

// Create a server
const server = restify.createServer();
server.listen(process.env.PORT || 3978, function () {
  console.log('%s listening to %s', server.name, server.url);
});

// Instantiate the Bot builder connector and bot
const connector = new builder.ChatConnector({
  // can be left undefined when testing with emulator
  appId: process.env.MICROSOFT_APP_ID,
  // can be left undefined when testing with emulator
  appPassword: process.env.MICROSOFT_APP_PASSWORD,
});

const bot = new builder.UniversalBot(connector);

// Instantiate Bot builder logger
const builderLogger = new BotmeterLogger({
  appId: '<APP_ID>',
  appKey: '<APP_KEY>',
}).botbuilder;

// Plug it
server.post('/api/messages', connector.listen());

bot.use(builderLogger);

bot.dialog('/', (session) => session.send('Hello World'));

License

See the LICENSE file.

1.11.0

5 years ago

1.10.0

6 years ago

1.9.4

7 years ago

1.9.3

7 years ago

1.9.2

7 years ago

1.9.1

7 years ago

1.9.0

7 years ago

1.0.0

7 years ago