deeppavlov/utils/alexa/bot.py
def _handle_request(self, request: dict) -> dict:
        """Processes Alexa requests from skill server and returns responses to Alexa.

        Args:
            request: Dict with Alexa request payload and metadata.
        Returns:
            result: Alexa formatted or error response.
        """
        request_body: bytes = request['request_body']
        signature_chain_url: str = request['signature_chain_url']
        signature: str = request['signature']
        alexa_request: dict = request['alexa_request']

        if not self._verify_request(signature_chain_url, signature, request_body):
            return {'error': 'failed certificate/signature check'}

        timestamp_str = alexa_request['request']['timestamp']
        timestamp_datetime = datetime.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%SZ')
        now = datetime.utcnow()

        delta = now - timestamp_datetime if now >= timestamp_datetime else timestamp_datetime - now

        if abs(delta.seconds) > REQUEST_TIMESTAMP_TOLERANCE_SECS:
            log.error(f'Failed timestamp check for request: {request_body.decode("utf-8", "replace")}')
            return {'error': 'failed request timestamp check'}

        conversation_key = alexa_request['session']['user']['userId']

        if conversation_key not in self.conversations.keys():
            if self.config['multi_instance']:
                conv_agent = self._init_agent()
                log.info('New conversation instance level agent initiated')
            else:
                conv_agent = self.agent

            self.conversations[conversation_key] = \
                Conversation(config=self.config,
                             agent=conv_agent,
                             conversation_key=conversation_key,
                             self_destruct_callback=lambda: self._del_conversation(conversation_key))

            log.info(f'Created new conversation, key: {conversation_key}')

        conversation = self.conversations[conversation_key]
        response = conversation.handle_request(alexa_request)

        return response
def _handle_request(self, request: dict) -> dict:
        request_body: bytes = request['request_body']
        signature_chain_url: str = request['signature_chain_url']
        signature: str = request['signature']
        alexa_request: dict = request['alexa_request']

        if not self._verify_request(signature_chain_url, signature, request_body):
            return {'error': 'failed certificate/signature check'}

        timestamp_str = alexa_request['request']['timestamp']
        timestamp_datetime = datetime.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%SZ')
        now = datetime.utcnow()

        delta = now - timestamp_datetime if now >= timestamp_datetime else timestamp_datetime - now

        if abs(delta.seconds) > REQUEST_TIMESTAMP_TOLERANCE_SECS:
            log.error(f'Failed timestamp check for request: {request_body.decode("utf-8", "replace")}')
            return {'error': 'failed request timestamp check'}

        conversation_key = alexa_request['session']['user']['userId']

        if conversation_key not in self.conversations.keys():
            if self.config['multi_instance']:
                conv_agent = self._init_agent()
                log.info('New conversation instance level agent initiated')
            else:
                conv_agent = self.agent

            self.conversations[conversation_key] = \
                Conversation(config=self.config,
                             agent=conv_agent,
                             conversation_key=conversation_key,
                             self_destruct_callback=lambda: self._del_conversation(conversation_key))

            log.info(f'Created new conversation, key: {conversation_key}')

        conversation = self.conversations[conversation_key]
        response = conversation.handle_request(alexa_request)

        return response
def _handle_request ( self , request : dict ) -> dict : request_body : bytes = request [ 'request_body' ] signature_chain_url : str = request [ 'signature_chain_url' ] signature : str = request [ 'signature' ] alexa_request : dict = request [ 'alexa_request' ] if not self . _verify_request ( signature_chain_url , signature , request_body ) : return { 'error' : 'failed certificate/signature check' } timestamp_str = alexa_request [ 'request' ] [ 'timestamp' ] timestamp_datetime = datetime . strptime ( timestamp_str , '%Y-%m-%dT%H:%M:%SZ' ) now = datetime . utcnow ( ) delta = now - timestamp_datetime if now >= timestamp_datetime else timestamp_datetime - now if abs ( delta . seconds ) > REQUEST_TIMESTAMP_TOLERANCE_SECS : log . error ( f'Failed timestamp check for request: {request_body.decode("utf-8", "replace")}' ) return { 'error' : 'failed request timestamp check' } conversation_key = alexa_request [ 'session' ] [ 'user' ] [ 'userId' ] if conversation_key not in self . conversations . keys ( ) : if self . config [ 'multi_instance' ] : conv_agent = self . _init_agent ( ) log . info ( 'New conversation instance level agent initiated' ) else : conv_agent = self . agent self . conversations [ conversation_key ] = Conversation ( config = self . config , agent = conv_agent , conversation_key = conversation_key , self_destruct_callback = lambda : self . _del_conversation ( conversation_key ) ) log . info ( f'Created new conversation, key: {conversation_key}' ) conversation = self . conversations [ conversation_key ] response = conversation . handle_request ( alexa_request ) return response
Processes Alexa requests from skill server and returns responses to Alexa.

        Args:
            request: Dict with Alexa request payload and metadata.
        Returns:
            result: Alexa formatted or error response.
Processes Alexa requests from skill server and returns responses to Alexa .