You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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()
}
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?
The text was updated successfully, but these errors were encountered: