This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waHandler.go
204 lines (169 loc) Β· 3.83 KB
/
waHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"WhatsAppToTelegram/utils"
"errors"
"fmt"
"github.com/Rhymen/go-whatsapp"
tg "gopkg.in/tucnak/telebot.v2"
"os"
"path"
"strings"
)
type waHandler struct {
wac *whatsapp.Conn
startTime uint64
}
func (wh *waHandler) HandleError(err error) {
fmt.Fprintf(os.Stderr, "error caught in handler: %v\n", err)
var errHandle error
// Refer: https://github.com/Rhymen/go-whatsapp/issues/476
if strings.Contains(err.Error(), "code: 1000") {
errHandle = waConn.Restore()
}
//
if strings.Contains(err.Error(), "read: connection reset by peer") {
errHandle = errors.New("a new device used the web, so log out")
}
if errHandle != nil {
fmt.Fprintf(os.Stderr, "error cannot be solved: %v\n", err)
sendTelegramTxt(fmt.Sprintf("WhatsApp client is offline: %v", err))
}
}
func (wh *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
if message.Info.FromMe || message.Info.Timestamp < wh.startTime {
return
}
jid := getJid(message.Info)
isGroup := strings.Contains(jid, "-")
if !utils.Exists("cache") {
os.Mkdir("cache", os.ModePerm)
}
imgPath := path.Join("cache", utils.RandStringBytes(10)+".jpg")
for {
if !utils.Exists(imgPath) {
break
}
imgPath = path.Join("cache", utils.RandStringBytes(10)+".jpg")
}
img, err := message.Download()
if err != nil {
if !isGroup {
sendWhatsAppTxtMsg(
wh,
message.Info.RemoteJid,
"Message transferred failed. More info please text .help\n"+
"Err: WA_R_IMG_DL",
)
}
_ = sendTelegramTxt(fmt.Sprintf(
"JID: %s\n"+
"MSG: IMG\n"+
"ERR: Image download failed!", jid))
return
}
err = utils.SavePic(img, imgPath)
defer os.Remove(imgPath)
if err != nil {
if !isGroup {
sendWhatsAppTxtMsg(
wh,
message.Info.RemoteJid,
"Message transferred failed. More info please text .help\n"+
"Err: WA_R_IMG_SV",
)
}
_ = sendTelegramTxt(fmt.Sprintf(
"JID: %s\n"+
"MSG: IMG\n"+
"ERR: Image save failed!", jid))
return
}
_, err = tgBot.Send(
tg.ChatID(tgChatId),
&tg.Photo{
File: tg.FromDisk(imgPath),
Caption: "JID: " + jid,
},
)
if err != nil {
if !isGroup {
sendWhatsAppTxtMsg(
wh,
message.Info.RemoteJid,
"Message transferred failed. More info please text .help\n"+
"Err: TG_S_IMG",
)
}
_ = sendTelegramTxt(fmt.Sprintf(
"JID: %s\n"+
"MSG: IMG\n"+
"ERR: Image sent failed!", jid))
fmt.Println(err)
return
}
if !isGroup {
if !skipNotify(message.Info.RemoteJid) {
sendWhatsAppTxtMsg(
wh,
message.Info.RemoteJid,
"Message transferred successfully. More info please text .help",
)
}
}
}
func (wh *waHandler) HandleTextMessage(message whatsapp.TextMessage) {
if message.Info.FromMe || message.Info.Timestamp < wh.startTime {
return
}
jid := getJid(message.Info)
msgStr := fmt.Sprintf(
"JID: %s\nMsg: %s\n",
jid,
message.Text,
)
fmt.Fprintf(os.Stdout, msgStr)
if strings.Contains(jid, "-") {
return
}
transferState := true
errS := sendTelegramTxt(msgStr)
if errS != nil {
fmt.Fprintf(os.Stderr, "Cannot send to TG")
transferState = false
}
// Func
if strings.HasPrefix(message.Text, ".") {
msg := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: message.Info.RemoteJid,
},
Text: getResponse(message.Text, message.Info.RemoteJid),
}
if _, err := wh.wac.Send(msg); err != nil {
fmt.Fprintf(os.Stderr, "error sending message: %v\n", err)
}
return
}
if transferState && skipNotify(message.Info.RemoteJid) {
return
}
transferStateMsg := "successfully"
if !transferState {
transferStateMsg = "failed"
}
sendWhatsAppTxtMsg(
wh,
message.Info.RemoteJid,
fmt.Sprintf(
"Message transferred %s. More info please text .help",
transferStateMsg,
),
)
}
func skipNotify(id string) bool {
v, exist := skipNotifyMap[id]
if !exist {
return false
}
return v
}