How to use the Bot Libre python SDK |
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 LinuxInstalling Windows Version
Installing pip on Windows:Download the Run the get-pip.py script to install pip: After installation is complete, verify that pip is installed by running: Linux Version
Installing pip on Linux:Installing pip using the package manager: After installation is complete, verify that pip is installed by running: Installing the requests and requests-toolbelt librariesBefore running this project, make sure you have Python, Installing requests libraryYou can install the Installing requests-toolbelt
You also need to install the requests-toolbelt library to simplify multipart encoding and file upload operations. Clone python SDK:Clone the Bot Libre python SDK by clicking on this link: https://github.com/BotLibre/BotLibre/tree/master/sdk/python. UsageTo 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:
Sending a chat message to a bot using Bot Libre Python SDKBefore launching the application with There is a class called ID for example class ID: ... APPLICATION = "12345678..." ... USER = "john" PASSWORD = "pass" ...
APPLICATION = "7773279532683487901"
Similarly for USER and PASSWORD USER = "" PASSWORD = ""
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
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.
Lets enter 'Hello World!' for testing, it will return a xml data, and the message will be parsed automatically. 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): try: ... # some code ... elif(int(option) == 1): # when selecting option number 1 we call the function sendChatMessage() response = main.sendChatMessage() print(response.message) 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.
|
|
|
|