def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.request, exc_info=True) speak_output = '' attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes exists_santa = True person_id = '' if 'santa' not in persistence_attr or 'id' not in persistence_attr[ 'santa']: #初回起動 exists_santa = False persistence_attr['santa'] = {} return AnswerClassIntentHandler(AbstractRequestHandler).handle( handler_input) if request_envelope.context.system.person is not None: person_id = request_envelope.context.system.person.person_id santa = is_santa(persistence_attr['santa'], person_id) subscription = is_skill_product(handler_input) if 'msg' in persistence_attr['session']: speak_output = _(data.WISH_ADD_NO_MSG) else: speak_output = _(data.NO_MSG) persistence_attr['session'] = {'dt': datetime.now().strftime('%s')} attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.request, exc_info=True) speak_output = '' attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes exists_santa = True person_id = None if 'santa' not in persistence_attr or 'id' not in persistence_attr[ 'santa']: #初回起動 exists_santa = False persistence_attr['santa'] = {} return AnswerClassIntentHandler(AbstractRequestHandler).handle( handler_input) if request_envelope.context.system.person is not None: person_id = request_envelope.context.system.person.person_id santa = is_santa(persistence_attr['santa'], person_id) subscription = is_skill_product(handler_input) if 'msg' in persistence_attr['session']: #1分経過してたら無効 logger.error(int(datetime.now().strftime('%s')), exc_info=True) logger.error(int(persistence_attr['session']['dt']), exc_info=True) if int(datetime.now().strftime('%s')) - int( persistence_attr['session']['dt']) > 60: speak_output = _(data.WISH_ADD_TIMEOUT_MSG) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) else: speak_output = _(data.WISH_ADD_NO_MSG) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) if 'msg' not in persistence_attr: persistence_attr['msg'] = [] idx = len(persistence_attr['msg']) txt = persistence_attr['session']['msg'] persistence_attr['msg'].append({}) persistence_attr['msg'][idx]['txt'] = txt persistence_attr['msg'][idx]['person_id'] = "{}".format(person_id) if person_id != "" and person_id is not None: speak_output = _(data.WISH_ADD_MSG).format(person_id, txt) else: speak_output = _(data.WISH_ADD_NONAME_MSG).format(txt) persistence_attr['session'] = {'dt': datetime.now().strftime('%s')} logger.error(persistence_attr, exc_info=True) logger.error(speak_output, exc_info=True) attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).response)
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.request, exc_info=True) speak_output = '' attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes person_id = '' if 'santa' not in persistence_attr or 'id' not in persistence_attr[ 'santa']: #初回起動 exists_santa = False persistence_attr['santa'] = {} return AnswerClassIntentHandler(AbstractRequestHandler).handle( handler_input) if request_envelope.context.system.person is not None: person_id = request_envelope.context.system.person.person_id password = get_resolved_query(handler_input.request_envelope.request, "password") message_number = get_resolved_query( handler_input.request_envelope.request, "message_number") logger.info(password, exc_info=True) if 'password' in persistence_attr['session']: if str(persistence_attr['session']['password']) == password: mymsg_id = int(persistence_attr['session']['delete']) logger.info(mymsg_id, exc_info=True) del persistence_attr['msg'][mymsg_id] del persistence_attr['session']['delete'] del persistence_attr['session']['password'] speak_output = _(data.WISH_DELETE_MSG) elif (str(persistence_attr['session']['password']) == 'a' and int(message_number) > 0): idx = int(message_number) - 1 del persistence_attr['msg'][idx] del persistence_attr['session']['delete'] del persistence_attr['session']['password'] speak_output = _( data.WISH_DELETE_PARENT_MSG).format(message_number) else: speak_output = _(data.WISH_DELETE_NG_MSG) else: speak_output = _(data.WISH_DELETE_NON_MSG) logger.info(persistence_attr['session'], exc_info=True) persistence_attr['session'] = {'dt': datetime.now().strftime('%s')} attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)
def test_save_persistent_attributes_without_persistence_adapter(self): session = Session() request_envelope = RequestEnvelope( version=None, session=session, context=None, request=None) attributes_manager = AttributesManager( request_envelope=request_envelope) with self.assertRaises(AttributesManagerException) as exc: attributes_manager.save_persistent_attributes() assert "Cannot save PersistentAttributes without persistence adapter!" in str(exc.exception), ( "AttributesManager should raise error when trying to save " "persistent attributes without persistence adapter" )
def test_save_persistent_attributes_without_changing_persistent_attributes(self): session = Session() request_envelope = RequestEnvelope( version=None, session=session, context=None, request=None) attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=MockPersistenceAdapter()) attributes_manager.save_persistent_attributes() assert attributes_manager._persistence_adapter.attributes == { "key_1": "v1", "key_2": "v2"}, ( "AttributesManager should do nothing if persistent attributes " "has not been changed")
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.request, exc_info=True) speak_output = '' attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes person_id = '' logger.info(request_envelope.context.system, exc_info=True) if request_envelope.context.system.person is None: # パーソナライズ無効 speak_output = _(data.WISH_DELETE_NOT_PERSONALIZE) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) else: person_id = request_envelope.context.system.person.person_id # 親か子か personClass = '' if 'personClass' in persistence_attr[person_id]: personClass = persistence_attr[person_id]['personClass'] else: return AnswerClassIntentHandler(AbstractRequestHandler).handle( handler_input) confirm = '消しません' resolved_value = get_resolved_value( handler_input.request_envelope.request, "confirm") resolved_id = get_resolved_id(handler_input.request_envelope.request, "confirm") if (resolved_value is not None): # Dialogを用いてるので原則として値は入ってくる confirm = resolved_value if confirm == '消します' and 'wish' in persistence_attr[person_id]: del (persistence_attr[person_id]['wish']) speak_output = _(data.WISH_DELETE_YES_MSG).format( persistence_attr[person_id]) elif confirm == '消します': speak_output = _(data.WISH_DELETE_NON_MSG).format( persistence_attr[person_id]) else: speak_output = _(data.WISH_DELETE_NO_MSG).format( persistence_attr[person_id]) attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)
def handle(self, handler_input): # type: (HandlerInput) -> Response _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) logger.info(request_envelope.context.system, exc_info=True) logger.info(request_envelope.context.system.person, exc_info=True) #DynamoDbをTest persistence_attr = attributes_manager.persistent_attributes persistence_attr['session'] = {'dt': datetime.now().strftime('%s')} exists_santa = True if 'santa' not in persistence_attr or 'id' not in persistence_attr[ 'santa']: #初回起動 exists_santa = False persistence_attr['santa'] = {} speak_output = '' person_id = '' # test用 'person_id': 'amzn1.ask.person.AHCN7IHVHTQBKRDNNDBYSWZRJISPUHOJEEZZW6MML255REWY2YOQFETSBE7STDPDAXOJUWBPIL5ZV6WZ5555HMZVI6YPKIVDWGOEL7KB'} if request_envelope.context.system.person is None and exists_santa == False: # パーソナライズ無効 speak_output = _(data.LAUNCH_NOT_PERSONALIZE) return ( handler_input.response_builder.speak(speak_output).response) elif exists_santa == False: # サンタ登録 speak_output = _(data.LAUNCH_NO_SANTA) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) elif request_envelope.context.system.person is not None: person_id = request_envelope.context.system.person.person_id santa = is_santa(persistence_attr['santa'], person_id) if santa == True: speak_output = _(data.LAUNCH_IS_PARENT).format(person_id) else: speak_output = _(data.LAUNCH_IS_CHILD).format(person_id) attributes_manager.save_persistent_attributes() logger.info(persistence_attr, exc_info=True) logger.info(speak_output, exc_info=True) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)
def test_save_persistent_attributes(self): session = Session() request_envelope = RequestEnvelope( version=None, session=session, context=None, request=None) attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=MockPersistenceAdapter()) attributes_manager.persistent_attributes = {"key": "value"} attributes_manager.save_persistent_attributes() assert attributes_manager._persistence_adapter.attributes == { "key": "value"}, ( "AttributesManager fails to save persistent attributes via " "persistence adapter")
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.context.system, exc_info=True) attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes speak_output = _(data.WELCOME_MESSAGE) person_id = '' if request_envelope.context.system.person is None: # パーソナライズ無効 speak_output = _(data.ANSWER_CLASS_NOT_PERSONALIZE) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) else: person_id = request_envelope.context.system.person.person_id if person_id not in persistence_attr: persistence_attr[person_id] = {} if 'personClass' in persistence_attr[person_id]: #登録済 pass personCount = get_count_person(persistence_attr) resolved_value = get_resolved_value( handler_input.request_envelope.request, "class") subscription = is_skill_product(handler_input) if (resolved_value is not None and resolved_value == "パパ"): if personCount[0] < 1 or subscription == True: persistence_attr[person_id]['personClass'] = 'parent' speak_output = _(data.ANSWER_CLASS_IS_PARENT).format( person_id, person_id) else: speak_output = _(data.ANSWER_CLASS_LIMIT_MSG) else: if personCount[1] < 2 or subscription == True: persistence_attr[person_id]['personClass'] = 'child' speak_output = _(data.ANSWER_CLASS_IS_CHILD).format(person_id) else: speak_output = _(data.ANSWER_CLASS_LIMIT_MSG) attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.context.system, exc_info=True) attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes persistence_attr['session'] = {'dt': datetime.now().strftime('%s')} person_id = '' exists_santa = True if 'santa' not in persistence_attr or 'id' not in persistence_attr[ 'santa']: #初回起動 exists_santa = False persistence_attr['santa'] = {} if request_envelope.context.system.person is not None: person_id = request_envelope.context.system.person.person_id speak_output = _(data.WELCOME_MESSAGE) if request_envelope.context.system.person is None: # パーソナライズ無効 speak_output = _(data.ANSWER_CLASS_NOT_PERSONALIZE) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) resolved_value = get_resolved_value( handler_input.request_envelope.request, "class") if (resolved_value is not None and resolved_value == "サンタ"): persistence_attr['santa']['id'] = person_id speak_output = _(data.ANSWER_CLASS_IS_PARENT).format( person_id, person_id) elif exists_santa == False: speak_output = _(data.ANSWER_CLASS_IS_NG).format(person_id) else: speak_output = _(data.ANSWER_CLASS_IS_CHILD).format(person_id) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).response)
def handle(self, handler_input): # type: (HandlerInput) -> Response _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) logger.info(request_envelope.context, exc_info=True) logger.info(request_envelope.context.system, exc_info=True) logger.info(request_envelope.context.system.person, exc_info=True) #DynamoDbをTest persistence_attr = attributes_manager.persistent_attributes speak_output = '' person_id = '' # test用 'person_id': 'amzn1.ask.person.AHCN7IHVHTQBKRDNNDBYSWZRJISPUHOJEEZZW6MML255REWY2YOQFETSBE7STDPDAXOJUWBPIL5ZV6WZ5555HMZVI6YPKIVDWGOEL7KB'} if request_envelope.context.system.person is None: # パーソナライズ無効 speak_output = _(data.LAUNCH_NOT_PERSONALIZE) return ( handler_input.response_builder.speak(speak_output).response) else: person_id = request_envelope.context.system.person.person_id if person_id not in persistence_attr: persistence_attr[person_id] = {} if 'personClass' not in persistence_attr[person_id]: speak_output = _(data.LAUNCH_NOT_CLASS) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) personClass = persistence_attr[person_id]['personClass'] if personClass == 'parent': speak_output = _(data.LAUNCH_IS_PARENT).format(person_id) else: speak_output = _(data.LAUNCH_IS_CHILD).format(person_id) attributes_manager.save_persistent_attributes() logger.info(persistence_attr, exc_info=True) logger.info(speak_output, exc_info=True) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.request, exc_info=True) speak_output = _(data.WISH_ADD_MSG) attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes person_id = '' if request_envelope.context.system.person is None: # パーソナライズ無効 speak_output = _(data.WISH_ADD_NOT_PERSONALIZE) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) else: person_id = request_envelope.context.system.person.person_id # 親か子か personClass = '' if 'personClass' in persistence_attr[person_id]: personClass = persistence_attr[person_id]['personClass'] else: return AnswerClassIntentHandler(AbstractRequestHandler).handle( handler_input) query = get_resolved_query(handler_input.request_envelope.request, "query") if (query is not None and personClass == 'child'): # Dialogを用いてるので原則として値は入ってくる persistence_attr[person_id]['wish'] = [query] speak_output = _(data.WISH_ADD_MSG).format(query) elif (query is not None and personClass == 'parent'): persistence_attr[person_id]['wish'] = [query] speak_output = _(data.WISH_ADD_PARENT_MSG).format(query) else: speak_output = _(data.WISH_ADD_MSG).format('聞き取れませんでした') attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.request, exc_info=True) speak_output = '' attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes persistence_attr['session'] = {'dt': datetime.now().strftime('%s')} person_id = '' exists_santa = True if 'santa' not in persistence_attr or 'id' not in persistence_attr[ 'santa']: #初回起動 exists_santa = False persistence_attr['santa'] = {} if request_envelope.context.system.person is not None: person_id = request_envelope.context.system.person.person_id santa = is_santa(persistence_attr['santa'], person_id) if request_envelope.context.system.person is None: # パーソナライズ無効 speak_output = _(data.WISH_LIST_NOT_PERSONALIZE) return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) else: person_id = request_envelope.context.system.person.person_id reprompt = True mymsg_id = -1 mymsg = '' if 'msg' not in persistence_attr: persistence_attr['msg'] = [] idx = len(persistence_attr['msg']) for msg in range(len(persistence_attr['msg'])): if persistence_attr['msg'][msg][ 'person_id'] == person_id and person_id != '': mymsg = persistence_attr['msg'][msg]['txt'] mymsg_id = msg if mymsg != '' and santa == False: x = random.randrange(1000, 9999) persistence_attr['session']['password'] = x persistence_attr['session']['delete'] = mymsg_id speak_output = _(data.WISH_LIST_CHILD_MSG).format(mymsg, x) elif mymsg == '' and santa == False: speak_output = _(data.WISH_LIST_NONE_CHILD_MSG) elif len(persistence_attr['msg']) < 1: speak_output = _(data.WISH_LIST_NONE_PARENT_MSG) else: speak_output = '' for msg in range(len(persistence_attr['msg'])): logger.info(persistence_attr['msg'][msg]['person_id'], exc_info=True) if (persistence_attr['msg'][msg]['person_id'] is not None and persistence_attr['msg'][msg]['person_id'] != 'None' and persistence_attr['msg'][msg]['person_id'] != ''): speak_output += _(data.WISH_LIST_PARENT_EXT_MSG).format( msg + 1, persistence_attr['msg'][msg]['person_id'], persistence_attr['msg'][msg]['txt']) else: speak_output += _(data.WISH_LIST_PARENT_MSG).format( msg + 1, persistence_attr['msg'][msg]['txt']) if speak_output != '': speak_output += '願い事は以上です。さて次は何をしましょうか。「一番めの願い事を削除」のように言うと願い事の削除もできますよ。' persistence_attr['session']['password'] = '******' persistence_attr['session']['delete'] = 'a' else: speak_output = _(data.WISH_LIST_NONE_PARENT_MSG) logger.info(speak_output, exc_info=True) logger.info(persistence_attr, exc_info=True) attributes_manager.save_persistent_attributes() if reprompt == True: return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) else: return ( handler_input.response_builder.speak(speak_output).response)
def handle(self, handler_input): _ = handler_input.attributes_manager.request_attributes["_"] request_envelope = handler_input.request_envelope logger.info(request_envelope.request, exc_info=True) speak_output = _(data.WISH_ADD_MSG) attributes_manager = AttributesManager( request_envelope=request_envelope, persistence_adapter=adapter) persistence_attr = attributes_manager.persistent_attributes persistence_attr['session'] = {'dt': datetime.now().strftime('%s')} exists_santa = True person_id = '' if 'santa' not in persistence_attr or 'id' not in persistence_attr[ 'santa']: #初回起動 exists_santa = False persistence_attr['santa'] = {} return AnswerClassIntentHandler(AbstractRequestHandler).handle( handler_input) if request_envelope.context.system.person is not None: person_id = request_envelope.context.system.person.person_id santa = is_santa(persistence_attr['santa'], person_id) query = get_resolved_query(handler_input.request_envelope.request, "query") subscription = is_skill_product(handler_input) if 'msg' not in persistence_attr: persistence_attr['msg'] = [] idx = len(persistence_attr['msg']) if (query is not None and santa == False): # Dialogを用いてるので原則として値は入ってくる if idx < 3 or subscription == True: #persistence_attr['santa'][len]['msg'] = query persistence_attr['session']['msg'] = query if person_id != "" and person_id is not None: speak_output = _(data.WISH_ADD_CONFIRM_MSG).format( person_id, query) else: speak_output = _( data.WISH_ADD_CONFIRM_NONAME_MSG).format(query) else: speak_output = _(data.WISH_ADD_LIMIT_MSG) attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).ask( speak_output).response) elif (query is not None and santa == True): if idx < 3 or subscription == True: logger.info(idx, exc_info=True) persistence_attr['msg'].append({}) persistence_attr['msg'][idx]['txt'] = query persistence_attr['msg'][idx]['person_id'] = person_id speak_output = _(data.WISH_ADD_PARENT_MSG).format(query) else: speak_output = _(data.WISH_ADD_LIMIT_MSG) else: speak_output = _(data.WISH_ADD_MSG).format('聞き取れませんでした') attributes_manager.save_persistent_attributes() return (handler_input.response_builder.speak(speak_output).ask( speak_output).response)