@ambit-ai/ambit-client

AmbitAI Bot Connection Client

Usage no npm install needed!

<script type="module">
  import ambitAiAmbitClient from 'https://cdn.skypack.dev/@ambit-ai/ambit-client';
</script>

README

ambit-client

A library to connect with the bot using SocketIO

install

npm install --save @ambit-ai/ambit-client

use

const { AmbitClient, ConnectionStatus } = require("@ambit-ai/ambit-client");

const botConnection = new AmbitClient({
  baseUrl: 'https://yourbot.ambithub.com',
  secret: 'bot webchat secret',
  conversationId: 'optional convesation id to reconnect to'
  user: { // optional, needed if reconnecting to an existing conversation
    id: 'user uuid',
    name: 'user name'
  }
});

botConnection.connectionStatus$.subscribe(
  connectionStatus => {
    if (connectionStatus === ConnectionStatus.Online) {
      // Bot Connection is ready to start talking to the bot
    }
  }
);

botConnection.activity$.subscribe(activity => {
  // an incoming activity
  if (activity.type === 'event' && activity.name === 'synapseBotEvent') {
    // a generic bot event
  }
  if (activity.type === 'operatorHandOver' || activity.type === 'operatorHandBack') {
    // convesation handed over from and to the human operator
  }
  if (activity.type === 'typing') {
    // typing event sent by the bot
  }
  if (activity.type === 'message') {
    if (activity.from.id === botConnection.user.id) {
      // received activity sent by the client
    } else {
      // received activity sent by the bot
    }
  }
});

botConnection.postActivity(activity); // post an activity to the Bot

botConnection.user; // The user object, either generated by the bot, or provided when connection stablished
botConnection.conversationId; // The stablished connection Id to reconnect to

The client connection provides two RxJs streams, connectionStatus$ and activity$.

The first one can be used to listen to connectivity events. The bot will be ready to start talking with the connection status is ConnectionStatus.Online.

The second provides a stream of activities (messages) coming from the bot, it also provides activies that the user has sent to the bot.

After the connection gets stablished, both user and conversationId will be available so they can be stored to reconnect to the conversation at a later stage.