Git Product home page Git Product logo

blip-sdk-js's Introduction

blip-sdk-js

Simple BLiP SDK for JavaScript

This is a work in progress

npm version npm downloads Gitter Travis branch Commitizen friendly semantic-release


See more about BLiP here

Installing

Node.js

If you are using node.js (or webpack), it's necessary to install blip-sdk package (via npm) to access the BLiP server.

npm install --save blip-sdk lime-transport-websocket

Browser

If you are using a web application (on browser) with "pure" Javascript is possible to install the package via npm using the <script> tag. For this case beyond blip-sdk package it's necessary install others dependences as the lime-js and lime-transport-websocket packages:

<script src="./node_modules/lime-js/dist/lime.js" type="text/javascript"></script>
<script src="./node_modules/lime-transport-websocket/dist/WebSocketTransport.js" type="text/javascript"></script>
<script src="./node_modules/blip-sdk/dist/blip-sdk.js" type="text/javascript"></script>

You can also use unpkg to get the packages if you are not using npm on development:

<script src="https://unpkg.com/lime-js" type="text/javascript"></script>
<script src="https://unpkg.com/lime-transport-websocket" type="text/javascript"></script>
<script src="https://unpkg.com/blip-sdk" type="text/javascript"></script>

Instantiate the BlipSdk Client

You will need an identifier and a access key to connect a chatbot to BLiP. To get them:

  • Go to Painel BLiP and login;
  • Click on Chatbots and then click on Create chatbot;
  • Choose SDK model option;
  • Create your chatbot, then your identifier and access key will be displayed.

In order to instantiate the client use ClientBuilder class informing the identifier and access key:

import * as BlipSdk from 'blip-sdk';
import * as WebSocketTransport from 'lime-transport-websocket'

// Create a client instance passing the identifier and accessKey of your chatbot
let client = new BlipSdk.ClientBuilder()
    .withIdentifier(IDENTIFIER)
    .withAccessKey(ACCESS_KEY)
    .withTransportFactory(() => new WebSocketTransport())
    .build();

// Connect with server asynchronously
// Connection will occurr via websocket on 8081 port.
client.connect() // This method return a 'promise'.
    .then(function(session) {
        // Connection success. Now is possible send and receive envelopes from server. */
        })
    .catch(function(err) { /* Connection failed. */ });

Each client instance represent a server connection and can be reused. To close a connection use:

client.close()
    .then(function() { /* Disconnection success */ })
    .catch(function(err) { /* Disconnection failed */ });

Receiving

All messages sent to the chatbot are redirected to registered receivers of messages and notifications. You also can define filters to each receiver. The following example show how to add a simple message receiver:

client.addMessageReceiver(true, function(message) {
  // Process received message
});

The next sample show how to add notification receiver with filter to received event type:

client.addNotificationReceiver("received", function(notification) {
  // Process received notifications
});

It's also possible use a custom function as receiver filter:

Example of message receiver with filter of originator:

client.addMessageReceiver(function(message) { message.from === "[email protected]" }, function(message) {
  // Process received message
});

// Using expression lambda
client.addNotificationReceiver(() => true, function(notification) {
  // Process received notifications
});

Each registration of receivers return a handler that can be used to cancel the registration:

var removeJsonReceiver = client.addMessageReceiver("application/json", handleJson);
// ...
removeJsonReceiver();

Sending

It's possible send notifications and messages only after sessions has been stablished.

The following sample show how to send a message after connection has been stablished:

client.connect()
    .then(function(session) {
      // After connection is possible send messages
      var msg = { type: "text/plain", content: "Hello, world", to: "[email protected]" };
      client.sendMessage(msg);
    });

The following sample show how to send a notification after connection has been stablished:

client.connect()
    .then(function(session) {
      // Sending "received" notification
      var notification = { id: "ef16284d-09b2-4d91-8220-74008f3a5788", to: "[email protected]", event: Lime.NotificationEvent.RECEIVED };
      client.sendNotification(notification);
    });

Contributing

For information on how to contribute to this package, please refer to our Contribution guidelines.

blip-sdk-js's People

Contributors

samwx avatar ravpacheco avatar andrebires avatar andreminelli avatar brenoqueiroz avatar victorbalbo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.