This application is a demonstration of how to use our APIs through an app developed through Python's Flask Framework to interact with your deployed bot.
First, clone this repository.
$ git clone https://github.com/Botlhale-AI/flaskTemplate.git
$ cd flaskTemplate
After, install all necessary to run:
$ pip install -r requirements.txt
Then, add all the required parameters in the config.json & run the application:
$ python app.py
To see your application, access this url in your browser:
http://localhost:5000
You can configure BotID
, LanguageCode
, MessageType
, ResponseType
, refreshToken
in config.json
. After providing the required parameters in the config.json
you can run python app.py
and your bot should respond.
{
"BotID": "<BotID>",
"LanguageCode": "English",
"MessageType": "text",
"ResponseType": "text",
"refreshToken": "<Refresh Token>"
}
BotID
and the refreshToken
are provided to you in the Botlhale NLP Toolkit.
The flask app has 2 main routes, namely startConversation
and sendMessage
. These routes follow Botlhale AI's API Documentation to interact with a chatbot deployment from Botlhale's NLP Toolkit.
This routes hits the https://dev-botlhale.io/generateAuthToken
with the refreshToken
to get the IdToken
for Bearer Auth on other endpoints. After getting the IdToken
, the https://dev-botlhale.io/startConversation
endpoint is hit with the IdToken
in the header and BotID
& LanguageCode
in the request body to generate the ConversationID
.
@app.route('/startConversation')
def startConversation():
# Generate IdToken for Bearer Auth on other endpoints.
payload={
'REFRESH_TOKEN': refreshToken,
}
IdToken = json.loads(requests.request("POST", generateAuthTokenUrl, data=payload).content)['AuthenticationResult']['IdToken']
# Generate ConversationID.
payload={
'BotID': BotID,
'LanguageCode': LanguageCode
}
headers = {"Authorization": "Bearer {}".format(IdToken)}
ConversationID = json.loads(requests.request("POST", connectUrl, headers=headers, data=payload).content)['ConversationID']
# Store ConversationID & IdToken in the session for use in the sendMessage route.
session['ConversationID'] = ConversationID
session['IdToken'] = IdToken
return make_response(jsonify({"ConversationID":ConversationID, "IdToken":IdToken}), 200)
This route hits the https://dev-botlhale.io/message
endpoint with the user's message and the required parameters in the body and IdToken
in the header.
@app.route('/sendMessage', methods = ['GET'])
def sendMessage():
# Get session variables
ConversationID = session['ConversationID']
IdToken = session['IdToken']
# Get request parameters
TextMsg = request.args.get('text')
# Send message to bot
payload={
'BotID': BotID,
'LanguageCode': LanguageCode,
'ConversationID': ConversationID,
'MessageType': MessageType,
'ResponseType': ResponseType,
'TextMsg': TextMsg
}
headers = {"Authorization": "Bearer {}".format(IdToken)}
response = requests.request("POST", sendMessageUrl, headers=headers, data=payload).text
return response