slack-sdk-go

module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2025 License: MIT

README ΒΆ

πŸš€ Slack SDK for Go

A lightweight and modular SDK for building Slack bots and apps in Go. With this SDK, you can easily send messages, manage users, and extend functionality to integrate with the Slack API seamlessly.


🌟 Features
  • πŸ“¨ Send messages to Slack channels
  • πŸ‘€ Fetch user information
  • ⚑️ Extendable for additional Slack APIs
  • 🧩 Lightweight and modular design
  • 🎯 Easy integration into Go projects

πŸ›  Getting Started
βœ… Prerequisites
  • Go 1.18 or later
  • A Slack Bot Token with the required permissions.

    Create a Slack App to generate your token.

πŸ“¦ Installation

Add the SDK to your project using:

go get github.com/pramithamj/slack-sdk-go/pkg/slack  

✨ Example Usage

Here’s a quick example to send a message using the SDK:

package main  

import (  
	"fmt"  
	"log"  

	"github.com/pramithamj/slack-sdk-go/pkg/slack"  
)  

func main() {  
	// Replace with your actual Slack Bot Token  
	token := "xoxb-your-slack-bot-token"  
	sdk := slack.NewSlackSDK(token)  

	channel := "C123456789" // Slack channel ID  
	message := "Hello, Slack from Go SDK!"  

	response, err := sdk.SendMessage(channel, message)  
	if err != nil {  
		log.Fatalf("Error sending message: %v", err)  
	}  

	fmt.Printf("Message sent successfully: %v\n", response)  
}  
πŸ“– API Methods

1️⃣ SendMessage

Send a message to a Slack channel.

πŸ“₯ Parameters: β€’ channel (string): The channel ID where the message will be sent. β€’ text (string): The message text.

πŸ“€ Example:

response, err := sdk.SendMessage("C123456789", "Hello, Slack!")  
if err != nil {  
	log.Fatalf("Error: %v", err)  
}  
fmt.Println(response)  

2️⃣ GetUserInfo

Fetch details about a Slack user.

πŸ“₯ Parameters: β€’ userID (string): The ID of the user.

πŸ“€ Example:

response, err := sdk.GetUserInfo("U123456789")  
if err != nil {  
	log.Fatalf("Error: %v", err)  
}  
fmt.Println(response)  
πŸ” Testing

Run all tests using:

go test ./tests/...

🀝 Contributing

We welcome contributions! To contribute: 1. Fork the repository 2. Create a new branch: git checkout -b feature-name 3. Commit your changes: git commit -m 'Add feature' 4. Push the branch: git push origin feature-name 5. Open a Pull Request

πŸ“œ License

This project is licensed under the MIT License. See the LICENSE file for more details.

πŸ‘€ Author

PramithaMJ β€’ πŸ’» [PramithaMJ]](https://github.com/pramithamj) β€’ πŸ“§ lpramithamj@gmail.com

Slack SDK for Go πŸš€

A powerful and flexible Go SDK for interacting with the Slack API, providing a simple yet comprehensive interface for sending messages and managing Slack communications.

Features ✨

  • πŸ“ Simple and rich message support
  • 🧡 Thread reply functionality
  • πŸ”„ Automatic retries with configurable attempts
  • ⏱️ Context support for timeouts and cancellation
  • 🎯 Custom error types and proper error handling
  • πŸ” Secure configuration via environment variables
  • πŸ“¦ Block kit and message attachments support

Installation πŸ“₯

go get github.com/pramithamj/slack-go-sdk

Quick Start πŸš€

package main

import (
    "context"
    "log"
    "os"
    "time"

    "github.com/pramithamj/slack-go-sdk/pkg/slack"
)

func main() {
    // Get token from environment
    token := os.Getenv("SLACK_BOT_TOKEN")
    if token == "" {
        log.Fatal("SLACK_BOT_TOKEN is required")
    }

    // Initialize SDK
    sdk := slack.NewSlackSDK(token)

    // Send a message
    ctx := context.Background()
    response, err := sdk.SendMessage(ctx, "CHANNEL_ID", "Hello from Go SDK!")
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("Message sent: %v", response)
}

Advanced Usage πŸ”§

Custom Configuration
sdk := slack.NewSlackSDKWithConfig(slack.Config{
    Token:         token,
    BaseURL:       "https://slack.com/api/",
    Timeout:       10 * time.Second,
    RetryAttempts: 2,
    RetryWaitTime: time.Second,
})
Rich Messages with Blocks
richMessage := slack.MessagePayload{
    Channel: channelID,
    Blocks: []slack.Block{
        {
            Type: "section",
            Text: &slack.TextObject{
                Type: "mrkdwn",
                Text: "*Hello!*\nThis is a rich message.",
            },
        },
    },
}

response, err := sdk.SendRichMessage(ctx, richMessage)
Thread Replies
response, err := sdk.SendThreadReply(ctx, channelID, threadTS, "This is a reply!")

Environment Variables πŸ”

  • SLACK_BOT_TOKEN: Your Slack bot token (required)
  • SLACK_CHANNEL_ID: Default channel ID for messages

Error Handling πŸ”

response, err := sdk.SendMessage(ctx, channelID, message)
if err != nil {
    if slackErr, ok := err.(*slack.SlackError); ok {
        log.Printf("Slack API error: %s", slackErr.Message)
    } else {
        log.Printf("Other error: %v", err)
    }
}

Contributing 🀝

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit PRs

Contact πŸ“«

License πŸ“„

This project is licensed under the MIT License - see the LICENSE file for details.


Last Updated: March 1, 2025

Directories ΒΆ

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL