def handler(user_id: str, project: str) -> Response: """ Handler of TEAM_06_ADD_PROJECT intent, TEAM_06_ADD_PROJECT intent is activated when user says '[Aa]m (?P<project>.*)' returns question for task :return: Response """ # Check project_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) # Help the ASR if project == 'Hecker Tom' or project == 'hekatron' or project == 'Hecker Ton' or project == 'Hecker': project = 'Hackathon' # Create new Project try: project_id = projects[project] except KeyError: add_project(clockify_api_key, workspace_id, project_name=project) if set_project(user_id, project): msg = _('ASK_TASK') else: msg = _('ASK_PORJECT') response = ask(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(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_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 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_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'))
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 test_ask(self): """ Check is responses.ask returns Response(type_=RESPONSE_TYPE_ASK) """ r = ask('Question') self.assertIsInstance(r, Response) self.assertEqual(r.type_, RESPONSE_TYPE_ASK)