Build Your Slack App Home in Golang Using Socket Mode
By: Alexandre Couëdelo
Tags: Slack, Golang, Software Development, Programming, Web Development

Step 3: Create Controllers -> appHomeController.go

I created a sequence diagram (inspired by Tomomi Imura’s) to represent what we are about to code visually. I believe that it is convenient to keep such diagrams alongside my code. It should make it much easier to follow this tutorial. Besides, I added references to the diagram in my code as comments.

Created with PlantUML.

Handling events

First, in controllers/appHomeController.go , create a struct representing our Controller to handle dependency injection. So far, we only require socketmode.SocketmodeHandler to register the Slack events we want to handle. But in a more extensive application, you might have other dependencies such as repositories that handle database requests.

type AppHomeController struct {

EventHandler *socketmode.SocketmodeHandler

}

Second, create an initialization function for our Controller. This function is in charge of registering which event we want to receive and which function should handle it. If you refer to the sequence diagram, this Controller needs to listen to three events:

An Event API called app_home_opened (2).

(2). An interaction with the create button (12).

with the create button (12). An interaction with the modal submit button (22).

To register an event with EventHandler , you first need to identify what type of event we need to handle. At this point, you should get familiar with Slack’s terminology, including Event API, Interaction, and Block Action.

Each of those handler functions works the same way. You provide the type of event you expect (use autocompletion to find the one you need) and a callback function. The callback function requires:

A pointer to the event ( *socketmode.Event ). A pointer to the Socket Mode client ( *socketmode.Client ).

For instance, this is a valid function:

func callback(evt *socketmode.Event, clt *socketmode.Client) {}

A better alternative is to use a method that belongs to AppHomeController . That way, we benefit from the dependencies injected in AppHomeController :

func (c *AppHomeController) callback(evt *socketmode.Event, clt *socketmode.Client) {}

To conclude this section, here is my initialization constructor:

At this point, our Controller is ready. We only need to implement each of our three event-handling methods:

publishHomeTabView

openCreateStickieNoteModal

createStickieNote

Implementing publishHomeTabView

The goal of this function is to display the App Home tab:

All of our handlers have approximately the same structure:

Convert socketmode.Event into the desired type. socketmode.Event is the generic structure for all events, but we specified which event type we are expecting when registering this handler. Therefore, converting the struct is granted. Create the view. We have not implemented any view yet, so create placeholder functions for now. I prefer introducing how to make views all together in the last section. Send the view to Slack.

Implementing openCreateStickieNoteModal

This method opens a modal whenever a user clicks Add a Stickie .

This has the same structure as publishHomeTabView , except that we must acknowledge that we received the event. Otherwise, our users will see an error symbol on their side.

Implementing createStickieNote

The last part is updating the App Home when the user submits their information via the modal.

Once again, it’s the same structure. We acknowledge the event because it is an interaction. I find that extracting the values to create the sticky note is a bit tricky.

Slack requires a unique ID in the UI to identify Blocks and Actions. To minimize any errors, I created constants:

views.ModalDescriptionBlockID

views.ModalDescriptionActionID

views.ModalColorBlockID

views.ModalColorActionID