Skip to content

Commit

Permalink
Merge pull request #46 from kscout/session_endpoint
Browse files Browse the repository at this point in the history
created session endpoint
  • Loading branch information
janiew authored Jul 1, 2019
2 parents 0e982b1 + f106a98 commit efc37e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
17 changes: 13 additions & 4 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,28 @@ Response:
### New App Upload
`POST /newapp`




Stores meta data of new app to send it to watson for training

Request:

- `apps` ([App Model]) New app submitted to the hub

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
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
34 changes: 27 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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 = {}
Expand All @@ -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:
Expand All @@ -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/')

0 comments on commit efc37e8

Please sign in to comment.