From 6c78ec5f09da4c1a8e5b466359d6c3bae4b5d383 Mon Sep 17 00:00:00 2001 From: samching Date: Fri, 2 Dec 2022 19:52:50 -0500 Subject: [PATCH 1/3] Fixes --- README.md | 10 ++++++++++ main.go | 11 +++++++++-- requirements.txt | 2 ++ server.py | 12 ++++++++---- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 requirements.txt diff --git a/README.md b/README.md index afea151..f6e44d9 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,13 @@ * You'll run a dedicated browser in another window that's controlling ChatGPT. * Two terminals: `go run main.go`, and `python server.py`. I am extremely doubtful they will work for you on the first run. * This marks the end of the readme file; it is a bit sparse; thankfully the code is too! Just tuck in if you can... and I will try to add more here later. + +## Get Started + +1. Install Requirements (suggest to do it in `virtualenv` or `conda`) + +`pip install -r requirements.txt` + +2. Launch Terminal 1: `go run main.go` + +3. Launch Terminal 2: `python server.py` \ No newline at end of file diff --git a/main.go b/main.go index 5165e10..00934a5 100644 --- a/main.go +++ b/main.go @@ -33,8 +33,12 @@ func (mycli *MyClient) register() { func (mycli *MyClient) eventHandler(evt interface{}) { switch v := evt.(type) { case *events.Message: - newMessage := v.Message - msg := newMessage.GetExtendedTextMessage().GetText() + + // Using personal whatsapp + msg:= v.Message.GetConversation() + // Uncomment using a Biz Account + //newMessage := v.Message + // msg := newMessage.GetExtendedTextMessage().GetText() fmt.Println("Message received:", msg) // Make a http request to localhost:5001/chat?q= with the message, and send the response // URL encode the message @@ -51,6 +55,9 @@ 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 received:", response) + fmt.Println("userJID:", v.Info.Sender.User) + fmt.Println("DefaultUserServer", types.DefaultUserServer) userJid := types.NewJID(v.Info.Sender.User, types.DefaultUserServer) mycli.WAClient.SendMessage(context.Background(), userJid, "", response) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..33baf03 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +playwright \ No newline at end of file diff --git a/server.py b/server.py index ac43917..ce64544 100644 --- a/server.py +++ b/server.py @@ -13,16 +13,20 @@ BROWSER = PLAY.chromium.launch_persistent_context( user_data_dir="/tmp/playwright", headless=False, + channel="chrome", ) PAGE = BROWSER.new_page() def get_input_box(): - """Get the child textarea of `PromptTextarea__TextareaWrapper`""" - return PAGE.query_selector("div[class*='PromptTextarea__TextareaWrapper']").query_selector("textarea") + """Get the child textarea of `PromptTextarea__TextareaWrapper`""" + 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 From 88ef86d56afcde3b4697cd8a04d9f7a92431098d Mon Sep 17 00:00:00 2001 From: samching Date: Fri, 2 Dec 2022 19:59:30 -0500 Subject: [PATCH 2/3] Revert changes to main.go to avoid merge confs --- main.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 00934a5..d6234e5 100644 --- a/main.go +++ b/main.go @@ -33,12 +33,8 @@ func (mycli *MyClient) register() { func (mycli *MyClient) eventHandler(evt interface{}) { switch v := evt.(type) { case *events.Message: - - // Using personal whatsapp - msg:= v.Message.GetConversation() - // Uncomment using a Biz Account - //newMessage := v.Message - // msg := newMessage.GetExtendedTextMessage().GetText() + newMessage := v.Message + msg := newMessage.GetExtendedTextMessage().GetText() fmt.Println("Message received:", msg) // Make a http request to localhost:5001/chat?q= with the message, and send the response // URL encode the message @@ -55,9 +51,6 @@ 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 received:", response) - fmt.Println("userJID:", v.Info.Sender.User) - fmt.Println("DefaultUserServer", types.DefaultUserServer) userJid := types.NewJID(v.Info.Sender.User, types.DefaultUserServer) mycli.WAClient.SendMessage(context.Background(), userJid, "", response) @@ -114,4 +107,4 @@ func main() { <-c client.Disconnect() -} +} \ No newline at end of file From 4a6ef036e426c13678c692ea64d744e7a75013e0 Mon Sep 17 00:00:00 2001 From: samching Date: Fri, 2 Dec 2022 20:01:53 -0500 Subject: [PATCH 3/3] Revert changes to server.py to avoid merge confs --- server.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/server.py b/server.py index ce64544..cd7c3eb 100644 --- a/server.py +++ b/server.py @@ -22,11 +22,8 @@ def get_input_box(): return PAGE.query_selector("div[class*='PromptTextarea__TextareaWrapper']").query_selector("textarea") def is_logged_in(): - try: - # See if we have a textarea with data-id="root" - return get_input_box() is not None - except AttributeError: - return False + # See if we have a textarea with data-id="root" + return get_input_box() is not None def send_message(message): # Send the message