Instagram
youtube
Facebook
  • 2 years ago
  • 1291 Views

Create your own Slack-Bot in Go

Yaman Birla
Table of Contents

Hello Tech geeks! Today we are going to build something unique and creative like in our last blog where we made a discord bot from scratch today we are also going to the kinda the same thing but this time we are going to make a slack bot from scratch in Golang.

 

What is Slack?

First thing first, what is slack, and what is it about? Well “Slack is a messaging app for businesses that connects people to the information that they need. By bringing people together to work as one unified team, Slack transforms the way that organizations communicate.”

 

Just like discord slack also have a bot which every workspace probably has in it.

 

Process

So, here we will use a really helpful GitHub package named slacker, built on top of slack API. Slacker is a framework for creating slack bots. We’ll cover the installation of it in our project directory later. 

 

Now, let’s understand how we will code the bot locally on our machine and integrate it with slack API into our workspace before proceeding further it is essential for you to have a slack account with you and create a workspace in it. 




 

Let us understand the next process with the following points:

  • Create a slack account.
  • Go to api.slack.com
  • Create an app.
  • Select from scratch.
  • Head over to the socket mode.
  • Enable socket mode.
  • Copy the token id and save it as an app token.
  • Head over to the event subscriptions option and enable it.
  • Add permissions in event subscriptions and save changes.



 

  • Go to OAuth & permissions and add the following bot scopes:

 

  • Install the bot to the workspace.
  • Copy the bot token and save it with you.
  • Code your bot.
  • Mention it to your channel/workspace.

 

Time to Code!

 

Well, now we have to code it and get it started. Open your favorite IDE or code editor and make sure to have a go workspace folder that contains your all project root directories.

 

So, open your cmd or PowerShell and make a directory for our bot project

 

 

Now, inside the slack-test folder add the go mod file

 

 

Now, get the slacker Github package

 

 

Now time to code!

 

Inside the main.go file and add the following code!

package main

import(
	"fmt"
	"os"
	"context"
	"log"
	"github.com/shomali11/slacker"
)

func printCommandEvents(analyticsChannel <-chan *slacker.CommandEvent){
	for event := range analyticsChannel{
		fmt.Println("Command Events")
		fmt.Println(event.Timestamp)
		fmt.Println(event.Command)
		fmt.Println(event.Parameters)
		fmt.Println(event.Event)
		fmt.Println()
	}
}

func main() {

	os.Setenv("SLACK_BOT_TOKEN", "xoxb-your-bot-token")
	os.Setenv("SLACK_APP_TOKEN", "xapp-your-app-token")


	bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))
	
	go printCommandEvents(bot.CommandEvents())
	
	bot.Command("ping", &slacker.CommandDefinition{
		Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
			response.Reply("pong")
		},
	})

	bot.Command("Hii", &slacker.CommandDefinition{
		Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
			response.Reply("Hello! How're you today")
		},
	})

	bot.Command("I'm fine", &slacker.CommandDefinition{
		Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
			response.Reply("Well! great to have a person like you in conversation")
		},
	})

	bot.Command("How're you?", &slacker.CommandDefinition{
		Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
			response.Reply("I'm great! my developers are quite interactive with me.")
		},
	})

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	err := bot.Listen(ctx)
	if err != nil {
		log.Fatal(err)
	}
}

Let’s understand the code

 

The libraries that we have imported are:

 

  • "fmt": fmt is a Go package that is used to format basic strings, values, inputs, and outputs.
  • "Os": Package os provides a platform-independent interface to operating system functionality.
  • "context": context is a standard package of Golang that makes it easy to pass request-scoped values, cancelation signals, and deadlines across API boundaries to all the goroutines involved in handling a request.
  • "log": A Logger represents an active logging object that generates lines of output to an io.Writer.

 

In addition to this, we have made a function named printCommandEvents that will print all the commands with their timestamp and parameter to the output.

 

And in the rest of the code, we have made specific commands given to our bot to respond to it. 

 

Let’s run this program:)

 

First, In PowerShell build this code to check for errors.



 

And run the code!

 

If the output is the same as it resembles in the image above and congrats your bot is up and running now, head over to your slack workspace and test the bot.

Boom! 

And you can also check your communications with the bot in the PowerShell

 

Well, this isn’t the end here you can add many features to your bot by exploring your app in api.slack,com 

 

Github: https://github.com/cosmic-weber/Golang/tree/main/slack-test

 

Thanks for sticking to the end!

 

Add a comment: