This document provides a tutorial on how to develop a Slack bot using Golang. It discusses setting up a Slack workspace and creating a Slack app. It then covers installing Golang and the go-slack package to connect the bot to Slack. The tutorial demonstrates sending simple messages and handling events when the bot is mentioned. It includes code examples for connecting to Slack, posting messages, and responding to mention events.
3. Want to learn to build a slack bot using
Golang? Not sure where you can start? Here
we are to lessen your frustration and ease
your struggles. With the help of the go-slack
package, we will see how to set up a slack
workspace and connect the slack bot with
Golang.
In this tutorial, we will mainly focus on
setting up and creating a bot that can
interact with Slack channels and
workspace. The guide is mainly divided into
two sections:
4. Slack Set Up: Workspace setup and add
a bot app to it
Golang Set Up and Installation: Coding
part setup in which the bot app will be
sending requests to a Go backend via
Web Socket, termed Socket Mode in the
slack world.
Without further ado, let’s get started with
our tutorial: How to develop Slack Bot using
Golang.
7. Add required details, a team or company
name, a channel name, and invite other
teammates who can interact with the bot.
Want the best? Demand the best! Get the
best!
Contact Bacancy and hire Golang
developer from us today to meet your
product requirements. All you need is our
highly skilled Golang experts. Period.
9. Create a Slack application, visit the slack
website and select From scratch option.
Add an application name that will be
allowed by Workspace for further use.
Create a bot with the application.
10. Click on Bots; this will redirect to the Help
Page, select Add Scopes, and add
permissions to the application.
11. Click on Review Scopes to Add and add four
main scopes, which make the bot work with
the application.
Now install the application; if you’re not the
owner, then you have to request permission
from an Admin.
12. The next step is to select a channel the bot
can use to post on as an application.
13. Click Allow and get the OAuth token and
Webhook URL required for the
authentication process.
14. Invite the app to a channel. In my case, I
used a channel name slack-bot-golang.
Now, type a command message starting
with this /.; now we can invite the bot by
typing /invite @NameOfYourbot.
17. Create a new directory where we attach
all code further, set up communication
with the Slack channel, and write a code
with the authentication token.
We use the go-slack package that supports
the regular REST API, WebSockets, RTM,
and Events, and we use the godotenv
package to read environment variables.
19. First, create a .env file used to store slack
credentials, including channel ID. Find the
Token in the web UI where the application
is created; channel ID can be found in the
UI; go to Get channel details by clicking on
the dropdown arrow of the channel.
20. Let’s start coding. Create main.go. Start
with connecting to the workspace and post
a simple message to check if everything is
working.
Next, create a slack attachment, which
includes a message that we send to the
channel and add some fields to send extra
contextual data; it’s totally on us if we want
to add this data or not.
// main.go
package main
import (
"fmt"
"os"
"time"
"github.com/joho/godotenv"
"github.com/slack-go/slack"
)
25. Now, use slack events API and handle
events in the Slack channels. Our bot listen
to only mentioned events; if anyone
mentions the bot, it will receive a triggered
event. These events are delivered via
WebSocket.
First, we need to activate the section Socket
Mode; this allows the bot to connect via
WebSocket.
26. Now, add Event Subscriptions. Find it in the
Features tab, and toggle the button to
activate it. Then add the app_mention
scope to event subscriptions. This will
trigger mentioned new event in the
application.
27. The final thing is to generate an application
token. Currently, we have a bot token only,
but for events, we need an application
token.
Go to Settings->Basic Information and
scroll down to section App-Level Tokens
and click on Generate Tokens and Scope
and give a name for your Token.
28. On the app side, we need to add
connections:write scope to that token,
make sure to save the token by adding it to
the .env file as SLACK_APP_TOKEN.
To use Socket Mode, add a sub package of
slack-go called socketmode.
Next, create a new client for the socket
mode; with this, we have two clients, one
for regular API and one for WebSocket
events.
29. Now connect the WebSocket client by
calling socketmode.New and forward the
regular client as input and add
OptionAppLevelToken to the regular client
as is now required to connect to the Socket.
At last, we call socketClient.Run() , which
will block and process new WebSocket
messages on a channel at
socketClient.Events. We Put a for loop that
will continuously check for new events and
add a type switch to handle different
events. We attach a go-routine that will
handle incoming messages in the
background to listen to new events. And
with this, we trigger an event on the
EventAPI in Slack.
32. go func(ctx context.Context, client
*slack.Client, socketClient
*socketmode.Client) {
for {
select {
case <-ctx.Done():
log.Println("Shutting down
socketmode listener")
return
case event := <-socketClient.Events:
switch event.Type {
case
socketmode.EventTypeEventsAPI:
eventsAPI, ok := event.Data.
(slackevents.EventsAPIEvent)
if !ok {
log.Printf("Could not type cast
the event to the EventsAPI: %vn", event)
33. }
}
}
}(ctx, client, socketClient)
socketClient.Run()
}
To test, run the program and enter the Slack
app or web, and mention by bot by using
@yourbotname.
go run main.go
You should be able to see the event being
logged in the command line running the
bot. The event we get is of the type
event_callback, and that contains a payload
with the actual event that was performed.
34. Next, start to implement the
HandleEventMessage , which will continue
the type switching. We can use the type
field to know how to handle the Event. Then
we can reach the payload event by using the
InnerEvent field.
func HandleEventMessage(event
slackevents.EventsAPIEvent, client
*slack.Client) error {
switch event.Type {
case slackevents.CallbackEvent:
innerEvent := event.InnerEvent
switch evnt := innerEvent.Data.(type) {
err :=
HandleAppMentionEventToBot(evnt,
35. if err != nil {
return err
}
}
default:
return errors.New("unsupported event
type")
}
return nil
}
Replace the previous log in the main
function that prints the event with the new
HandleEventMessage function.
36. log.Println(eventsAPI)
replace with
err := HandleEventMessage(eventsAPI,
client)
if err != nil {
log.Fatal(err)
}
We need to make the bot respond to the
user who mentioned it.
Next start with logging into the application
and adding the users:read scope to the bot
token, as we did earlier. After adding the
scope to the token, we will create the
HandleAppMentionEventToBot
37. This function will take a
*slackevents.AppMentionEvent and a
slack.Client as input so it can respond.
The event contains the user ID in the
event.User with which we can fetch user
details. The channel response is also
available during the event.Channel. The
data we need is the actual message the user
sent while mentioning, which we can fetch
from the event.Text.
func
HandleAppMentionEventToBot(event
*slackevents.AppMentionEvent, client
*slack.Client) error {
user, err :=
client.GetUserInfo(event.User)
if err != nil {
return err
}
38. text := strings.ToLower(event.Text)
attachment := slack.Attachment{}
if strings.Contains(text, "hello") ||
strings.Contains(text, "hi") {
attachment.Text = fmt.Sprintf("Hello
%s", user.Name)
attachment.Color = "#4af030"
} else if strings.Contains(text, "weather")
{
attachment.Text =
fmt.Sprintf("Weather is sunny today. %s",
user.Name)
attachment.Color = "#4af030"
} else {
attachment.Text = fmt.Sprintf("I am
good. How are you %s?", user.Name)
attachment.Color = "#4af030"
39. }
_, _, err =
client.PostMessage(event.Channel,
slack.MsgOptionAttachments(attachment
))
if err != nil {
return fmt.Errorf("failed to post
message: %w", err)
}
return nil
}
Now restart the program and say hello or hi
and say something else to see whether it
works as expected. You have missed some
scope if you get a “missing_scope” error.
44. If you want to visit the source code, please
clone the repository and set up the
project on your system. You can try
playing around with the demo application
and explore more.
Here’s the github repository: slack-bot-
using-golang-example
45. Conclusion
I hope the purpose of this tutorial: How to
Develop Slack Bot using Golang is served
as expected. This is a basic step-by-step
guide to get you started with
implementing slack bot with the go-slack
package. Write us back if you have any
questions, suggestions, or feedback. Feel
free to clone the repository and play
around with the code.