diff --git a/DESIGN.md b/DESIGN.md index 87f3882..bcf2afe 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -57,9 +57,7 @@ Response: ### New App Upload `POST /newapp` - - - +Stores meta data of new app to send it to watson for training Request: @@ -67,9 +65,20 @@ Request: Response: -- `success Message` (String) +- `success Message` (JsonString) + +### Get Session id +`GET /session` + +Unique session id for users. +Request: + +- None + +Response: +- `session_id` (Json) ## Meta Endpoints ### Health Check diff --git a/README.md b/README.md index c608e1d..cc533c1 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,10 @@ and [Run](#run) sections. ## Database Start a local MongoDB server by running: +``` +make db +``` ## Configuration For local development, create a `Wastson Assitant` instance in a IBM Cloud Catalog. The instance will be created in a `default` resource group. @@ -38,6 +41,10 @@ Launch the Watson Assistant using dashboard and import training data available i Configuration is passed via environment variables. - `BOTUSER_KEY` : API key assigned to the bot - `WORKSPACE_ID` :Unique id given to the created skill +- `APP_DB_HOST` : Database host +- `APP_DB_NAME` : Database name +- `APP_DB_USER` : User for this database +- `APP_DB_PASSWORD` : Database user password You can create and inject your own training data / skill by using functions in https://github.com/knative-scout/chat-bot-api/tree/master/training/features diff --git a/app.py b/app.py index c2007e3..b1e6aaa 100644 --- a/app.py +++ b/app.py @@ -5,30 +5,30 @@ import nltk import json from pymongo import MongoClient +import uuid # Setting up NLTK nltk.data.path.append("/srv/bot_api/nltk_data/") - # Connecting to MondoDB client = MongoClient(config.db_config["DB_HOST"], config.db_config["DB_PORT"], username=config.db_config["DB_USER"], - password=config.db_config["DB_PASSWORD"], connect=False) # Connection to MongoDB + password=config.db_config["DB_PASSWORD"], connect=False) # Connection to MongoDB database = config.db_config["DB_NAME"] currentConversation = config.db_config["CURRENT"] db = client[database][currentConversation] -config.logger.info("Connection to Database: "+str(db)) +config.logger.info("Connection to Database: " + str(db)) if db.insert_one({'user_id': "xxx_xxx_xxx_xxx_test"}).inserted_id: try: db.delete_many({'user_id': "xxx_xxx_xxx_xxx_test"}) except Exception as e: - config.logger.info("Error Connecting to Database: "+str(e)) + config.logger.info("Error Connecting to Database: " + str(e)) config.logger.info("Connection to Database Successful") else: config.logger.info("Error Connecting to Database") - app = Flask(__name__) + # Function to receive messages from client application @app.route('/messages', methods=['GET', 'POST']) def receive_messages(): @@ -37,7 +37,7 @@ def receive_messages(): message_text = request.get_json()['text'] user = request.get_json()['user'] config.logger.info("POST request on /messages") - api_response = processmessage.process_message(message_text, user) + api_response = processmessage.process_message(message_text, user) return Response(json.dumps(api_response), status=200, mimetype='application/json') except IndexError: status = {} @@ -54,6 +54,27 @@ def receive_messages(): return Response(json.dumps(status), status=400, mimetype='application/json') +@app.route('/session', methods=['GET']) +def create_sessions(): + if request.method == 'GET': + try: + unique_id = {} + + # uuid takes the time stamp and host id to create unique identifier + + unique_id['session_id'] = str(uuid.uuid1()) + print(unique_id) + return Response(json.dumps(unique_id), status=200, mimetype='application/json') + except: + status = {} + status["error"] = "Could not generate unique id: " + str(e) + return Response(json.dumps(status), status=400, mimetype='application/json') + else: + status = {} + status["error"] = "Wrong Request Type" + return Response(json.dumps(status), status=400, mimetype='application/json') + + @app.route('/health', methods=['GET', 'POST']) @disable_logging def health_probe() -> Response: @@ -65,4 +86,3 @@ def health_probe() -> Response: if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=8080) nltk.data.path.append('/srv/bot_api/nltk_data/') -