This Go program calculates a user's age based on the year of birth (yob) provided in a Slack command. The program utilizes the slacker library for Slack bot development and reads environment variables from a .env file.
-
Create a
.env
file in the project root with the following format:SLACK_BOT_TOKEN=your_slack_bot_token SLACK_APP_TOKEN=your_slack_app_token
-
run the program:
go run main.go
The bot will listen for commands in Slack and respond with the calculated age.
- Calls the
loadEnv
function to read environment variables from the.env
file.
- Uses the Slack API token from the environment variables to create a new Slack API client.
- Defines a Slack bot command that calculates age based on the provided year of birth.
- Sets up context for the bot and starts listening for Slack events.
- Opens the
.env
file and reads it line by line.
- Parses key-value pairs from each line and sets them as environment variables.
- Extracts the "year" parameter from the command request.
- Converts the year parameter to an integer and calculates the age based on the current year.
- Prepares a response message with the calculated age.
- Replies to the Slack channel with the calculated age.
Feel free to adjust the configuration, and if you encounter any issues or have suggestions for improvement, please open an issue or submit a pull request.
Happy coding! 🚀👨💻