diff --git a/go.mod b/go.mod index 672c8a1..d67d2ba 100644 --- a/go.mod +++ b/go.mod @@ -2,15 +2,17 @@ module example.com/m go 1.19 +require ( + github.com/mattn/go-sqlite3 v1.14.16 + github.com/mdp/qrterminal/v3 v3.0.0 + go.mau.fi/whatsmeow v0.0.0-20221202110551-e067ee7293b0 + google.golang.org/protobuf v1.28.1 +) + require ( filippo.io/edwards25519 v1.0.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect - github.com/mattn/go-sqlite3 v1.14.16 // indirect - github.com/mdp/qrterminal v1.0.1 // indirect - github.com/mdp/qrterminal/v3 v3.0.0 // indirect go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf // indirect - go.mau.fi/whatsmeow v0.0.0-20221202110551-e067ee7293b0 // indirect golang.org/x/crypto v0.0.0-20221012134737-56aed061732a // indirect - google.golang.org/protobuf v1.28.1 // indirect rsc.io/qr v0.2.0 // indirect ) diff --git a/go.sum b/go.sum index ae3e166..003982b 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,7 @@ filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek= filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= @@ -8,7 +9,6 @@ github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= -github.com/mdp/qrterminal v1.0.1 h1:07+fzVDlPuBlXS8tB0ktTAyf+Lp1j2+2zK3fBOL5b7c= github.com/mdp/qrterminal v1.0.1/go.mod h1:Z33WhxQe9B6CdW37HaVqcRKzP+kByF3q/qLxOGe12xQ= github.com/mdp/qrterminal/v3 v3.0.0 h1:ywQqLRBXWTktytQNDKFjhAvoGkLVN3J2tAFZ0kMd9xQ= github.com/mdp/qrterminal/v3 v3.0.0/go.mod h1:NJpfAs7OAm77Dy8EkWrtE4aq+cE6McoLXlBqXQEwvE0= @@ -19,6 +19,7 @@ go.mau.fi/whatsmeow v0.0.0-20221202110551-e067ee7293b0/go.mod h1:2yweL8nczvtlIxk golang.org/x/crypto v0.0.0-20221012134737-56aed061732a h1:NmSIgad6KjE6VvHciPZuNRTKxGhlPfD6OA87W/PLkqg= golang.org/x/crypto v0.0.0-20221012134737-56aed061732a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= diff --git a/main.go b/main.go index 560b28e..d9064ce 100644 --- a/main.go +++ b/main.go @@ -21,16 +21,17 @@ import ( "google.golang.org/protobuf/proto" ) +// MyClient is a struct that holds the whatsmeow client and the eventHandlerID type MyClient struct { WAClient *whatsmeow.Client eventHandlerID uint32 } -func (mycli *MyClient) register() { - mycli.eventHandlerID = mycli.WAClient.AddEventHandler(mycli.eventHandler) +func (c *MyClient) register() { + c.eventHandlerID = c.WAClient.AddEventHandler(c.eventHandler) } -func (mycli *MyClient) eventHandler(evt interface{}) { +func (c *MyClient) eventHandler(evt interface{}) { switch v := evt.(type) { case *events.Message: newMessage := v.Message @@ -42,23 +43,31 @@ func (mycli *MyClient) eventHandler(evt interface{}) { // Make a http request to localhost:5001/chat?q= with the message, and send the response // URL encode the message urlEncoded := url.QueryEscape(msg) - url := "http://localhost:5001/chat?q=" + urlEncoded + u := "http://localhost:5001/chat?q=" + urlEncoded // Make the request - resp, err := http.Get(url) + resp, err := http.Get(u) if err != nil { fmt.Println("Error making request:", err) return } // Read the response buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) + _, err = buf.ReadFrom(resp.Body) + if err != nil { + fmt.Println("Error reading response:", err) + return + } newMsg := buf.String() // encode out as a string - response := &waProto.Message{Conversation: proto.String(string(newMsg))} + response := &waProto.Message{Conversation: proto.String(newMsg)} fmt.Println("Response:", response) userJid := types.NewJID(v.Info.Sender.User, types.DefaultUserServer) - mycli.WAClient.SendMessage(context.Background(), userJid, "", response) + _, err = c.WAClient.SendMessage(context.Background(), userJid, "", response) + if err != nil { + fmt.Println("Error sending message:", err) + return + } } }