https://github.com/lucasbpolo/berkeleyPortugueseBot
https://trello.com/b/FKvblX6h/campanha-de-prega%C3%A7%C3%A3o
- Git
- Node.js and npm
- Firebase CLI
You can download and install Git from the official website: https://git-scm.com/downloads.
You can download and install Node.js (which includes npm) from the official website: https://nodejs.org/en/download/.
To use the firebase deploy --only functions
command, you'll need to install the Firebase CLI and initialize it in your project.
-
To install the Firebase CLI, open a terminal or command prompt and run the following command:
npm install -g firebase-tools
. This will install the Firebase CLI globally on your system. -
Once the installation is complete, you can log in to your Firebase account by running the
firebase login
command. Follow the prompts to log in to your account. -
Now you should be able to use the
firebase deploy --only functions
command to deploy your Cloud Functions.
Make sure to grab the bot token, Firebase service account, Alba credentials, etc from Dropbox (ask Jonathan for access): https://www.dropbox.com/home/Schedules/BerkeleyPortugueseBotCredentials?di=left_nav_browse/.
- Clone the repository and navigate to the root folder.
- Run
npm install
in the root folder. - Navigate to the
functions
folder and runnpm install
again. - all the credentials from the dropbox to the
functions
folder. - To run the code locally, run
nodemon ./functions/dev.js
. - You can find the local bot at this URL: https://t.me/LucasBotLocalTestsBot.
- Run
firebase deploy --only functions
to deploy the bot. - After deployment, you can find the production bot at this URL: https://t.me/BerkeleyPortugueseBot.
File | Description | Details |
---|---|---|
./functions/bot.js | Main Bot Code | Imports telebot, alba credentials, and userids. Initializes a telebot instance with the token. |
./functions/dev.js | Local Code deployment | Will send data to LucasBotLocalTestsBot. |
./functions/index.js | Production Code deployment | Will send data to BerkeleyPortugueseBot. |
File | Description | Details |
---|---|---|
utils/parseAlbaHTML.js | Parse Alba HTML | Extracts id, territory, city, status, and details. Exports these values. |
utils/requestAlbaTerritories.js | Request Alba Territories | Fetches HTML for requesting territories. |
utils/validateUserIds.js | Validate User IDs | Validates the user IDs for authentication. |
The code that runs locally uses the node-telegram-bot-api
library in polling mode, while the code that runs in the Firebase function uses the webhook functionality of the library. The main purpose of having a local version is to expedite development.
The initialization of the local code is in ./functions/dev.js
, while the initialization of the code that runs in the Firebase function is in ./functions/index.js
. Both of these files reuse code for the bot that is located in ./functions/bot.js
.
This code initializes a Telegram bot using the node-telegram-bot-api library. It imports several utility functions for validating user IDs, parsing HTML from the Alba website, and requesting territories from Alba.
The init function takes an object with several properties as its argument: db, telegramToken, albaCookie, and env. These properties represent the Firebase database instance, the Telegram bot token, the Alba cookie for authentication, and the current environment (development or production), respectively.
The isDevEnv variable is set to true if the current environment is development (env === 'DEV'). This variable is used to determine whether or not to use polling mode when initializing the bot.
The bot variable is an instance of the TelegramBot class, initialized with the provided Telegram bot token and an options object. The options object sets the polling property to true if the current environment is development (as determined by the value of isDevEnv).
The commands object defines several commands that the bot can respond to. Each command has a corresponding description.
The bot listens for messages that match the /start command using the onText method. When a message matching this command is received, the bot sends a welcome message to the user that includes a list of available commands.
The bot also listens for messages that match the /cadastro command. When a message matching this command is received, the bot retrieves the current value of the authorizedUserIds node from the Firebase database and checks if the user’s ID is already in the array. If it is not, the user’s ID is added to the array and the value of the authorizedUserIds node is updated in the database. The user’s information is also saved to a new node in the database.
The bot also listens for three other commands: /territorio, /devolver, and /experiencia.
When the bot receives the /territorio command, it makes a request to the requestAlbaTerritories function with an albaCookie parameter. It then validates the user IDs using the validateUserIds function and the db parameter. If the user IDs are validated, the bot parses the HTML returned from the requestAlbaTerritories function using the parseAlbaHTML function. It then selects a random territory from the parsed data and sends a message to the user with information about the selected territory.
When the bot receives the /devolver command, it validates the user IDs using the validateUserIds function and the db parameter. If the user IDs are validated, it sends a message to the user indicating that their territory has been returned.
When the bot receives the /experiencia command, it validates the user IDs using the validateUserIds function and the db parameter. If the user IDs are validated, it sends a message to the user asking them to write their experience and sets their chat state to 'awaitingExperience'.