示例#1
0
    def get_layers(self) -> List[BaseLayer]:
        """
        Return all layers that can be found in the message.
        """
        out = []
        msg = self._event.get('message', {})

        if 'text' in msg:
            out.append(lyr.RawText(msg['text']))

        for attachment in msg.get('attachments') or []:
            if attachment['type'] == 'image':
                out.append(lyr.Image(UrlMedia(attachment['payload']['url'])))
            elif attachment['type'] == 'audio':
                out.append(lyr.Audio(UrlMedia(attachment['payload']['url'])))
            elif attachment['type'] == 'file':
                out.append(lyr.File(UrlMedia(attachment['payload']['url'])))
            elif attachment['type'] == 'video':
                out.append(lyr.Video(UrlMedia(attachment['payload']['url'])))
            elif attachment['type'] == 'location':
                # noinspection PyArgumentList
                out.append(lyr.Location(lyr.Location.Point(
                    lat=attachment['payload']['coordinates']['lat'],
                    lon=attachment['payload']['coordinates']['long'],
                )))

        if 'quick_reply' in msg:
            out.append(lyr.QuickReply(msg['quick_reply']['payload']))

        if 'postback' in self._event:
            payload = ujson.loads(self._event['postback']['payload'])
            out.append(lyr.Postback(payload))

        if 'link_click' in self._event:
            out.append(lyr.LinkClick(
                self._event['link_click']['url'],
                self._event['link_click']['slug'],
            ))

        if 'close_webview' in self._event:
            out.append(lyr.CloseWebview(
                self._event['close_webview']['slug'],
            ))

        if 'optin' in self._event:
            out.append(lyr.OptIn(self._event['optin']['ref']))

        return out
示例#2
0
    def get_layers(self) -> List[BaseLayer]:
        out = []

        if 'message' in self._update:
            msg = self._update.get('message', {})

            if 'text' in msg:
                text = msg['text']
                out.append(lyr.RawText(text))

                for entity in (msg.get('entities') or []):
                    o = entity['offset']
                    l = entity['length']
                    entity_text = text[o:o + l]

                    if entity['type'] == 'bot_command':
                        out.append(BotCommand(entity_text))

            if 'reply_to_message' in msg:
                sub_msg = TelegramMessage(
                    {'message': msg['reply_to_message']},
                    self._telegram,
                )
                out.append(lyr.Message(sub_msg))

            if 'photo' in msg:
                media = Photo(msg['photo'])
                out.append(lyr.Image(media))

        if 'callback_query' in self._update:
            payload = self._update['callback_query']['data']
            out.append(lyr.Postback(ujson.loads(payload)))
            out.append(InlineMessage())

            sub_msg = TelegramMessage(
                self._update['callback_query'],
                self._telegram,
            )
            out.append(lyr.Message(sub_msg))

        if 'inline_query' in self._update:
            out.append(InlineQuery(self._update['inline_query']))

        return out
    triggers as trg,
)
from bernard.platforms.telegram.layers import (BotCommand)

from .states import *
from .triggers import *

transitions = [
    Tr(
        dest=S001xWelcome,
        factory=trg.Equal.builder(BotCommand('/start')),
    ),
    Tr(
        dest=S002xDidTheRocketLaunchYetInitial,
        origin=S001xWelcome,
        factory=trg.Equal.builder(lyr.Postback('yes')),
    ),
    Tr(
        dest=S005xGoodbye,
        origin=S001xWelcome,
        factory=trg.Equal.builder(lyr.Postback('no')),
    ),
    Tr(
        dest=S003xDidTheRocketLaunchYetAgain,
        origin=S002xDidTheRocketLaunchYetInitial,
        factory=Bisection.builder(is_found=False),
    ),
    Tr(
        dest=S003xDidTheRocketLaunchYetAgain,
        origin=S003xDidTheRocketLaunchYetAgain,
        factory=Bisection.builder(is_found=False),