Skip to content

Commit

Permalink
Update django view to use conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jul 11, 2017
1 parent bf6c6c2 commit 8eede4f
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions chatterbot/ext/django_chatterbot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,35 @@ def get_chat_session(self, request):
Return the current session for the chat if one exists.
Create a new session if one does not exist.
"""
class Conversation(object):
def __init__(self):
self.id = None
self.statements = []

conversation = Conversation()

chat_session_id = request.session.get('chat_session_id', None)
chat_session = self.chatterbot.conversation_sessions.get(chat_session_id, None)
statements = self.chatterbot.storage.filter(
conversation__id=chat_session_id
)

if not chat_session:
chat_session = self.chatterbot.conversation_sessions.new()
request.session['chat_session_id'] = chat_session.id_string
if not statements:
chat_session_id = self.chatterbot.storage.create_conversation()
request.session['chat_session_id'] = chat_session_id

return chat_session
conversation.id = chat_session_id
conversation.statements = [
statement.serialize() for statement in statements
]

return conversation


class ChatterBotView(ChatterBotViewMixin, View):
"""
Provide an API endpoint to interact with ChatterBot.
"""

def _serialize_conversation(self, session):
if session.conversation.empty():
return []

conversation = []

for statement, response in session.conversation:
conversation.append([statement.serialize(), response.serialize()])

return conversation

def post(self, request, *args, **kwargs):
"""
Return a response to the statement in the posted data.
Expand All @@ -62,9 +65,9 @@ def post(self, request, *args, **kwargs):

self.validate(input_data)

chat_session = self.get_chat_session(request)
conversation = self.get_chat_session(request)

response = self.chatterbot.get_response(input_data, chat_session.id_string)
response = self.chatterbot.get_response(input_data, conversation.id)
response_data = response.serialize()

return JsonResponse(response_data, status=200)
Expand All @@ -73,12 +76,12 @@ def get(self, request, *args, **kwargs):
"""
Return data corresponding to the current conversation.
"""
chat_session = self.get_chat_session(request)
conversation = self.get_chat_session(request)

data = {
'detail': 'You should make a POST request to this endpoint.',
'name': self.chatterbot.name,
'conversation': self._serialize_conversation(chat_session)
'conversation': conversation.statements
}

# Return a method not allowed response
Expand Down

0 comments on commit 8eede4f

Please sign in to comment.