GithubHelp home page GithubHelp logo

anjasfedo / go-slack-age-bot Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 6 KB

Golang program utilizing the slacker library to create a Slack bot that calculates user age.

Go 100.00%
golang learning-by-doing slack slack-bot slacker

go-slack-age-bot's Introduction

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:

     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! ๐Ÿš€๐Ÿ‘จโ€๐Ÿ’ป

go-slack-age-bot's People

Contributors

anjasfedo avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.