Skip to content

Commit

Permalink
add: customize whatsapp client version
Browse files Browse the repository at this point in the history
  • Loading branch information
k1m0ch1 committed Aug 18, 2021
1 parent 375bdc4 commit fcf796b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ AWS_S3_DIR=
AWS_S3_ENDPOINT_URL=
AWS_STORAGE_BUCKET_NAME=
AWS_SECRET_ACCESS_KEY=
AWS_ACCESS_KEY_ID=
AWS_ACCESS_KEY_ID=
WHATSAPP_CLIENT_VERSION_MAJOR= (default 2)
WHATSAPP_CLIENT_VERSION_MINOR= (default 2123)
WHATSAPP_CLIENT_VERSION_HOTFIX= (default 7)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ k1m0ch1/nemo 08123123123
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)




## To Do


Expand Down
24 changes: 24 additions & 0 deletions docs/journal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Journal NeMo

## 18 August 2021

upgrade client version to

```
wac.SetClientVersion(2, 2123, 7)
```

- 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:

1. Startup
2. Validate the coral
2. Send Message
3. Read Message

- `log.Fatalf` will trigger the error and will be fail with a non-zero exit code
- so the main function in this app is using asynchronous, and we need the async/await to retry the connection if failed
- I need to research this again
- so I hold this feature
- 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
- 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
- in case the user using the old version of whatsapp, the env key must still exist
31 changes: 25 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
whatsapp "github.com/Rhymen/go-whatsapp"
cron "github.com/robfig/cron/v3"
godotenv "github.com/joho/godotenv"
// "github.com/davecgh/go-spew/spew"

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

func init() {
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
log.Fatal("No .env file found")
}
}

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

wac, err := whatsapp.NewConn(5 * time.Second)
// wac.SetClientVersion(0, 4, 2080)
wac.SetClientVersion(2, 2035, 14)
wac.SetClientVersion(Settings.WhatsAppClientVersionMajor, Settings.WhatsAppClientVersionMinor, Settings.WhatsAppClientVersionHotfix)
if err != nil {
log.Fatalf("error creating connection: %v\n", err)
}
log.Fatal("error creating connection: %v\n", err)
}

// isRetry := true
// for isRetry {
// retryAttempt := 0
// wac, err := whatsapp.NewConn(5 * time.Second)
// wac.SetClientVersion(2, 2123, 7)
// if err != nil {
// isRetry = true
// retryAttempt += 1
// if retryAttempt > Settings.MaxSessionRetries {
// log.Println("Reach Max Retries, Force close app")
// log.Fatal("error creating connection: %v\n", err)
// } else {
// log.Println("error creating connection: %v\n", err)
// log.Println("[%d] Retrying", retryAttempt)
// }
// }else{
// log.Println("Kaga Error")
// isRetry = false
// }
// }

handler := &waHandler{wac, uint64(time.Now().Unix()), make(map[string]struct{})}
wac.AddHandler(handler)
Expand Down
4 changes: 4 additions & 0 deletions utils/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type Setting struct {
AwsStorageBucketName string `json:"AWS_STORAGE_BUCKET_NAME"`
AwsSecretAccessKey string `json:"AWS_SECRET_ACCESS_KEY"`
AwsAccessKeyId string `json:"AWS_ACCESS_KEY_ID"`
MaxSessionRetries int `json:"MAX_SESSION_RETRIES"`
WhatsAppClientVersionMajor int `json:"WHATSAPP_CLIENT_VERSION_MAJOR"`
WhatsAppClientVersionMinor int `json:"WHATSAPP_CLIENT_VERSION_MINOR"`
WhatsAppClientVersionHotfix int `json:"WHATSAPP_CLIENT_VERSION_HOTFIX"`
}

type Commands struct {
Expand Down
8 changes: 6 additions & 2 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func LoadSetting() *Setting {
os.MkdirAll(pwd + "/coral", os.ModePerm)
}

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

Expand All @@ -49,6 +49,10 @@ func LoadSetting() *Setting {
AwsStorageBucketName: getEnvString("AWS_STORAGE_BUCKET_NAME", ""),
AwsSecretAccessKey: getEnvString("AWS_SECRET_ACCESS_KEY", ""),
AwsAccessKeyId: getEnvString("AWS_ACCESS_KEY_ID", ""),
MaxSessionRetries: getEnvInt("MAX_SESSION_RETRIES", 3),
WhatsAppClientVersionMajor: getEnvInt("WHATSAPP_CLIENT_VERSION_MAJOR", 2),
WhatsAppClientVersionMinor: getEnvInt("WHATSAPP_CLIENT_VERSION_MINOR", 2123),
WhatsAppClientVersionHotfix: getEnvInt("WHATSAPP_CLIENT_VERSION_HOTFIX", 7),
}
}

Expand Down Expand Up @@ -219,4 +223,4 @@ func getEnvInt(key string, defaultVal int) int {
return value
}
return defaultVal
}
}

0 comments on commit fcf796b

Please sign in to comment.