def handler() -> Response: """ A very basic handler of SMALLTALK__GREETINGS intent, SMALLTALK__GREETINGS intent is activated when user says 'Hello' returns translated 'Hello' greeting :return: Response """ try: target = context.attributes['target'][0] response = tell(setStateAndGetText(target)) # We return the response return response except: return tell('')
def handler() -> Response: """ A sample handler of AMUSEMENT__JOKE intent, AMUSEMENT__JOKE intent is activated when user asks to tell him a joke returns a random joke from Chuck Norris jokes database: http://api.icndb.com/jokes/random :return: Response """ try: # We request a random joke from icndb with time-out set to 10 seconds response = requests.get('http://api.icndb.com/jokes/random', timeout=10) # We parse the response json or raise exception if unsuccessful response.raise_for_status() data = response.json() # We get the joke from the response data joke = data['value']['joke'] if data.get('type') == 'success' else None # We format our response to user or ask for an excuse if joke: msg = _('HELLOAPP_JOKE', joke=joke) else: msg = _('HELLOAPP_RESPONSE_ERROR') except requests.exceptions.RequestException as err: msg = _('HELLOAPP_REQUEST_ERROR', err=err) # We create a response with either joke or error message return tell(msg)
def handler() -> Response: try: print('nextStep') msg = nextStep() print(msg) except: msg = '' return tell(msg)
def handler() -> Response: try: print('repeat') msg = currentStep() print(msg) except: msg = '' return tell(msg)
def handler() -> Response: """ Find sense of life :return: Response """ try: msg = getIngredients() except: msg = '' return tell(msg)
def mood_handler(): try: number = randrange(10) # TODO: add functionality to create personalized preferences, social/close friends group, to trigger a call. if number < 3: # TODO: Maybe add jokes in a database and source from there # Jokes sourced from "https://github.com/derphilipp/Flachwitze" response = requests.get( 'https://raw.githubusercontent.com/derphilipp/Flachwitze/master/README.md', timeout=10) raw_jokes = response.text.split("##")[1].split("\n")[2:] jokes = [j[2:] for j in raw_jokes] msg = _('HAPPINESS_JOKE', joke=jokes[randrange(len(jokes))]) response = tell(msg) elif number < 6: # TODO: Add more URL's and if possible learn from user history video_ids = ["DODLEX4zzLQ", "dQw4w9WgXcQ", "tvMO9TNfdHs"] msg = _("HAPPINESS_CHECK_PHONE") response = tell(msg) response.card = Card( title=_("HAPPINESS_TITLE"), subtitle=_("HAPPINESS_SUB_TITLE"), action= f"https://www.youtube.com/watch?v={video_ids[randrange(len(video_ids))]}", actiontext=_("HAPPINESS_ACTION_TEXT")) else: msg = _("HAPPINESS_CHECK_PHONE") response = tell(msg) response.card = Card( title=_("HAPPINESS_TITLE"), subtitle=_("HAPPINESS_SUB_TITLE"), action= f"https://soundcloud.com/mariagrazia84/pharrell-williams-happy", actiontext=_("HAPPINESS_ACTION_TEXT")) except requests.exceptions.RequestException as err: msg = _('HAPPINESS_REQUEST_ERROR', err=err) response = tell(msg) # We create a response with either joke or error message return response
def handler(user_id: str) -> Response: """ Handler of TEAM_06_OPEN_TIME_TRACKING intent, TEAM_06_OPEN_TIME_TRACKING intent is activated when user says 'zeiterfassung stoppen' welcomes user :return: Response """ user_info = get_user(user_id) if user_info: update_user(user_info) else: create_user(user_id) clockify_api_key = get_clockify_api_key(user_id) if clockify_api_key is None or clockify_api_key == '': user_token = get_user_token(user_id) if user_token is None or user_token == '': user_token = get_random_string(4) set_user_token(user_id, user_token) msg = _('WELCOME_NEW_USER') response = tell(msg) response.card = Card( type_="GENERIC_DEFAULT", title="Time Tracker: Hinterlege deinen Clockify Account", text="User Token: {token}".format(token=user_token), action=get_auth_url(token=user_token), action_text="Klick hier, um einen Account zu hinterlegen.") else: clockify = get_clockify(clockify_api_key) clockify_id = clockify['user_id'] workspace_id = clockify['active_workspace_id'] time_entries = get_time_entries(clockify_api_key=clockify_api_key, workspace_id=workspace_id, user_id=clockify_id) running_timer = check_running_timer(time_entries) # Get time tracking status if running_timer: msg = _('WELCOME_RETURNING_USER') msg = msg + " " + _('WELCOME_STOP_SELECTION') else: msg = _('WELCOME_SELECTION') response = ask(msg) return response
def handler() -> Response: """ A very basic handler of SMALLTALK__GREETINGS intent, SMALLTALK__GREETINGS intent is activated when user says 'Hello' returns translated 'Hello' greeting :return: Response """ # We get a translated message msg = _('HELLOAPP_HELLO') # We create a simple response response = tell(msg) # We return the response return response
def handler_memo(text: str): print(context) user_id = '2' if start_round(text): topic = text.split("thema ")[-1] cursor = connection.cursor() if topic == text: question_id, quiz, answer, _ = cursor.execute( "select questions.id, quiz, answer, max(user_questions.id) as max_id from questions left join user_questions on questions.id = user_questions.questions_id\ where user_id = ? and step<6 group by questions.id order by max_id limit 1", [user_id]).fetchone() else: question_id, quiz, answer, _ = cursor.execute( "select questions.id, quiz, answer, max(user_questions.id) as max_id from questions left join user_questions on questions.id = user_questions.questions_id\ where user_id = ? and step<6 and topic = ? group by questions.id order by max_id limit 1", [user_id, topic]).fetchone() if quiz is None: msg = _("NO_MORE_QUIESTION") return tell(msg) cursor.execute("insert into user_questions (questions_id) values (?)", [question_id]) connection.commit() return ask(quiz) else: cursor = connection.cursor() last_question_id, answer, step = cursor.execute( "select questions_id, answer, step from user_questions uq\ JOIN questions q on q.id = uq.questions_id where q.user_id = ? order by uq.id desc limit 1", [user_id]).fetchone() if similar_answer(answer, text): step += 1 else: step = max(1, step - 1) cursor.execute("update questions set step = ? where id = ?", [step, last_question_id]) connection.commit() return tell(_('END_QUESTION_REVIEW'))
async def handler() -> Response: """ A handler of SMALLTALK__GREETINGS intent. SMALLTALK__GREETINGS intent is activated when user says 'Hello' returns translated 'Hello' greeting :return: Response """ # Get a translated message msg = _("HELLOAPP_HELLO") # Create a simple response response = tell(msg) # Return the response return response
def handler() -> Response: """ This handler is the first point of contact when your utterance is actually resolved! It will make sure to send you funny memes to your phone. :return: Response """ # We get a translated message msg = _('RANDOM_MEME_AFFIRMATION') # We create a simple response response = tell(msg) response.card = Card(type_="GENERIC_DEFAULT", title=_("RANDOM_MEME_TITLE"), sub_title=_("RANDOM_MEME_SUB_TITLE"), action=get_random_meme_url(), action_text=_("RANDOM_MEME_ACTION_TEXT")) return response
def handler(user_id: str) -> Response: """ Handler of TEAM_06_STOP_TIME_TRACKING intent, TEAM_06_STOP_TIME_TRACKING intent is activated when user says 'zeiterfassung stoppen' stops running timer :return: Response """ clockify_api_key = get_clockify_api_key(user_id) clockify = get_clockify(clockify_api_key) clockify_id = clockify['user_id'] workspace_id = clockify['active_workspace_id'] time_entries = get_time_entries(clockify_api_key, workspace_id, clockify_id) project_ids = get_project_ids(clockify_api_key, workspace_id) # Get time tracking status running_timer = check_running_timer(time_entries) if running_timer: # Stop time tracking now = datetime.utcnow() now_str = now.isoformat() now_str = now_str.split('.')[0] + ".000Z" time_entrie = stop_time_entrie(clockify_api_key, workspace_id, user_id=clockify_id, end_datetime=now_str) project = project_ids[time_entrie['projectId']] duration = parse_duration(time_entrie['timeInterval']['duration']) task = time_entrie['description'] set_project(user_id, '') set_task(user_id, '') msg = _('STOP_COMFIRMATION', project=project, task=task, duration=duration) else: msg = _('STOP_ERROR') response = tell(msg) return response
def handler(user_id: str, task: str) -> Response: """ Handler of TEAM_06_ADD_TASK intent, TEAM_06_ADD_TASK intent is activated when user says '(ich möchte|ich werde) (die|den|das)(?P<task>.*)' returns confirmation start timer :return: Response """ set_task(user_id, task) # Get project from db project = get_project(user_id) clockify_api_key = get_clockify_api_key(user_id) clockify = get_clockify(clockify_api_key) clockify_id = clockify['user_id'] workspace_id = clockify['active_workspace_id'] projects = get_projects(clockify_api_key, workspace_id) project_id = projects[project] if project is None: msg = _('ASK_PROJECT') response = ask(msg) elif task is None: msg = _('ASK_TASK') response = ask(msg) else: # Start time tracking now = datetime.utcnow() now_str = now.isoformat() now_str = now_str.split('.')[0] + ".000Z" add_time_entrie(clockify_api_key, workspace_id, project_id, task, now_str, end_datetime=None) msg = _('START_COMFIRMATION') response = tell(msg) return response
def handler(stt: str, user_id: str): stt = stt.lower() try: if stt.lower() == 'spiel beenden': with CircuitBreakerSession() as session: session.delete( 'https://n3i39w6xtc.execute-api.eu-west-1.amazonaws.com/prod/delsession?id=' + user_id) return tell("Auf Wiedersehen. bis bald") logger = logging.getLogger(__name__) logger.info("**** CVI Context = " + str(context)) with CircuitBreakerSession() as session: logger.info("**** user_hash = " + str(user_id)) response = session.get( 'https://hi4m6llff6.execute-api.eu-west-1.amazonaws.com/prod/a?id=' + user_id + '&stt=' + stt) return ask_freetext(response.text) except Exception as e: logger.info("**** Exception = " + str(e)) return ask_freetext("Es ist ein Fehler aufgetreten!")
def handler(user_id: str) -> Response: """ Handler of TEAM_06_START_TIME_TRACKING intent, TEAM_06_START_TIME_TRACKING intent is activated when user says 'zeiterfassung starten' returns question for project :return: Response """ user_info = get_user(user_id) if user_info: update_user(user_info) else: create_user(user_id) clockify_api_key = get_clockify_api_key(user_id) if clockify_api_key is None or clockify_api_key == '': user_token = get_user_token(user_id) if user_token is None or user_token == '': user_token = get_random_string(4) set_user_token(user_id, user_token) msg = _('WELCOME_NEW_USER') response = tell(msg) response.card = Card( type_="GENERIC_DEFAULT", title="Time Tracker: Hinterlege deinen Clockify Account", text="User Token: {token}".format(token=user_token), action=get_auth_url(token=user_token), action_text="Klick hier, um einen Account zu hinterlegen.") else: msg = _('ASK_PROJECT') response = ask(msg) return response
def handler(number: int) -> Response: """ The implementation :param number: :return: Response """ try: # We check if value is in range of 1 to 10 assert 1 <= number <= 10 # We get a random number if number == randint(1, 10): # ... and congratulate the winner! msg = _('HELLOAPP_NUMBER_SUCCESS_MESSAGE') else: # ... or encourage them to keep trying msg = _('HELLOAPP_NUMBER_WRONG_MESSAGE') response = tell(msg) except (AssertionError, TypeError, ValueError): msg = _('HELLOAPP_NO_NUMBER_MESSAGE') # We create a response with NO_NUMBER_MESSAGE and ask to repeat the number response = ask(msg) return response
def handler_card(text: str) -> Response: print('*******') print(context) user_id = '2' if not has_open_question(user_id): cursor = connection.cursor() cursor.execute("insert into questions (user_id) values (?) ", [user_id]) connection.commit() msg = _('ASK_YOUR_QUESTION') return ask(msg) elif has_open_question_no_quiz(user_id): cursor = connection.cursor() question = get_last_quiz(user_id) cursor.execute("update questions set quiz = ? where id = ?", [text, question[0]]) connection.commit() msg = _('INSERT_YOUR_ANSWER') return ask(msg) elif has_open_question_no_answer(user_id): cursor = connection.cursor() question = get_last_quiz(user_id) cursor.execute("update questions set answer = ? where id = ?", [text, question[0]]) connection.commit() msg = _('WHAT_IS_THE_TOPIC') return ask(msg) else: cursor = connection.cursor() question = get_last_quiz(user_id) cursor.execute("update questions set topic = ? where id = ?", [text, question[0]]) connection.commit() msg = _('DONE') return tell(msg) print("AFTER ALL IFS")
def test_tell(self): """ Check if responses.tell returns Response(type_=RESPONSE_TYPE_TELL) """ r = tell('Hello') self.assertIsInstance(r, Response) self.assertEqual(r.type_, RESPONSE_TYPE_TELL)
def handler(user_id: str) -> Response: """ Handler of TEAM_06_SHOW_TIME_TRACKING intent, TEAM_06_SHOW_TIME_TRACKING intent is activated when user says 'Stunden' returns booked working hours :return: Response """ user_info = get_user(user_id) if user_info: update_user(user_info) else: create_user(user_id) clockify_api_key = get_clockify_api_key(user_id) if clockify_api_key is None or clockify_api_key == '': user_token = get_user_token(user_id) if user_token is None or user_token == '': user_token = get_random_string(4) set_user_token(user_id, user_token) msg = _('WELCOME_NEW_USER') response = tell(msg) response.card = Card( type_="GENERIC_DEFAULT", title="Time Tracker: Hinterlege deinen Clockify Account", text="User Token: {token}".format(token=user_token), action=get_auth_url(token=user_token), action_text="Klick hier, um einen Account zu hinterlegen." ) else: try: clockify = get_clockify(clockify_api_key) clockify_id = clockify['user_id'] workspace_id = clockify['active_workspace_id'] project_ids = get_project_ids(clockify_api_key, workspace_id) time_entries = get_time_entries(clockify_api_key, workspace_id, clockify_id) time_entrie = time_entries[0] if time_entrie['timeInterval']['duration'] is None: time_entrie = time_entries[1] if time_entrie: project = project_ids[time_entrie['projectId']] duration = parse_duration(time_entrie['timeInterval']['duration']) task = time_entrie['description'] msg = _("SHOW_TIME", project=project, duration=duration, task=task) response = tell(msg) response.card = Card( type_="GENERIC_DEFAULT", title="Time Tracker: Clockify Link", text="Hier ist der Link zu deinen gebuchten Stunden:", action=get_clockify_url(), action_text="Öffnen" ) else: msg = _("SHOW_ERROR") response = tell(msg) except: msg = _("SHOW_ERROR") response = tell(msg) return response
def handler() -> Response: try: msg = setStep(0) except requests.exceptions.RequestException as err: msg = '' return tell(msg)