Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better documentation on testing socketmode client #1371

Open
dnck opened this issue Feb 5, 2025 · 1 comment
Open

Better documentation on testing socketmode client #1371

dnck opened this issue Feb 5, 2025 · 1 comment

Comments

@dnck
Copy link

dnck commented Feb 5, 2025

Description

I'm struggling to test my socketmode client. Ideally, I want to mock the slack server, and send different events to my connected client. I found the current docs on testserver, but didn't quite understand how to get it all wired up and I ended up writing my own mock websocket server, which still isn't working 100% but I got further than using the testserver. Is there any better documentation on testing a socketmode client? Alternatively, can one of the maintainers talk to me in this thread, and help me make a contribution to documenting how to do so for the community benefit?

@nlopes nlopes self-assigned this Mar 10, 2025
@nlopes nlopes removed their assignment Mar 10, 2025
@nlopes nlopes added enhancement SocketMode about SocketMode labels Mar 10, 2025
@nlopes
Copy link
Collaborator

nlopes commented Mar 22, 2025

Hi @dnck,

The best approach, I think, would be to do something like the following:

[...]
import (
	"net/http"
	"fmt"
	"os"
	"time"

	"github.com/slack-go/slack"
	"github.com/slack-go/slack/slackevents"
	"github.com/slack-go/slack/slacktest"
	"github.com/slack-go/slack/socketmode"
)

func appsConnectionOpenHandler(wsURL string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, `{"ok": true, "url":"`+wsURL+`"}`)
	}
}

func main() {
	s := slacktest.NewTestServer()
	go s.Start()

	s.Handle("/apps.connections.open", appsConnectionOpenHandler(s.GetWSURL()))

	api := slack.New("DOES NOT MATTER", slack.OptionAPIURL(s.GetAPIURL()))
	client := socketmode.New(api)

        // This is just an example! I'm basically waiting a few seconds and then sending a message to the websocket from the server side (think of it as Slack sending your client a message)
	go func() {
		time.Sleep(3 * time.Second)
		s.SendToWebsocket(`
{
  "payload": {
    "type": "message",
	"channel": "C12345",
	"user": "W012A3CDE",
	"text": "Hello, world!",
	"ts": "1742665072",
	"username": "Egon Spengler",
	"replace_original": false,
	"delete_original": false,
	"metadata": {
	  "event_type": "",
	  "event_payload": null
	},
	"blocks": null
  },
  "envelope_id": "1-2-3",
  "type": "events_api",
  "accepts_response_payload": false
}
`)
		time.Sleep(3 * time.Second)
	}()

        // This is the core of your client... mostly a copy&paste from what's in examples/socketmode/socketmode.go
	go func() {
		for evt := range client.Events {
			switch evt.Type {
			case socketmode.EventTypeConnecting:
				fmt.Println("Connecting to Slack with Socket Mode...")
			case socketmode.EventTypeConnectionError:
				fmt.Println("Connection failed. Retrying later...")
			case socketmode.EventTypeConnected:
				fmt.Println("Connected to Slack with Socket Mode.")
                         [...]
                 }
	}()

	client.Run()
}

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants