Пример #1
0
    def _init_puppet(self):
        """
        start puppet channel contact_self_qr_code
        """
        log.info('init puppet')
        if not self.options.token and not self.options.end_point:
            raise WechatyPuppetConfigurationError(
                'Please set a valid envrioment<WECHATY_PUPPET_SERVICE_TOKEN|ENDPOINT|endpoint> or '
                '<WECHATY_PUPPET_SERVICE_ENDPOINT|ENDPOINT|endpoint>'
            )
        # otherwise load them from server by the token
        if not self.options.end_point:
            # Query the end_point by the token.
            log.info('There is no endpoint in cache, trying to fetch endpoint with token.')
            # TODO: the endpoint should be
            response = requests.get(
                f'https://api.chatie.io/v0/hosties/{self.options.token}'
            )

            if response.status_code != 200:
                raise WechatyPuppetGrpcError('service server is invalid ... ')

            data = response.json()

            if 'ip' not in data or data['ip'] == '0.0.0.0':
                raise WechatyPuppetGrpcError(
                    'Your service token has no available endpoint, is your token correct?'
                )
            if 'port' not in data:
                raise WechatyPuppetGrpcError("can't find service server port")
            log.debug('get puppet ip address : <%s>', data)
            self.options.end_point = '{ip}:{port}'.format(**data)

        if not re.match(r'^(?:(?!-)[\d\w-]{1,63}(?<!-)\.)+(?!-)[\d\w]{1,63}(?<!-):\d{2,5}$',
                        self.options.end_point):
            raise WechatyPuppetConfigurationError(
                'Malformed endpoint format, should be {hostname}:{port}'
            )

        host, port = self.options.end_point.split(':')
        self.channel = Channel(host=host, port=port)

        # pylint: disable=W0212
        self.channel._authority = self.options.token

        self._puppet_stub = PuppetStub(self.channel)
Пример #2
0
 async def room_list(self) -> List[str]:
     """
     get all room list
     :return:
     """
     response = await self.puppet_stub.room_list()
     if response is None:
         raise WechatyPuppetGrpcError('can"t get room_list response')
     return response.ids
Пример #3
0
 async def contact_list(self) -> List[str]:
     """
     get contact list
     :return:
     """
     response = await self.puppet_stub.contact_list()
     if response is None:
         # TODO -> need to refactor the raised error
         raise WechatyPuppetGrpcError('response is invalid')
     return response.ids
Пример #4
0
 async def contact_alias(self, contact_id: str, alias: Optional[str] = None
                         ) -> str:
     """
     get/set contact alias
     :param contact_id:
     :param alias:
     :return:
     """
     response = await self.puppet_stub.contact_alias(
         id=contact_id, alias=alias)
     if response.alias is None and alias is None:
         raise WechatyPuppetGrpcError('can"t get contact<%s> alias' % contact_id)
     return response.alias
Пример #5
0
 async def message_image(self, message_id: str, image_type: ImageType = 3
                         ) -> FileBox:
     """
     get message image data
     :param message_id:
     :param image_type:
     :return:
     """
     response = await self.puppet_stub.message_image(id=message_id, type=image_type)
     json_response = json.loads(response.filebox)
     if 'base64' not in json_response:
         raise WechatyPuppetGrpcError('image response data structure is not correct')
     file_box = FileBox.from_base64(
         json_response['base64'],
         name=json_response['name']
     )
     return file_box