Exemple #1
0
    async def set_webhook(self,
                          url: object,
                          webhook_events: object = None) -> object:
        """ For each set_webhook request Viber will send a callback to the
        webhook URL to confirm it is available. The expected HTTP response to
        the callback is 200 OK – any other response will mean the webhook is
         available. If the webhook is not available the set_webhook response
         sent to the user will be status 1: invalidUrl."""

        self._logger.debug(u"setting webhook to url: {0}".format(url))
        data = {'url': url}
        if webhook_events is not None:
            if isinstance(webhook_events, str):
                webhook_events = [webhook_events]

            for event in webhook_events:
                if event not in EventType.all():
                    webhook_events.remove(event)
                    self._logger.warning(
                        'Wrong event type {} in set_webhook'.format(event))

            if len(webhook_events) > 0:
                data['event_types'] = webhook_events

        result = await self._make_request(self.endpoints.SET_WEBHOOK, data)
        return result['event_types']
Exemple #2
0
    def __init__(self,
                 name: str,
                 avatar: str,
                 auth_token: str,
                 webhook: str,
                 webhook_events: list = None,
                 set_webhook_on_startup: bool = True,
                 unset_webhook_on_cleanup: bool = True,
                 host: str = '0.0.0.0',
                 port: int = 8000,
                 loop: aio.AbstractEventLoop = None,
                 check_signature: bool = True,
                 static_serve: bool = False) -> None:
        assert len(name) < 28, "Length of name should be shorty then 28 symbols"
        self.name = name
        self.avatar = avatar
        self.auth_token = auth_token

        # Server
        self.host = host
        self.port = port

        # Loop
        self.loop = aio.get_event_loop() if loop is None else loop

        # Viber API
        self._session = None
        self.api = Api(bot_configuration=BotConfiguration(
            name=self.name,
            avatar=self.avatar,
            auth_token=self.auth_token
        ), session=self.session, loop=self.loop)

        # Viber webhook
        self.webhook = webhook
        self.webhook_events = webhook_events
        self._set_webhook_on_startup = set_webhook_on_startup
        self._unset_webhook_on_cleanup = unset_webhook_on_cleanup

        def no_event_handle(event_type: str):
            return lambda msg: logger.debug("no event handle for %s", event_type)

        def no_message_handle(message_type: str):
            return lambda msg: logger.debug("no message handle for %s", message_type)

        # Callback — function for request processing messages excluded
        self._events_callbacks = {et: no_event_handle(et) for et in EventType.all()}
        # command — functions for processing text messages
        self._commands = []
        # handle — functions for processing messages exclude text
        self._handlers = {mt: no_message_handle(mt) for mt in MessageType.all()}

        self._default = lambda event: None

        # Application
        self.check_signature = check_signature
        self.app = self.get_app(static_serve=static_serve)
Exemple #3
0
    async def _process_request(self, request: ViberRequest) -> None:
        logger.debug('request: %s', str(request))

        coro = None

        # Process request with function from _events_callbacks
        if request.event_type in EventType.all():
            coro = self._events_callbacks[request.event_type](request)

        # Process messages
        if request.event_type == EventType.MESSAGE:
            coro = self._process_message(request)

        if coro:
            # TODO: add error processing
            t = self.loop.create_task(coro)
Exemple #4
0
    def event_handler(self, event_type):
        """
        Set callback for specific event type:
            - delivered
            - seen
            - conversation_started
            - message
            - subscribed
            - unsubscribed
            - failed
            - webhook
        """
        assert event_type in EventType.not_message_events(), 'Wrong event type'

        def wrap(coro):
            assert aio.iscoroutinefunction(coro), 'Decorated function should be coroutine'

            self._events_callbacks[event_type] = coro
            return coro

        return wrap