Skip to content

Commit

Permalink
Merge pull request #6055 from IzidoroBaltazar/conversation-id-viber
Browse files Browse the repository at this point in the history
[FEATURE] modify conversation id of server to accomodate viber id
  • Loading branch information
degiz authored Aug 25, 2020
2 parents 001f5d3 + a67ca4b commit 0c34ff7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/6055.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support for rasa conversation id with special characters on the server side - necessary for some channels (e.g. Viber)
16 changes: 8 additions & 8 deletions rasa/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ async def status(request: Request):
}
)

@app.get("/conversations/<conversation_id>/tracker")
@app.get("/conversations/<conversation_id:path>/tracker")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def retrieve_tracker(request: Request, conversation_id: Text):
Expand All @@ -483,7 +483,7 @@ async def retrieve_tracker(request: Request, conversation_id: Text):
500, "ConversationError", f"An unexpected error occurred. Error: {e}"
)

@app.post("/conversations/<conversation_id>/tracker/events")
@app.post("/conversations/<conversation_id:path>/tracker/events")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def append_events(request: Request, conversation_id: Text):
Expand Down Expand Up @@ -538,7 +538,7 @@ def _get_events_from_request_body(request: Request) -> List[Event]:

return events

@app.put("/conversations/<conversation_id>/tracker/events")
@app.put("/conversations/<conversation_id:path>/tracker/events")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def replace_events(request: Request, conversation_id: Text):
Expand Down Expand Up @@ -567,7 +567,7 @@ async def replace_events(request: Request, conversation_id: Text):
500, "ConversationError", f"An unexpected error occurred. Error: {e}"
)

@app.get("/conversations/<conversation_id>/story")
@app.get("/conversations/<conversation_id:path>/story")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def retrieve_story(request: Request, conversation_id: Text):
Expand All @@ -591,7 +591,7 @@ async def retrieve_story(request: Request, conversation_id: Text):
500, "ConversationError", f"An unexpected error occurred. Error: {e}"
)

@app.post("/conversations/<conversation_id>/execute")
@app.post("/conversations/<conversation_id:path>/execute")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def execute_action(request: Request, conversation_id: Text):
Expand Down Expand Up @@ -641,7 +641,7 @@ async def execute_action(request: Request, conversation_id: Text):

return response.json(response_body)

@app.post("/conversations/<conversation_id>/trigger_intent")
@app.post("/conversations/<conversation_id:path>/trigger_intent")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def trigger_intent(request: Request, conversation_id: Text) -> HTTPResponse:
Expand Down Expand Up @@ -695,7 +695,7 @@ async def trigger_intent(request: Request, conversation_id: Text) -> HTTPRespons

return response.json(response_body)

@app.post("/conversations/<conversation_id>/predict")
@app.post("/conversations/<conversation_id:path>/predict")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def predict(request: Request, conversation_id: Text) -> HTTPResponse:
Expand All @@ -712,7 +712,7 @@ async def predict(request: Request, conversation_id: Text) -> HTTPResponse:
500, "ConversationError", f"An unexpected error occurred. Error: {e}"
)

@app.post("/conversations/<conversation_id>/messages")
@app.post("/conversations/<conversation_id:path>/messages")
@requires_auth(app, auth_token)
@ensure_loaded_agent(app)
async def add_message(request: Request, conversation_id: Text):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,29 @@ def test_push_multiple_events(rasa_app: SanicTestClient):
assert tracker.get("events") == events


def test_post_conversation_id_with_slash(rasa_app: SanicTestClient):
conversation_id = str(uuid.uuid1())
id_len = len(conversation_id) // 2
conversation_id = conversation_id[:id_len] + "/+-_\\=" + conversation_id[id_len:]
conversation = f"/conversations/{conversation_id}"

events = [e.as_dict() for e in test_events]
_, response = rasa_app.post(
f"{conversation}/tracker/events",
json=events,
headers={"Content-Type": "application/json"},
)
assert response.json is not None
assert response.status == 200

_, tracker_response = rasa_app.get(f"/conversations/{conversation_id}/tracker")
tracker = tracker_response.json
assert tracker is not None

# there is also an `ACTION_LISTEN` event at the start
assert tracker.get("events") == events


def test_put_tracker(rasa_app: SanicTestClient):
data = [event.as_dict() for event in test_events]
_, response = rasa_app.put(
Expand Down

0 comments on commit 0c34ff7

Please sign in to comment.