I'm building out a simple Slackbot as a learning experience with Go, and I've hit my first snag: I'm unable to write a message back to the connection!
Here's my main func:
func main() {
conn, botId, err := slackInit(os.Getenv("SLACKBOT_TOKEN"))
if err != nil { return }
defer conn.Close()
for {
_, event, err := conn.ReadMessage()
if err != nil {
fmt.Println("Error processing message:", err)
return
}
fmt.Println(string(event))
message, err := slackGetMessage(event)
if strings.Contains(message, botId) {
fmt.Println("Bot was mentioned!")
resp := []byte("You talkin' to me?")
err = conn.WriteMessage(1, resp)
if err != nil {
fmt.Println("Error writing message:", string(resp))
return
}
}
}
}
This listens and reports events with no issue, but fails as soon as the conn.WriteMessage
method is called. I get a fatal error from websocket
with code 1006 & the message "unexpected closure".
I'm using Gorilla for my websocket library, and I suspect maybe this is a concurrency issue related to their "one reader, one writer" limit. I've tried a few tweaks, but honestly just don't know enough about the languge/library yet to really dive deep on debugging this. :-/
I'm sure I'm missing something obvious here! Any tips for managing my Writer to get my bot talking back? Thanks!
The message sent to the server is not JSON as expected by the server. The server closed the connection without a handshake.