0

Invite Members to Slack Channel

by
Published Jun 12, 2025

Invite members to an existing slack channel by email address

Script slack
  • Submitted by sfunkhouser260 Go
    Created 376 days ago
    1
    package inner
    2
    
    
    3
    import (
    4
    	"context"
    5
    
    
    6
    	"github.com/rs/zerolog/log"
    7
    	"github.com/slack-go/slack"
    8
    )
    9
    
    
    10
    // invite members to an existing channel by email
    11
    // provide a token, channel ID and emails of users to retreive and add to a channel
    12
    func main(token string, channelID string, emails []string) (interface{}, error) {
    13
    	api := slack.New(token)
    14
    
    
    15
      userIDs := []string{}
    16
    
    
    17
      for _, email := range emails {
    18
        user, err := api.GetUserByEmail(email)
    19
        if err != nil {
    20
          return nil, err
    21
        }
    22
      
    23
        userIDs = append(userIDs, user.ID)
    24
      }
    25
    
    
    26
      // nothing to do here, return early
    27
      if len(userIDs) == 0 {
    28
        log.Info().Msg("no users to add, skipping")
    29
        
    30
        return nil,  nil
    31
      }
    32
    
    
    33
      log.Info().Str("channelID", channelID).Strs("email", emails).Msg("inviting user to channel")
    34
        
    35
      _, err := api.InviteUsersToConversationContext(context.Background(), channelID, userIDs...)
    36
      if err != nil {
    37
        return nil, err
    38
      }
    39
    
    
    40
    	return nil, nil
    41
    }
    42