Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Outdated Client and Send Message Errors #93

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 79 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,79 @@
# whatsapp-gpt
* You'll need to run WhatsApp from a phone number using the golang library I'm using.
* 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.
* You can also try `multichat.py` if you want to watch two ChatGPTs talk to each other.
* 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.
# whatsapp-gpt-fix

## Introduction

This repository contains a forked and modified version of the original `whatsapp-gpt` project. The current version addresses two main issues I encountered while trying to run the project:

1. **Outdated Client Error** during WebSocket connection.
2. **Chatbot Integration:** Added a feature to interact with a local chatbot API.

---

## Issue 1: Outdated Client Error

When attempting to connect, the following error appeared:

```bash
10:09:20.723 [Client/Socket DEBUG] Dialing wss://web.whatsapp.com/ws/chat
10:09:20.975 [Client/Socket DEBUG] Frame websocket read pump starting 0x140001f24d0
10:09:23.120 [Client/Recv DEBUG] <failure location="cco" reason="405"/>
10:09:23.120 [Client ERROR] Client outdated (405) connect failure
10:09:23.121 [Client/QRChannel DEBUG] Closing channel with status {Event:err-client-outdated Error:<nil> Code: Timeout:0s}
Login event: err-client-outdated
10:09:23.122 [Client/Socket ERROR] Error reading from websocket: websocket: close 1006 (abnormal closure): unexpected EOF
```

### Solution

The issue is due to an outdated WhatsApp client. To fix this, upgrade your Go packages by running the following command in your terminal:

```bash
go get -u ./...
```

---

## Issue 2: Send Message Error

After addressing the outdated client error, I encountered a new issue related to sending messages using the latest version of the WhatsApp package. The code for sending a message was:

```bash
# command-line-arguments
./main.go:61:61: cannot use "" (untyped string constant) as *waE2E.Message value in argument to mycli.WAClient.SendMessage
./main.go:61:65: cannot use response (variable of type *waE2E.Message) as whatsmeow.SendRequestExtra value in argument to mycli.WAClient.SendMessage
```

This error is about this line in the code:
```go
mycli.WAClient.SendMessage(context.Background(), userJid, "", response)
```

### Solution

The newest version of the WhatsApp package introduced changes that required updating how messages are sent. The updated implementation now correctly handles sending messages and catching any errors that occur. This ensures that the bot can properly send responses back to users. So I just used the new syntax in the code and it worked:

```go
_, err = mycli.WAClient.SendMessage(context.Background(), userJid, response, whatsmeow.SendRequestExtra{})
if err != nil {
fmt.Println("Error sending message:", err)
}
```
---

## How to Run the Project

1. Clone the repository:
```bash
git clone <your-fork-url>
cd whatsapp-gpt-fix
```
2. Install dependencies and update packages:
```bash
go mod tidy
go get -u ./...
```
3. Ensure a chatbot API is running by running `python server.py` in your terminal.
4. Run the project:
```bash
go run main.go
```
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ func (mycli *MyClient) eventHandler(evt interface{}) {
fmt.Println("Response:", response)

userJid := types.NewJID(v.Info.Sender.User, types.DefaultUserServer)
mycli.WAClient.SendMessage(context.Background(), userJid, "", response)

//Updating the syntax in the message
_, err = mycli.WAClient.SendMessage(context.Background(), userJid, response, whatsmeow.SendRequestExtra{})
if err != nil {
fmt.Println("Error sending message:", err)
}

}
}
Expand Down