Skip to content

Commit ae8075f

Browse files
committed
clean: just clean up some codes
1 parent fcf796b commit ae8075f

12 files changed

+313
-290
lines changed

docs/journal.md

+14-8
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ upgrade client version to
88
wac.SetClientVersion(2, 2123, 7)
99
```
1010

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:
11+
- [`21:00`]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:
1212

1313
1. Startup
1414
2. Validate the coral
1515
2. Send Message
1616
3. Read Message
1717

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
18+
- [`21:30`] `log.Fatalf` will trigger the error and will be fail with a non-zero exit code
19+
- [`22:10`] so the main function in this app is using asynchronous, and we need the async/await to retry the connection if failed
20+
- [`22:30`] I need to research this again
21+
- [`22:30`] so I hold this feature
22+
- [`22:30`] 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+
- [`22:30`] 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+
- [`22:30`] in case the user using the old version of whatsapp, the env key must still exist
25+
- [`1:28`] I move all handler to `handlers` dir, I need to clean it up more detail
26+
27+
## 19 August 2021
28+
29+
- [`9:42`] clean up code and move some to utils
30+
- [`11:32`] now much cleaner than before

handlers/init.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package handlers
2+
3+
import (
4+
whatsapp "github.com/Rhymen/go-whatsapp"
5+
6+
// "github.com/eFishery/NeMo/utils"
7+
)
8+
9+
// var BuildGreetings []utils.BuildGreeting
10+
// var BuildCommands []utils.BuildCommand
11+
// var Settings *utils.Setting
12+
13+
type WaHandler struct {
14+
C *whatsapp.Conn
15+
StartTime uint64
16+
Chats map[string]struct{}
17+
}
18+
19+
// func init() {
20+
// Settings = utils.LoadSetting()
21+
// BuildGreetings = utils.ReadGreetingsFile()
22+
// BuildCommands = utils.ReadBuildCommandsFiles()
23+
// }

wa_default_handler.go handlers/wa_default_handler.go

+35-19
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
package main
1+
package handlers
22

33
import (
44
"fmt"
55
"os"
66
"log"
77
"strings"
8+
"time"
89

910
whatsapp "github.com/Rhymen/go-whatsapp"
1011
req "github.com/imroc/req"
1112

1213
"github.com/eFishery/NeMo/utils"
1314
)
1415

15-
1616
func currently_it_do_nothing(wac *whatsapp.Conn, RJID string) {
1717
phone_number := strings.Split(RJID, "@")[0]
1818

1919
// if the user suddenly sent the image this will trigger error because there is no available session
2020
// need to test this
21-
_, err := loadSession(phone_number)
21+
_, err := utils.LoadSession(phone_number)
2222
if err != nil {
2323
log.Println("I don't know what you do but it do nothing")
2424
return
2525
}
2626
}
2727

28-
func (wh *waHandler) HandleDocumentMessage(message whatsapp.DocumentMessage) {
29-
if !(message.Info.Timestamp < wh.startTime) {
30-
go currently_it_do_nothing(wh.c, message.Info.RemoteJid)
28+
func (wh *WaHandler) HandleDocumentMessage(message whatsapp.DocumentMessage) {
29+
if !(message.Info.Timestamp < wh.StartTime) {
30+
go currently_it_do_nothing(wh.C, message.Info.RemoteJid)
3131
}
3232
}
3333

34-
func (wh *waHandler) HandleVideoMessage(message whatsapp.VideoMessage) {
35-
if !(message.Info.Timestamp < wh.startTime) {
36-
go currently_it_do_nothing(wh.c, message.Info.RemoteJid)
34+
func (wh *WaHandler) HandleVideoMessage(message whatsapp.VideoMessage) {
35+
if !(message.Info.Timestamp < wh.StartTime) {
36+
go currently_it_do_nothing(wh.C, message.Info.RemoteJid)
3737
}
3838
}
3939

40-
func (wh *waHandler) HandleContactMessage(message whatsapp.ContactMessage) {
41-
if !(message.Info.Timestamp < wh.startTime) {
42-
go currently_it_do_nothing(wh.c, message.Info.RemoteJid)
40+
func (wh *WaHandler) HandleContactMessage(message whatsapp.ContactMessage) {
41+
if !(message.Info.Timestamp < wh.StartTime) {
42+
go currently_it_do_nothing(wh.C, message.Info.RemoteJid)
4343
}
4444
}
4545

4646
// need to test if the greeting is function well and return nothing after send message
4747
func greeting(wac *whatsapp.Conn, RJID string, message string){
48-
for gIndex := range(BuildGreetings) {
49-
for pIndex := range(BuildGreetings[gIndex].ExpectedUsers) {
50-
if(strings.Split(RJID, "@")[1] == "g.us" && BuildGreetings[gIndex].ExpectedUsers[pIndex] == "any"){
48+
for gIndex := range(utils.BuildGreetings) {
49+
for pIndex := range(utils.BuildGreetings[gIndex].ExpectedUsers) {
50+
if(strings.Split(RJID, "@")[1] == "g.us" && utils.BuildGreetings[gIndex].ExpectedUsers[pIndex] == "any"){
5151
fmt.Println("The any default message is enabled, and only accepted by direct message")
5252
return
5353
}
54-
if(BuildGreetings[gIndex].ExpectedUsers[pIndex] == RJID || BuildGreetings[gIndex].ExpectedUsers[pIndex] == "any"){
55-
url := BuildGreetings[gIndex].Webhook.URL
54+
if(utils.BuildGreetings[gIndex].ExpectedUsers[pIndex] == RJID || utils.BuildGreetings[gIndex].ExpectedUsers[pIndex] == "any"){
55+
url := utils.BuildGreetings[gIndex].Webhook.URL
5656

5757
logGreeting := utils.LogGreeting {
5858
Message: message,
5959
PhoneNumber: strings.Split(RJID, "@")[0],
6060
}
6161

62-
switch BuildGreetings[gIndex].Webhook.Service {
62+
switch utils.BuildGreetings[gIndex].Webhook.Service {
6363
case "DISCORD":
6464
_, LogErr := LogToDiscord(url, logGreeting)
6565
if LogErr != nil {
@@ -72,7 +72,7 @@ func greeting(wac *whatsapp.Conn, RJID string, message string){
7272
}
7373
}
7474

75-
go sendMessage(wac, BuildGreetings[gIndex].Message, RJID)
75+
go utils.SendMessage(wac, utils.BuildGreetings[gIndex].Message, RJID)
7676

7777
return
7878
}
@@ -121,4 +121,20 @@ func sendImage(wac *whatsapp.Conn, RJID string, imageUrl string, caption string)
121121
} else {
122122
fmt.Println("Message Sent -> ID : " + msgId)
123123
}
124+
}
125+
126+
//HandleError needs to be implemented to be a valid WhatsApp handler
127+
func (h *WaHandler) HandleError(err error) {
128+
if e, ok := err.(*whatsapp.ErrConnectionFailed); ok {
129+
log.Printf("Connection failed, underlying error: %v", e.Err)
130+
log.Println("Waiting 30sec...")
131+
<-time.After(30 * time.Second)
132+
log.Println("Reconnecting...")
133+
err := h.C.Restore()
134+
if err != nil {
135+
log.Fatalf("Restore failed: %v", err)
136+
}
137+
} else {
138+
log.Printf("Error occured: %v\n", err)
139+
}
124140
}

wa_image_handler.go handlers/wa_image_handler.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package handlers
22

33
import (
44
"encoding/json"
@@ -16,13 +16,13 @@ import (
1616
"github.com/eFishery/NeMo/utils"
1717
)
1818

19-
func (wh *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
20-
if !(message.Info.Timestamp < wh.startTime) {
19+
func (wh *WaHandler) HandleImageMessage(message whatsapp.ImageMessage) {
20+
if !(message.Info.Timestamp < wh.StartTime) {
2121

2222
phone_number := strings.Split(message.Info.RemoteJid, "@")[0]
2323

2424
// if the user suddenly sent the image this will trigger error because there is no available session
25-
Sessions, err := loadSession(phone_number)
25+
Sessions, err := utils.LoadSession(phone_number)
2626
if err != nil {
2727
log.Println(phone_number + " sent me image but it does nothing")
2828
return
@@ -41,15 +41,15 @@ func (wh *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
4141

4242
// prevent for user put image on any rule
4343
if coral.Process.Questions[sIndex].Question.Validation.Rule != "image" {
44-
go sendMessage(wh.c, coral.Process.Questions[sIndex].Question.Validation.Message, message.Info.RemoteJid)
44+
go utils.SendMessage(wh.C, coral.Process.Questions[sIndex].Question.Validation.Message, message.Info.RemoteJid)
4545
}
4646

4747
data, err := message.Download()
4848
if err != nil {
4949
if err != whatsapp.ErrMediaDownloadFailedWith410 && err != whatsapp.ErrMediaDownloadFailedWith404 {
5050
return
5151
}
52-
if _, err = wh.c.LoadMediaInfo(message.Info.RemoteJid, message.Info.Id, strconv.FormatBool(message.Info.FromMe)); err == nil {
52+
if _, err = wh.C.LoadMediaInfo(message.Info.RemoteJid, message.Info.Id, strconv.FormatBool(message.Info.FromMe)); err == nil {
5353
data, err = message.Download()
5454
if err != nil {
5555
return
@@ -68,7 +68,7 @@ func (wh *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
6868
}
6969
log.Printf("%v %v\n\timage received, saved at:%v\n", message.Info.Timestamp, message.Info.RemoteJid, filename)
7070

71-
uploadS3 := Settings.AddFileToS3(filename)
71+
uploadS3 := utils.Settings.AddFileToS3(filename)
7272

7373
log.Println("Files Uploaded and here is the link : " + uploadS3)
7474

@@ -87,7 +87,7 @@ func (wh *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
8787
_ = ioutil.WriteFile(utils.FileSession(phone_number), file, 0644)
8888

8989
if reply != "timeout" {
90-
go sendMessage(wh.c, reply, message.Info.RemoteJid)
90+
go utils.SendMessage(wh.C, reply, message.Info.RemoteJid)
9191
}
9292

9393
return
@@ -112,7 +112,7 @@ func (wh *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
112112

113113
Sessions.Datas = append(Sessions.Datas, dataBaru)
114114

115-
go saveSession(Sessions, phone_number)
115+
go utils.SaveSession(Sessions, phone_number)
116116

117117
if coral.Process.Log {
118118
logged := SentTo(coral.Log.Service, coral.Log.URL, Sessions)
@@ -132,14 +132,14 @@ func (wh *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
132132

133133
if reply != "timeout" {
134134
if Sessions.ProcessStatus != "WAIT_ANSWER" {
135-
go sendMessage(wh.c, reply, message.Info.RemoteJid)
135+
go utils.SendMessage(wh.C, reply, message.Info.RemoteJid)
136136

137137
if Sessions.ProcessStatus != "DONE" {
138138
log.Println(Sessions.ProcessStatus)
139139
Sessions.ProcessStatus = "WAIT_ANSWER"
140140
}
141141

142-
go saveSession(Sessions, phone_number)
142+
go utils.SaveSession(Sessions, phone_number)
143143
}
144144
}
145145
}

0 commit comments

Comments
 (0)