Tiesitkö, että Bot Libre tarjoaa myös ilmaisia isännöityjä foorumeita omalle verkkosivustollesi tai mobiilisovellukseen?
Bot Libre Forum

How to use the Bot Libre python SDK

tekijä OmarBajunaid Lähetetty Oct 2 2023, 17:48

The Bot Libre python SDK allows you to easily integrate Bot Libre with a python application. Through the SDK you can connect to your bots on Bot Libre, send messages, and train and configure your bots. You can also access Bot Libre analytics and AI services, and other content.

Setting up Python for both Windows and Linux

Installing Windows Version

  1. Go to the official Python website: https://www.python.org/downloads/windows/

  2. Download the installer for your Windows version and Run it.

  3. Check the "Add Python.exe to PATH" option during installation.

  4. Click "Install Now" and wait for the installation to finish.

  5. Open Command Prompt and type python to verify the installation.

Installing pip on Windows:

Download the get-pip.py script to install pip. You can do this with the following command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Run the get-pip.py script to install pip:
python get-pip.py

After installation is complete, verify that pip is installed by running:
pip --version

Linux Version

  1. Open a terminal and update the package list (if needed):
    sudo apt update
  2. Installing python3:
    sudo apt install python3
  3. Check python version
    python3 --versoin

Installing pip on Linux:

Installing pip using the package manager:
sudo apt install python3-pip

After installation is complete, verify that pip is installed by running:
pip3 --version

Installing the requests and requests-toolbelt libraries

Before running this project, make sure you have Python, requests and requests-toolbelt libraries installed. They provide essential functionality for making HTTP requests and handling multipart data.

Installing requests library

You can install the requests library using pip, the Python package manager. Open your terminal or command prompt and run the following command:
pip install requests

Installing requests-toolbelt

You also need to install the requests-toolbelt library to simplify multipart encoding and file upload operations.
pip install requests-toolbelt

Clone python SDK:

Clone the Bot Libre python SDK by clicking on this link: https://github.com/BotLibre/BotLibre/tree/master/sdk/python.


To run the application, please ensure that you are in the same directory where you have cloned the SDK. Then, use the following command from the command line:
python ./main.py


Sending a chat message to a bot using Bot Libre Python SDK

Before launching the application with python ./main.py, you need to prepare the file. Edit the following lines from the beginning:

There is a class called ID for example

class ID:
    APPLICATION = "12345678..."
    USER = "john"
    PASSWORD = "pass"

In the application main.py, you need to assign some IDs that are required for this test. When you execute the application and select an option, it will perform an operation that requires these IDs you provide for verification and connection. Let's start by setting the 'Application ID', user and password, you can get an application ID from any of the services websites (Bot Libre, Bot Libre for Business). 

APPLICATION = "7773279532683487901"


Similarly for USER and PASSWORD
Please enter your account details. Both your username and password are required. If you don't have an account yet, you can create one to use.

USER = ""


For this test, we'll be using the Brain Bot as the bot. Its ID is: 165.

BOT = "165"


Note: There are several other empty ID variables used for various operations. For example, when posting on a forum, a forum ID is necessary to create a post, and a User Account is also required.

Now we can run python ./main.py and will output the following for the first run:

|->[Main] init SDKConnection
URL: https://www.botlibre.com/rest/api
HOST: www.botlibre.com
Application ID: 7773279532683487901
Debug: True

* BotLibre SDK *
0 - Current User Account
1 - Post Chat
2 - Fetch User (user)
3 - Fetch Forum Post

Enter an option (0 : 99) ('q' to quit or 'h' for help): 1

For this test, we will input the number (1), which will trigger the execution and verification of all entered information. Initially, it will confirm the user's login status, followed by the verification of the bot's ID once a connection is established. Afterward, it will prompt the user to input a message for sending.


<user user="****" application="7773279532683487901" password="****"></user>

|->POST_SUCCESSFUL: https://www.botlibre.com/rest/api/check-user
b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><user hint="***" name="****"  showName="true" gen...
BotID: 165 - Enter Message: Hello World!

Lets enter 'Hello World!' for testing, it will return a xml data, and the message will be parsed automatically.

|->Message Detials
Response: Hi.
Enter an option (0 : 99) ('q' to quit or 'h' for help):

Let's take a look at how this operates behind the scenes.

In the main.py file there is a function called switch which takes an integer option.

# Select options
def switch(option):
       ... # some code ...
        elif(int(option) == 1): # when selecting option number 1 we call the function sendChatMessage()
            response = main.sendChatMessage()
Lets take a look at sendChatMessageFunction()
 # Send a chat message to a bot
    def sendChatMessage(self, botId: str = ID.BOT) -> ChatResponse: # BOT ID is set by default
        isUser = self.connectUserAccount() # log in and verify user account.
        if(isUser == False):
            return None
        config = ChatConfig() # using ChatConfig
        config.instance = botId # Setting instance ID which is the BOT ID = i.e "165"
        config.message = "Hello World!" # Here we set the message we want to send the bot.
        return self.connection.chat(config) #Then we use the SDKConnection and send the data.
Also connectUserAccount() is using something similar
# Login in with user credentials
    def connectUserAccount(self) -> bool:
        if(self.connection.user != None):
            return True
        userConfig = UserConfig() #Using UserConfig to set application Id, user and password
        userConfig.application = self.applicationId
        userConfig.user = self.username
        userConfig.password = self.password
         .... # Validation code
        return self.connection.connect(userConfig) #Then we use the SDKConnection and send the data.
The required imports for this to work, it only needs the following:
from sdk.SDKConnection import SDKConnection
from sdk.BotlibreCredentials import BotlibreCredentials
from config.UserConfig import UserConfig # For log in user account
from config.ChatConfig import ChatConfig # To set up the message
from config.ChatResponse import ChatResponse # For the response of the message
Take a look at main.py and see how the main class is setup using SDKConnection and BotlibreCredentials.

Id: 49601508
Tunnisteet: sdk, python
Lähetetty: Oct 2 2023, 17:48
Päivitetty: Nov 7 2023, 9:17
Vastaukset: 0
Näkymät: 271, Tänään: 1, Viikolla: 2, Kuukausi: 19
1 0 5.0/5