Skip to content

Commit

Permalink
Fixes 3 issues
Browse files Browse the repository at this point in the history
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 #1

3. Clearer logging for the WhatsApp Client (imo)
  • Loading branch information
zlenner committed Dec 2, 2022
1 parent e3c3be0 commit 4c082e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env/
examplestore.db
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ 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
box = get_input_box()
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"""
Expand All @@ -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
Expand Down

0 comments on commit 4c082e7

Please sign in to comment.