Skip to content

Commit

Permalink
enrich yandex interface with morning show, geolocation and image gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
avidale committed Feb 6, 2021
1 parent 044dec2 commit a5534a2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tgalice",
version="0.2.22",
version="0.2.23",
author="David Dale",
author_email="dale.david@mail.ru",
description="Yet another common wrapper for Alice skills and Facebook/Telegram bots",
Expand Down
2 changes: 1 addition & 1 deletion tgalice/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

from tgalice import dialog, dialog_manager, interfaces, storage, nlu, nlg, testing, utils, dialog_connector
from tgalice import dialog, dialog_manager, interfaces, storage, nlu, nlg, testing, utils, dialog_connector, criteria
from tgalice.server import flask_server
from tgalice.storage import session_storage, message_logging
from tgalice.dialog_manager.base import COMMANDS
Expand Down
11 changes: 11 additions & 0 deletions tgalice/criteria.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .dialog import Context
from .dialog.names import REQUEST_TYPES


def is_morning_show_context(ctx: Context) -> bool:
if not ctx.yandex or not ctx.yandex.request:
return False
r = ctx.yandex.request
if r.type != REQUEST_TYPES.SHOW_PULL:
return False
return r.show_type == 'MORNING'
4 changes: 4 additions & 0 deletions tgalice/dialog/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ class SOURCES:

class COMMANDS:
EXIT = 'exit'
REQUEST_GEOLOCATION = 'request_geolocation'


class REQUEST_TYPES:
SIMPLE_UTTERANCE = 'SimpleUtterance'
BUTTON_PRESSED = 'ButtonPressed'
SHOW_PULL = 'Show.Pull'
GEOLOCATION_ALLOWED = 'Geolocation.Allowed'
GEOLOCATION_REJECTED = 'Geolocation.Rejected'
5 changes: 5 additions & 0 deletions tgalice/dialog_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ def set_user_object(self, user_id, user_object):

def standardize_output(self, source, original_message, response: Response):
has_exit_command = False
directives = {}
if response.commands:
for command in response.commands:
if command == COMMANDS.EXIT:
has_exit_command = True
elif command == COMMANDS.REQUEST_GEOLOCATION:
directives[COMMANDS.REQUEST_GEOLOCATION] = {}
else:
raise NotImplementedError('Command "{}" is not implemented'.format(command))
if source == SOURCES.TELEGRAM:
Expand Down Expand Up @@ -187,6 +190,8 @@ def standardize_output(self, source, original_message, response: Response):
result['response']['card'] = response.image.to_dict()
if response.show_item_meta is not None:
result['response']['show_item_meta'] = response.show_item_meta
if directives:
result['response']['directives'] = directives
return result
elif source == SOURCES.FACEBOOK:
if response.raw_response is not None:
Expand Down
8 changes: 8 additions & 0 deletions tgalice/interfaces/yandex/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class Application(FreeSerializeable):
application_id: str = attr.ib(default=None)


@attr.s
class Location(FreeSerializeable):
lat: Optional[float] = attr.ib(default=None)
lon: Optional[float] = attr.ib(default=None)
accuracy: Optional[float] = attr.ib(default=None)


@attr.s
class Session(FreeSerializeable):
message_id: int = attr.ib()
Expand All @@ -106,6 +113,7 @@ class Session(FreeSerializeable):
user: Optional[User] = attr.ib(default=None)
application: Optional[Application] = attr.ib(default=None)
new: bool = attr.ib(default=False)
location: Optional[Location] = attr.ib(default=None, converter=Location.from_dict)


@attr.s
Expand Down
18 changes: 17 additions & 1 deletion tgalice/interfaces/yandex/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class CARD_TYPES:
BIG_IMAGE = 'BigImage'
ITEMS_LIST = 'ItemsList'
IMAGE_GALLERY = 'ImageGallery'


@attr.s
Expand Down Expand Up @@ -72,6 +73,18 @@ class ItemsList(Card):
footer: Optional[ItemsListFooter] = attr.ib(converter=ItemsListFooter.from_dict, default=None)


@attr.s
class ImageGalleryItem(Serializeable):
image_id: str = attr.ib(default=None)
title: str = attr.ib(default=None)


@attr.s
class ImageGallery(Card):
type: str = attr.ib(default=CARD_TYPES.IMAGE_GALLERY, init=False)
items: List[ImageGalleryItem] = attr.ib(converter=list_converter(ImageGalleryItem), factory=list)


@attr.s
class ShowItemMeta(Serializeable):
content_id: Optional[str] = attr.ib(default=None)
Expand All @@ -94,16 +107,19 @@ def card_converter(data):
return BigImage.from_dict(new_data)
if card_type == CARD_TYPES.ITEMS_LIST:
return ItemsList.from_dict(new_data)
if card_type == CARD_TYPES.IMAGE_GALLERY:
return ImageGallery.from_dict(new_data)


@attr.s
class Response(Serializeable):
text: str = attr.ib()
tts: str = attr.ib(default=None)
buttons: List[Button] = attr.ib(converter=list_converter(Button), factory=list)
card: Optional[Union[BigImage, ItemsList]] = attr.ib(converter=card_converter, default=None)
card: Optional[Union[BigImage, ItemsList, ImageGallery]] = attr.ib(converter=card_converter, default=None)
end_session: bool = attr.ib(default=False)
show_item_meta: Optional[ShowItemMeta] = attr.ib(default=None, converter=ShowItemMeta.from_dict)
directives: Optional[Dict] = attr.ib(default=None)


@attr.s
Expand Down

0 comments on commit a5534a2

Please sign in to comment.