def __process_telegram_command(user: User, command, user_input): if is_not_blank(command): command_argument = get_command_argument(user_input, command) response = COMMAND_HANDLERS.get( command, handle_invalid_command)(command_argument) send_message(user, command, response)
def __process_telegram_commands(user: User, session: Session, commands): # Need to call Dialogflow to start a new session with main context here, if session retrieved from cache is new if session.is_new: detect_intent_via_event(session.id, 'NINJA_CAFE_MAIN_EVENT') individual_responses = filter( is_not_blank, map(__process_individual_telegram_command, commands)) response = "\n---\n".join(individual_responses) send_message(user, ", ".join(commands), session.id, response)
def __process_telegram_commands(user: User, session: Session, commands, user_input): chosen_command = commands.pop() result = __process_individual_telegram_command_with_parameter( chosen_command, user_input, user) if isinstance(result, list): send_message_with_link(user, ", ".join(commands), session.id, result[0], result[1]) else: send_message(user, ", ".join(commands), session.id, result)
def remove_team_handler(chat_id, arguments): teams = get_user_teams(chat_id) def generateKeyboardOptions(team): return generate_telegram_inline_keyboard_inputs( text=generate_team_string(team), type="/removeteamwithid", data=team.get("id"), ) inline_keyboard_options = list(map(generateKeyboardOptions, teams)) send_message( chat_id=chat_id, text="Select your team:", options={"reply_markup": { "inline_keyboard": inline_keyboard_options }})
def __submit_order(user: User, intent_result, session_id): order_items = get_current_order(user, session_id) response = intent_result.fulfillment_text order_api.create_order(user, order_items) clear_from_order(user, session_id) return send_message(user, intent_result.intent.display_name, session_id, response)
def start_handler(chat_id, arguments): # TODO read from .md file. # myteams - List the teams you are following # addteam - Search and select your teams. ex: /addteam {team-name} # removeteam - List your teams and select to remove text = "*Welcome to Matchday Reminder* \n Here is a list of available commands: \n `/addTeam {teamName}` Search and select a team to be notified about _\\(team name is required\\)_" json = send_message(chat_id=chat_id, text=text, options={"parse_mode": "MarkdownV2"}) print(json)
def __show_menu_response(user: User, intent_result, session_id): response = "Glad you asked! Here's what we offer:\n" for x in range(1, len(list(MENU_CODES_TO_OPTIONS.values())) + 1): response += "{}: {}\n".format( x, list(MENU_CODES_TO_OPTIONS.values())[x - 1]) response += "\nWhat would you like?\n" return send_message(user, intent_result.intent.display_name, session_id, response)
def __show_menu_options(user: User, intent_result, session_id): response = intent_result.fulfillment_text if is_not_blank(response): return send_message_with_options(user, intent_result.intent.display_name, session_id, response, *list(MENU_CODES_TO_OPTIONS.values())) else: return send_message(user, intent_result.intent.display_name, session_id, DEFAULT_ERROR_MESSAGE)
def __update_order(user: User, intent_result, session_id): order_items = get_items_from_response(intent_result) if len(order_items) > 0: response = intent_result.fulfillment_text add_to_order(user, session_id, order_items) return send_message_with_options(user, intent_result.intent.display_name, session_id, response, "I'm done!") else: response = "Sorry, I wasn't able to detect your order. Could you repeat yourself?" return send_message(user, intent_result.intent.display_name, session_id, response)
def __show_orders(user: User, intent_result, session_id): orders = order_api.list_orders(user) if len(orders) > 0: response = "Here are your current orders:\n" for x in range(1, len(orders) + 1): response += "{}. {}\n<Placed @ {}>\n\n"\ .format(x, orders[x - 1]['order_description'], orders[x - 1]['timestamp']) response += "\nHow else can I help you?" return send_message_with_options(user, intent_result.intent.display_name, session_id, response, *MAIN_SUGGESTIONS, row_width=1) else: response = "You have no orders yet. Let me know if you wanna order something!" return send_message(user, intent_result.intent.display_name, session_id, response)
def __confirm_order(user: User, intent_result, session_id): order_items = get_current_order(user, session_id) if len(order_items) > 0: response = "Here are the items in your order:\n" for name, count in order_items.items(): response += "- {}x {}\n".format( count, default_if_blank(MENU_CODES_TO_OPTIONS[name], 'N.A.')) response += "\nSubmit?" return send_message_with_options(user, intent_result.intent.display_name, session_id, response, "Yes, submit my order!", "Nah, cancel it") else: detect_intent_via_event(session_id, 'NINJA_CAFE_MAIN_EVENT') response = "Oops, you don't have any items in your order. Let me know how else I can help you!" return send_message(user, intent_result.intent.display_name, session_id, response)
def add_team_with_id_handler(chat_id, arguments): team_id = arguments[0] if arguments and len(arguments) > 0 else None if not team_id: error_text = "Something went wrong: `Team id` is required." print(error_text) send_message(chat_id=chat_id, text=error_text) return error_text try: add_follower(team_id=team_id, chat_id=chat_id) except Exception as err: print(err) send_message(chat_id=chat_id, text="Something went wrong.") return "Something went wrong." else: team = get_team_metadata(team_id) success_message = f"Successfully added you to followers of {team.get('name')}" print(success_message) send_message(chat_id=chat_id, text=success_message) return success_message
def add_team_handler(chat_id, arguments): team_name = arguments[0] if arguments and len(arguments) > 0 else "" if not len(team_name) > 3: print("Less then four letters on team name.") send_message(chat_id=chat_id, text="Team name cannot be less then three letters.") return teams = query_teams(team_name) if not teams: teams = fetch_teams(team_name) if not teams or not len(teams) > 0: print(f"No team was found for search: {team_name}.") send_message(chat_id=chat_id, text=f"No team was found with name _{team_name}_.") return def get_inline_keyboard_options(team): update_team_metadata(team) return generate_telegram_inline_keyboard_inputs( text=generate_team_string(team), type="/addteamwithid", data=team.get("id"), ) inline_keyboard_options = list(map(get_inline_keyboard_options, teams)) send_message( chat_id=chat_id, text="Select your team:", options={"reply_markup": { "inline_keyboard": inline_keyboard_options }})
def my_teams_handler(chat_id, arguments): teams = get_user_teams(chat_id) texts = list(map(generate_team_string, teams)) text = '\n'.join(texts) send_message(chat_id=chat_id, text=text)
def __display_default_response(user: User, intent_result, session_id): response = default_if_blank(intent_result.fulfillment_text, DEFAULT_ERROR_MESSAGE) return send_message(user, intent_result.intent.display_name, session_id, response)
def __cancel_order(user: User, intent_result, session_id): response = intent_result.fulfillment_text clear_from_order(user, session_id) return send_message(user, intent_result.intent.display_name, session_id, response)
def handle_invalid_intent(user: User, intent_result, session_id): response = "Sorry, I did not understand you. What were you saying?" return send_message(user, intent_result.intent.display_name, session_id, response)