Skip to content

Sarufi NodeJs SDK to help you interact with Sarufi Conversational API

License

Notifications You must be signed in to change notification settings

flexcodelabs/sarufi

Repository files navigation

NODEJS SARUFI SDK

TESTS RELEASES DEVELOP PR RELEASE PR

Sarufi NodeJS SDK to help you interact with SARUFI platform inspired by Python Sarufi SDK

Table of Contents

  1. Installation
    1.1 Installation
  2. Use
    2.1. Get your api key
    2.1. Create an empty chatbot
    2.3. Updating your bot
    2.4. Get bot by id
    2.5. Get bots
    2.6. Delete bot
    2.7. Start conversation
    2.8. All responses details

Installation and Use

Installation

From terminal in the root directory of your project, run

npm i sarufi

Use

import sarufi from sarufi

Get your api key

Head to Sarufi, copy your api_key and login:

sarufi.login({ api_key: YOUR_API_KEY })

Create an Empty chatbot

We supply chatbot details

Request payload

{
  "name": "YOUR AWESOME BOT NAME",
  "description": "PUT DESCRIPTION HERE",
  "industry": "YOUR BOT INDUSTRY",
  "intents": {},
  "flows": {},
}

Then we call

// call this first if you haven't logged in.
sarufi.login({ api_key: YOUR_API_KEY }) 

sarufi.createBot({bot: REQUEST PAYLOAD})

NB: For detailed description of intents and flows to be used in conversation, refer to Python Sarufi SDK Docs

Response for successful bot creation

{
  "success": true,
  "bot": {  "name": "YOUR AWESOME BOT NAME",
            "description": "PUT DESCRIPTION HERE",
            "user_id": "YOUR ID",
            "industry": "YOUR BOT INDUSTRY",
            "intents": {}, //intents if they were supplied
            "flows": {}, //flows if they were supplied
            "updated_at": "DATE THE BOT WAS LAST UPDATED",
            "id": "BOT ID",
            "model_name": "BOT MODEL NAME",
            "created_at": "DATE THE BOT WAS CREATED"
  },
  "chat": "({message: string, chat_id?: uknown}) => RETURNS CHAT RESPONSE" //A callable method that starts a chat with your bot, it takes in a string message and optional chat ID
}

Updating your bot

Updating a bot, takes in the same arguments as creating a bot with addition of bot id

Request

// api_key is optional
sarufi.updateBot({bot: REQUEST PAYLOAD, id: YOUR BOT ID, api_key: YOUR_API_KEY})

Response on success, is the same as the response for creating a bot

Get bot by id

We call the following method on sarufi and pass the bot id

Request

// api_key is optional
sarufi.getBot({id: BOT ID, api_key: YOUR_API_KEY})

Response on success, is the same as the response for creating and updating a bot

Get bots

We call the following method on sarufi and pass the bot id

Request

//For version 0.0.1-Beta
sarufi.getBots()

//For versions 0.0.2-Beta and above,
sarufi.getBots({api_key: YOUR_API_KEY}) //This accepts optional paramemters url and api key for persisted authorization tokens.

Response on success

{
    "success": true,
    "bots": [] // Array of all bots you created
}

Delete bot

We call the following method on sarufi and pass the bot id

// api_key is optional
sarufi.deleteBot({id: BOT ID, api_key: YOUR_API_KEY})

Response on success

{
    "success": true,
    "message": "A MESSAGE STATING THAT YOUR BOT HAS BEEN DELETED"
}

Start conversation

There are two methods for this, i.e

  1. bot.chat() this requires us to get a bot we want to have a conversation with and calling a chat method
  2. sarufi.chat() this requires a required message, required bot_id and an optional chat_id as request arguments
// api_key is optional

// bot.chat()
const bot = await sarufi.getBot({id: 'OUR BOT ID', api_key: YOUR_API_KEY})
await bot.chat({message: 'Yooh', channel: 'whatsapp'}) //channel is optional (Default is general)

//sarufi.chat()
await sarufi.chat({message: 'Hey', bot_id: bot.id, chat_id: 'HEYOO', api_key: YOUR_API_KEY, channel: 'whatsapp'})

Response on success

// General Channel
{
  "message":  string| number | unknown | [string] | Record<string, unknown> | [Record<string, unknown>],
  "success": true,
  "memory": { [key: string]: string | unknown},
  [key: string]: string | unknown
}

// Whatsapp Channel
{
  "actions": Array<{
    "send_message"?: string[];
    "send_button"?: any;
    "send_reply_button"?: { 
      "type": string; 
      "body": { "text": string }; 
      "action": any 
    };
    "send_image"?: Array<{ 
      "link": string; 
      "caption": string 
    }>;
    "send_audio"?: any;
    "send_videos"?: any;
  }>;,
 "success": true,
  "memory": { [key: string]: string | unknown},
}

All Responses have

  1. Success property that shows whether or not the request was successful
  2. For failed requests, the response's success property will be false and additional properties for tracing will be added to the response object

Example of failed request

{
  "success": false,
  "message": "MESSAGE", //an error message explaining the error
  "code": "string",
  "status": "NUMBER",
}

Although an error response can have more than those properties, when the status is 500, that will denote an error occured within the sarufi otherwise it will be from an error originating from the remote sarufi server.

All requests have an optional url property that can be passed in case the url changes in the future

NB: For detailed documentation, please visit the Python Sarufi SDK Docs

Developed and Maintained with ❤️ at Flexcode Labs

About

Sarufi NodeJs SDK to help you interact with Sarufi Conversational API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published