If I want the query the title of a module from the module code, how would I do this?
In this instance, we set {acadYear} as 2023-2024, and the user will input their module code.
def get_mod_title(module_code: str) -> str:
endpoint = f'https://api.nusmods.com/v2/2023-2024/'
f'modules/{module_code}.json'
response = requests.get(endpoint)
description = response.json()
title = description['title']
return title
Inline Keyboard Buttons
Let's say we want to see all the focus areas via INLINE KEYBOARD AREAS
Define the the function
Create the Keyboard via a 2D list of of InLineKeyboardButtonobjects
-> The number of InLineKeyboardButton() elements in a sublist represents the number of buttons in a row
async def focusarea(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
keyboard = [
[ # 4 Buttons in a row
InlineKeyboardButton(text="Algo & Theory", callback_data="Algorithms and Theory"),
InlineKeyboardButton(text="AI", callback_data="Artificial Intelligence"),
InlineKeyboardButton(text="G & G", callback_data="Computer Graphics and Games"),
],
[
InlineKeyboardButton(text="Security", callback_data="Computer Security"),
InlineKeyboardButton(text="DB", callback_data="Database Systems"),
InlineKeyboardButton(text="MIR", callback_data="Multimedia Information Retrieval"),
],
[ # 4 buttons in a row
InlineKeyboardButton(text="Networks", callback_data="Networking and Distributed Systems"),
InlineKeyboardButton(text="Parallel", callback_data="Parallel Computing"),
InlineKeyboardButton(text="Prog Langs", callback_data="Programming Languages"),
InlineKeyboardButton(text="SWE", callback_data="Software Engineering"),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(text="Please choose a focus area: ", reply_markup=reply_markup)
Inline Keyboard Markup that displays how the keyboard is displayed
Sending the message "Please choose a focus area: "
We want the message to change and reflect the user's chosen focus area and it's primaries. Let's create a helper function get_primaries()
Note: we have previously created a function get_primaries_text()
def get_primaries(focus_area_title: str) -> str:
primaries_list = []
match focus_area_title:
case "Algorithms and Theory":
primaries_list = PRIMARIES_LIST[0]
case "Artificial Intelligence":
primaries_list = PRIMARIES_LIST[1]
case "Computer Graphics and Games":
primaries_list = PRIMARIES_LIST[2]
case "Computer Security":
primaries_list = PRIMARIES_LIST[3]
case "Database Systems":
primaries_list = PRIMARIES_LIST[4]
case "Multimedia Information Retrieval":
primaries_list = PRIMARIES_LIST[5]
case "Networking and Distributed Systems":
primaries_list = PRIMARIES_LIST[6]
case "Parallel Computing":
primaries_list = PRIMARIES_LIST[7]
case "Programming Languages":
primaries_list = PRIMARIES_LIST[8]
case "Software Engineering":
primaries_list = PRIMARIES_LIST[9]
case _:
primaries_list = []
return get_primaries_text(primaries_list)
Create the display_focus_area() function
async def display_focus_area(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# The Inline Keyboard Provides a Callback Query that waits for the user's answer
query = update.callback_query
await query.answer()
# Obtains the primaries text based on the data provided by the Query
focus_area_primaries = get_primaries(query.data)
# Changes the 'Please select' message to:
await query.edit_message_text(text=f"Selected option: <b>{query.data}</b>\n"
f"Enter '(MODULE_CODE)' to find out more:\n\n"
f"Primaries: \n{focus_area_primaries}",
parse_mode=ParseMode.HTML)
Explanation
query = update.callback_query
Retrieve the callback query (info) from the update (i.e. the button the user clicked on)
await query.answer()
Informs telegram that the query has been received
focus_area_primaries = get_primaries(query.data)
Obtains the text of primaries based on data from query
await query.edit_message_text(text=f"Selected option: <b>{query.data}</b>\n"
f"Enter '(MODULE_CODE)' to find out more:\n\n"
f"Primaries: \n{focus_area_primaries}",
parse_mode=ParseMode.HTML)
Changes the original message that contained the inline keyboard to
-> Show the user's selected option in bold
-> Prompt the user to enter a module code
-> Show the primary modules in that focus area