Skip to content

Commit fcf796b

Browse files
committed
add: customize whatsapp client version
1 parent 375bdc4 commit fcf796b

File tree

6 files changed

+65
-9
lines changed

6 files changed

+65
-9
lines changed

.env.example

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ AWS_S3_DIR=
99
AWS_S3_ENDPOINT_URL=
1010
AWS_STORAGE_BUCKET_NAME=
1111
AWS_SECRET_ACCESS_KEY=
12-
AWS_ACCESS_KEY_ID=
12+
AWS_ACCESS_KEY_ID=
13+
WHATSAPP_CLIENT_VERSION_MAJOR= (default 2)
14+
WHATSAPP_CLIENT_VERSION_MINOR= (default 2123)
15+
WHATSAPP_CLIENT_VERSION_HOTFIX= (default 7)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ k1m0ch1/nemo 08123123123
7575
The configuration is called coral, you can learn about the coral configuration in here [Understanding Coral Configuration](https://nemo-efishery.readthedocs.io/en/latest/#understanding-basic-coral-configuration)
7676

7777

78+
79+
7880
## To Do
7981

8082

docs/journal.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Journal NeMo
2+
3+
## 18 August 2021
4+
5+
upgrade client version to
6+
7+
```
8+
wac.SetClientVersion(2, 2123, 7)
9+
```
10+
11+
- problem with stability of NeMO, whenever it comes to fault or error, it will force close, it will be much better if I add the restart NeMo in a specific feature:
12+
13+
1. Startup
14+
2. Validate the coral
15+
2. Send Message
16+
3. Read Message
17+
18+
- `log.Fatalf` will trigger the error and will be fail with a non-zero exit code
19+
- so the main function in this app is using asynchronous, and we need the async/await to retry the connection if failed
20+
- I need to research this again
21+
- so I hold this feature
22+
- and then I add the env key `WHATSAPP_CLIENT_VERSION_MAJOR` `WHATSAPP_CLIENT_VERSION_MINOR` and `WHATSAPP_CLIENT_VERSION_HOTFIX` so user can customize the whatsapp client version
23+
- the best way to do is by get the client version on device and set this automatically so we don't need to do this
24+
- in case the user using the old version of whatsapp, the env key must still exist

main.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
whatsapp "github.com/Rhymen/go-whatsapp"
1616
cron "github.com/robfig/cron/v3"
1717
godotenv "github.com/joho/godotenv"
18-
// "github.com/davecgh/go-spew/spew"
1918

2019
"github.com/eFishery/NeMo/utils"
2120
)
@@ -29,7 +28,7 @@ type waHandler struct {
2928

3029
func init() {
3130
if err := godotenv.Load(); err != nil {
32-
log.Print("No .env file found")
31+
log.Fatal("No .env file found")
3332
}
3433
}
3534

@@ -54,11 +53,31 @@ func main() {
5453
BuildCommands = utils.ReadBuildCommandsFiles()
5554

5655
wac, err := whatsapp.NewConn(5 * time.Second)
57-
// wac.SetClientVersion(0, 4, 2080)
58-
wac.SetClientVersion(2, 2035, 14)
56+
wac.SetClientVersion(Settings.WhatsAppClientVersionMajor, Settings.WhatsAppClientVersionMinor, Settings.WhatsAppClientVersionHotfix)
5957
if err != nil {
60-
log.Fatalf("error creating connection: %v\n", err)
61-
}
58+
log.Fatal("error creating connection: %v\n", err)
59+
}
60+
61+
// isRetry := true
62+
// for isRetry {
63+
// retryAttempt := 0
64+
// wac, err := whatsapp.NewConn(5 * time.Second)
65+
// wac.SetClientVersion(2, 2123, 7)
66+
// if err != nil {
67+
// isRetry = true
68+
// retryAttempt += 1
69+
// if retryAttempt > Settings.MaxSessionRetries {
70+
// log.Println("Reach Max Retries, Force close app")
71+
// log.Fatal("error creating connection: %v\n", err)
72+
// } else {
73+
// log.Println("error creating connection: %v\n", err)
74+
// log.Println("[%d] Retrying", retryAttempt)
75+
// }
76+
// }else{
77+
// log.Println("Kaga Error")
78+
// isRetry = false
79+
// }
80+
// }
6281

6382
handler := &waHandler{wac, uint64(time.Now().Unix()), make(map[string]struct{})}
6483
wac.AddHandler(handler)

utils/struct.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ type Setting struct {
1515
AwsStorageBucketName string `json:"AWS_STORAGE_BUCKET_NAME"`
1616
AwsSecretAccessKey string `json:"AWS_SECRET_ACCESS_KEY"`
1717
AwsAccessKeyId string `json:"AWS_ACCESS_KEY_ID"`
18+
MaxSessionRetries int `json:"MAX_SESSION_RETRIES"`
19+
WhatsAppClientVersionMajor int `json:"WHATSAPP_CLIENT_VERSION_MAJOR"`
20+
WhatsAppClientVersionMinor int `json:"WHATSAPP_CLIENT_VERSION_MINOR"`
21+
WhatsAppClientVersionHotfix int `json:"WHATSAPP_CLIENT_VERSION_HOTFIX"`
1822
}
1923

2024
type Commands struct {

utils/util.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func LoadSetting() *Setting {
3131
os.MkdirAll(pwd + "/coral", os.ModePerm)
3232
}
3333

34-
if _, err := os.Stat(pwd + "/.build/sessions/image"); os.IsNotExist(err) {
34+
if _, err := os.Stat(pwd + "/.build/sessions"); os.IsNotExist(err) {
3535
os.MkdirAll(pwd + "/.build/sessions", os.ModePerm)
3636
}
3737

@@ -49,6 +49,10 @@ func LoadSetting() *Setting {
4949
AwsStorageBucketName: getEnvString("AWS_STORAGE_BUCKET_NAME", ""),
5050
AwsSecretAccessKey: getEnvString("AWS_SECRET_ACCESS_KEY", ""),
5151
AwsAccessKeyId: getEnvString("AWS_ACCESS_KEY_ID", ""),
52+
MaxSessionRetries: getEnvInt("MAX_SESSION_RETRIES", 3),
53+
WhatsAppClientVersionMajor: getEnvInt("WHATSAPP_CLIENT_VERSION_MAJOR", 2),
54+
WhatsAppClientVersionMinor: getEnvInt("WHATSAPP_CLIENT_VERSION_MINOR", 2123),
55+
WhatsAppClientVersionHotfix: getEnvInt("WHATSAPP_CLIENT_VERSION_HOTFIX", 7),
5256
}
5357
}
5458

@@ -219,4 +223,4 @@ func getEnvInt(key string, defaultVal int) int {
219223
return value
220224
}
221225
return defaultVal
222-
}
226+
}

0 commit comments

Comments
 (0)