Integrating API's
If you are a CS student, you may start to think about the different Focus Areas and what you may want to take.
Let's create a telebot that does the following.
Shows us what Focus Areas there are in CS
What modules are associated with the respective Focus Areas
Retrieve information about the module
First, import the following packages
import asyncio
import nest_asyncio
from typing import Final
from telegram import (Update, InlineKeyboardButton, InlineKeyboardMarkup)
from telegram.ext import (Application, CallbackQueryHandler, CommandHandler,
MessageHandler, filters, ContextTypes)
from telegram.constants import ParseMode
import requests
nest_asyncio.apply()Next, we replace the Token with our unique token ID
TOKEN: Final = '' # NEVER SHARE THIS PUBLICLYWe create an immutable list with each sublist representing a focus area
We will make use of the NUSMods API: https://api.nusmods.com/v2/
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.
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 ofInLineKeyboardButton()elements in a sublist represents the number of buttons in a row
Inline Keyboard Markup that displays how the keyboard is displayed
Sending the message
"Please choose a focus area: "
The user clicks on one of the buttons.
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()
Create the display_focus_area() function
Explanation
Retrieve the callback query (info) from the update (i.e. the button the user clicked on)
Informs telegram that the query has been received
Obtains the text of primaries based on data from
queryChanges 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
The user inputs a module code.
Last updated