Skip to content

Commit

Permalink
Merge pull request #14 from uburuntu/master
Browse files Browse the repository at this point in the history
new: support Yandex's side states
  • Loading branch information
mahenzon authored Apr 10, 2022
2 parents f87b2e2 + c2c4e3f commit c091b8a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
50 changes: 37 additions & 13 deletions aioalice/types/alice_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from attr import attrs, attrib
from aiohttp.web import Request as WebRequest
from attr import attrs, attrib

from aioalice.types import (
AliceObject,
Expand All @@ -23,32 +23,43 @@ class AliceRequest(AliceObject):
session = attrib(converter=ensure_cls(Session))
version = attrib(type=str)

def _response(self, response):
def _response(self, response, session_state=None, user_state_update=None, application_state=None):
return AliceResponse(
response=response,
session=self.session.base,
session_state=session_state or {},
user_state_update=user_state_update or {},
application_state=application_state or {},
version=self.version,
)

def response(self, responose_or_text, **kwargs):
def response(self, response_or_text, session_state=None, user_state_update=None, application_state=None, **kwargs):
"""
Generate response
:param responose_or_text: Response or Response's text:
if responose_or_text is not an instance of Response,
:param response_or_text: Response or Response's text:
if response_or_text is not an instance of Response,
it is passed to the Response initialisator with kwargs.
Otherwise it is used as a Response
:param kwargs: tts, card, buttons, end_session for Response
NOTE: if you want to pass card, concider using one of
NOTE: if you want to pass card, consider using one of
these methods: response_big_image, response_items_list
:param session_state: Session's state
:param user_state_update: User's state
:param application_state: Application's state
Allows to store data on Yandex's side
Read more: https://yandex.ru/dev/dialogs/alice/doc/session-persistence.html
:return: AliceResponse
"""
if not isinstance(responose_or_text, Response):
responose_or_text = Response(responose_or_text, **kwargs)
return self._response(responose_or_text)
if not isinstance(response_or_text, Response):
response_or_text = Response(response_or_text, **kwargs)
return self._response(response_or_text, session_state, user_state_update, application_state)

def response_big_image(self, text, image_id, title, description, button=None, **kwargs):
def response_big_image(self, text, image_id, title, description, button=None,
session_state=None, user_state_update=None, application_state=None, **kwargs):
"""
Generate response with Big Image card
Expand All @@ -57,6 +68,11 @@ def response_big_image(self, text, image_id, title, description, button=None, **
:param title: Image's title for BigImage Card
:param description: Image's description for BigImage Card
:param button: Image's button for BigImage Card
:param session_state: Session's state
:param user_state_update: User's state
:param application_state: Application's state
Allows to store data on Yandex's side
Read more: https://yandex.ru/dev/dialogs/alice/doc/session-persistence.html
:param kwargs: tts, buttons, end_session for Response
:return: AliceResponse
"""
Expand All @@ -65,17 +81,24 @@ def response_big_image(self, text, image_id, title, description, button=None, **
text,
card=Card.big_image(image_id, title, description, button),
**kwargs,
)
),
session_state, user_state_update, application_state
)

def response_items_list(self, text, header, items, footer=None, **kwargs):
def response_items_list(self, text, header, items, footer=None,
session_state=None, user_state_update=None, application_state=None, **kwargs):
"""
Generate response with Items List card
:param text: Response's text
:param header: Card's header
:param items: Card's items - list of `Image` objects
:param footer: Card's footer
:param session_state: Session's state
:param user_state_update: User's state
:param application_state: Application's state
Allows to store data on Yandex's side
Read more: https://yandex.ru/dev/dialogs/alice/doc/session-persistence.html
:param kwargs: tts, buttons, end_session for Response
:return: AliceResponse
"""
Expand All @@ -84,5 +107,6 @@ def response_items_list(self, text, header, items, footer=None, **kwargs):
text,
card=Card.items_list(header, items, footer),
**kwargs,
)
),
session_state, user_state_update, application_state
)
3 changes: 3 additions & 0 deletions aioalice/types/alice_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ class AliceResponse(AliceObject):

response = attrib(converter=ensure_cls(Response))
session = attrib(converter=ensure_cls(BaseSession))
session_state = attrib(type=dict)
user_state_update = attrib(type=dict)
application_state = attrib(type=dict)
version = attrib(type=str)

0 comments on commit c091b8a

Please sign in to comment.