Beispiel #1
0
    async def _handle_telegram_response(self, response, ignore=None):
        """
        Parse a response from Telegram. If there's an error, an exception will
        be raised with an explicative message.

        :param response: Response to parse
        :return: Data
        """

        if ignore is None:
            ignore = set()

        ok = response.status == 200

        try:
            data = await response.json()

            if not ok:
                desc = data['description']

                if desc in ignore:
                    return

                raise PlatformOperationError(
                    'Telegram replied with an error: {}'.format(desc))
        except (ValueError, TypeError, KeyError):
            raise PlatformOperationError('An unknown Telegram error occurred')

        return data
Beispiel #2
0
    async def _handle_fb_response(self, response: aiohttp.ClientResponse):
        """
        Check that Facebook was OK with the API call we just made and raise
        an exception if it failed.
        """

        ok = response.status == 200

        if not ok:
            # noinspection PyBroadException
            try:
                error = (await response.json())['error']['message']
            except Exception:
                error = '(nothing)'

            raise PlatformOperationError('Facebook says: "{}"'.format(error))
Beispiel #3
0
    def _access_token(self, request: Request=None, page_id: Text=''):
        """
        Guess the access token for that specific request.
        """

        if not page_id:
            msg = request.message  # type: FacebookMessage
            page_id = msg.get_page_id()

        for page in settings.FACEBOOK:
            if page['page_id'] == page_id:
                return page['page_token']

        raise PlatformOperationError('Trying to get access token of the '
                                     'page "{}", which is not configured.'
                                     .format(page_id))