https://github.com/anjasfedo/go-slack-age-bot
Golang program utilizing the slacker library to create a Slack bot that calculates user age.
https://github.com/anjasfedo/go-slack-age-bot
golang learning-by-doing slack slack-bot slacker
Last synced: about 1 month ago
JSON representation
Golang program utilizing the slacker library to create a Slack bot that calculates user age.
- Host: GitHub
- URL: https://github.com/anjasfedo/go-slack-age-bot
- Owner: Anjasfedo
- Created: 2024-01-15T11:46:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-15T12:44:49.000Z (over 2 years ago)
- Last Synced: 2025-09-02T13:05:45.689Z (10 months ago)
- Topics: golang, learning-by-doing, slack, slack-bot, slacker
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Slack Age Calculator 🎂
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.
## Usage 🚀
1. Create a `.env` file in the project root with the following format:
```env
SLACK_BOT_TOKEN=your_slack_bot_token
SLACK_APP_TOKEN=your_slack_app_token
```
2. run the program:
```
go run main.go
```
The bot will listen for commands in Slack and respond with the calculated age.
## Code Explanation 📜
### main Function 🚀
#### Load Environment Variables:
- Calls the `loadEnv` function to read environment variables from the `.env` file.
#### Create Slack API Client:
- Uses the Slack API token from the environment variables to create a new Slack API client.
#### Define Slack Bot Command:
- Defines a Slack bot command that calculates age based on the provided year of birth.
#### Listen for Slack Events:
- Sets up context for the bot and starts listening for Slack events.
### loadEnv Function 🌐
#### Open and Read .env File:
- Opens the `.env` file and reads it line by line.
#### Set Environment Variables:
- Parses key-value pairs from each line and sets them as environment variables.
### Handler Function 📊
#### Extract Year of Birth (yob):
- Extracts the "year" parameter from the command request.
#### Calculate Age:
- Converts the year parameter to an integer and calculates the age based on the current year.
#### Prepare Response:
- Prepares a response message with the calculated age.
#### Reply to Slack Channel:
- Replies to the Slack channel with the calculated age.
## Closing Notes 📝
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! 🚀👨💻