def test_create_request(): request = create_request(SAMPLE_REQUEST) assert isinstance(request, ViberUnsubscribedRequest) assert request.event_type == SAMPLE_REQUEST['event'] assert request.timestamp == SAMPLE_REQUEST['timestamp'] assert request.user_id == SAMPLE_REQUEST['user_id']
def parse_request(self, request_data): self._logger.debug("parsing request") request_dict = json.loads(request_data.decode( ) if isinstance(request_data, bytes) else request_data) request = create_request(request_dict) self._logger.debug(u"parsed request={0}".format(request)) return request
async def webhook_handle(self, request) -> web.Response: data = await request.json() viber_request = create_request(data) # type: ViberRequest t = self.loop.create_task(self._process_request(viber_request)) # TODO add error processing return web.Response()
def test_create_request_missing_event(): sample_request = dict( timestamp=datetime.now(), message_token="912661846655238145", sender=dict( id="01234567890A=", name="viberUser", avatar="http://avatar_url" ), message=dict( type=MessageType.TEXT, text="HI!" )) with pytest.raises(Exception) as exc: create_request(sample_request) assert exc.value.message.startswith("request is missing field 'event'")
def test_create_request(): request = create_request(SAMPLE_REQUEST) assert isinstance(request, ViberSeenRequest) assert request.event_type == SAMPLE_REQUEST['event'] assert request.timestamp == SAMPLE_REQUEST['timestamp'] assert request.meesage_token == SAMPLE_REQUEST['message_token'] assert request.user_id == SAMPLE_REQUEST['user_id']
def test_create_request(): request = create_request(SAMPLE_REQUEST) assert isinstance(request, ViberSubscribedRequest) assert request.event_type == SAMPLE_REQUEST['event'] assert request.timestamp == SAMPLE_REQUEST['timestamp'] assert request.user.id == SAMPLE_REQUEST['user']['id'] assert request.user.name == SAMPLE_REQUEST['user']['name'] assert request.user.avatar == SAMPLE_REQUEST['user']['avatar'] assert request.user.country == SAMPLE_REQUEST['user']['country'] assert request.user.language == SAMPLE_REQUEST['user']['language']
def test_create_request(): request = create_request(SAMPLE_REQUEST) assert isinstance(request, ViberMessageRequest) assert request.event_type == SAMPLE_REQUEST['event'] assert request.timestamp == SAMPLE_REQUEST['timestamp'] assert request.message_token == SAMPLE_REQUEST['message_token'] assert request.silent == SAMPLE_REQUEST['silent'] assert request.sender.id == SAMPLE_REQUEST['sender']['id'] assert request.sender.name == SAMPLE_REQUEST['sender']['name'] assert request.sender.avatar == SAMPLE_REQUEST['sender']['avatar'] assert isinstance(request.message, TextMessage)
def test_create_request(): request = create_request(SAMPLE_REQUEST) assert isinstance(request, ViberConversationStartedRequest) assert request.event_type == SAMPLE_REQUEST['event'] assert request.timestamp == SAMPLE_REQUEST['timestamp'] assert request.message_token == SAMPLE_REQUEST['message_token'] assert request.type == SAMPLE_REQUEST['type'] assert request.context == SAMPLE_REQUEST['context'] assert request.user.id == SAMPLE_REQUEST['user']['id'] assert request.user.name == SAMPLE_REQUEST['user']['name'] assert request.user.avatar == SAMPLE_REQUEST['user']['avatar'] assert request.user.country == SAMPLE_REQUEST['user']['country'] assert request.user.language == SAMPLE_REQUEST['user']['language'] assert request.subscribed == SAMPLE_REQUEST['subscribed']
def test_user_has_no_avatar(): SAMPLE_REQUEST = dict(event=EventType.MESSAGE, timestamp=datetime.now(), message_token="912661846655238145", sender=dict(id="01234567890A=", name="viberUser"), message=dict(type=MessageType.TEXT, text="HI!"), silent=False) request = create_request(SAMPLE_REQUEST) assert isinstance(request, ViberMessageRequest) assert request.event_type == SAMPLE_REQUEST['event'] assert request.timestamp == SAMPLE_REQUEST['timestamp'] assert request.message_token == SAMPLE_REQUEST['message_token'] assert request.sender.id == SAMPLE_REQUEST['sender']['id'] assert request.sender.name == SAMPLE_REQUEST['sender']['name'] assert isinstance(request.message, TextMessage)
def parse_message(self, request: Request) -> Message: # NOTE: There is no way to get the body # after processing the request in DRF. # # Verify signature # sign = request.META.get('HTTP_X_VIBER_CONTENT_SIGNATURE') # if not self.bot.verify_signature(request.body, sign): # raise IMApiException(f'Viber message not verified; ' # f'Data={request.data}; Sign={sign};') # Parse message data in to viber types vb_request = create_request(request.data) try: return self._get_message(vb_request) except Exception as err: # TODO: remove this after development log.exception(f'Parse message error; Message={vb_request}; ' f'Error={err};') return Message(MessageType.UNDEFINED)
def test_request_is_chat_extension(): SAMPLE_REQUEST = dict(event=EventType.MESSAGE, timestamp=datetime.now(), message_token="912661846655238145", chat_id=5048086924903818307, reply_type='message', sender=dict(id="01234567890A=", name="viberUser"), message=dict(type=MessageType.TEXT, text="HI!"), silent=False) request = create_request(SAMPLE_REQUEST) assert isinstance(request, ViberMessageRequest) assert request.event_type == SAMPLE_REQUEST['event'] assert request.timestamp == SAMPLE_REQUEST['timestamp'] assert request.message_token == SAMPLE_REQUEST['message_token'] assert request.sender.id == SAMPLE_REQUEST['sender']['id'] assert request.sender.name == SAMPLE_REQUEST['sender']['name'] assert request.chat_id == SAMPLE_REQUEST['chat_id'] assert request.reply_type == SAMPLE_REQUEST['reply_type'] assert isinstance(request.message, TextMessage)
def test_missing_user_optional_params(): request_dict = dict(event=EventType.CONVERSATION_STARTED, timestamp=datetime.now(), message_token="912661846655238145", type="open", context="context info", user=dict(id="01234567890A=", name="viberUser")) request = create_request(request_dict) # should not fail assert isinstance(request, ViberConversationStartedRequest) assert request.event_type == request_dict['event'] assert request.timestamp == request_dict['timestamp'] assert request.message_token == request_dict['message_token'] assert request.type == request_dict['type'] assert request.context == request_dict['context'] assert request.user.id == request_dict['user']['id'] assert request.user.name == request_dict['user']['name'] assert request.user.avatar is None assert request.user.country is None assert request.user.language is None assert request.subscribed is None
def parse_request(self, request_data): self._logger.debug("parsing request") request_dict = json.loads(request_data) request = create_request(request_dict) self._logger.debug(u"parsed request={0}".format(request)) return request