From 4c082e7345f9c1cd5a1d0ebea524fc1b3981e243 Mon Sep 17 00:00:00 2001 From: louis-jackman <119705166+zlenner@users.noreply.github.com> Date: Fri, 2 Dec 2022 15:32:18 -0600 Subject: [PATCH] Fixes 3 issues 1. Detects when ChatGPT has finished replying instead of waiting 10 seconds 2. Gives an error message instead of crashing if user is logged out - fixes danielgross#1 3. Clearer logging for the WhatsApp Client (imo) --- .gitignore | 2 ++ main.go | 12 +++++++++--- server.py | 10 +++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba36a17 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +env/ +examplestore.db \ No newline at end of file diff --git a/main.go b/main.go index 5165e10..560b28e 100644 --- a/main.go +++ b/main.go @@ -34,13 +34,17 @@ func (mycli *MyClient) eventHandler(evt interface{}) { switch v := evt.(type) { case *events.Message: newMessage := v.Message - msg := newMessage.GetExtendedTextMessage().GetText() - fmt.Println("Message received:", msg) + msg := newMessage.GetConversation() + fmt.Println("Message from:", v.Info.Sender.User, "->", msg) + if msg == "" { + return + } // 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 // Make the request - resp, err := http.Get("http://localhost:5001/chat?q=" + urlEncoded) + resp, err := http.Get(url) if err != nil { fmt.Println("Error making request:", err) return @@ -51,6 +55,8 @@ func (mycli *MyClient) eventHandler(evt interface{}) { newMsg := buf.String() // encode out as a string response := &waProto.Message{Conversation: proto.String(string(newMsg))} + fmt.Println("Response:", response) + userJid := types.NewJID(v.Info.Sender.User, types.DefaultUserServer) mycli.WAClient.SendMessage(context.Background(), userJid, "", response) diff --git a/server.py b/server.py index ac43917..c277ef0 100644 --- a/server.py +++ b/server.py @@ -21,8 +21,11 @@ def get_input_box(): return PAGE.query_selector("div[class*='PromptTextarea__TextareaWrapper']").query_selector("textarea") def is_logged_in(): - # See if we have a textarea with data-id="root" - return get_input_box() is not None + try: + # See if we have a textarea with data-id="root" + return get_input_box() is not None + except AttributeError: + return False def send_message(message): # Send the message @@ -30,6 +33,8 @@ def send_message(message): box.click() box.fill(message) box.press("Enter") + while PAGE.query_selector(".result-streaming") is not None: + time.sleep(0.1) def get_last_message(): """Get the latest message""" @@ -42,7 +47,6 @@ def chat(): message = flask.request.args.get("q") print("Sending message: ", message) send_message(message) - time.sleep(10) # TODO: there are about ten million ways to be smarter than this response = get_last_message() print("Response: ", response) return response