-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
178 lines (155 loc) · 4.37 KB
/
main.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
_ "github.com/mattn/go-sqlite3"
"github.com/mdp/qrterminal/v3"
"go.mau.fi/whatsmeow"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
)
type MyClient struct {
WAClient *whatsmeow.Client
eventHandlerID uint32
}
type Config struct {
PhoneNumbers []string
HotWords []string
Author string
}
func (mycli *MyClient) register() {
mycli.eventHandlerID = mycli.WAClient.AddEventHandler(mycli.eventHandler)
}
// contains checks if a string is in an array of strings
func contains(arr []string, str string) bool {
// if the array is empty, return true
if len(arr) == 0 {
return true
}
for _, a := range arr {
if a == str {
return true
}
}
return false
}
func readConfig() Config {
// Read the config file
file, err := os.Open("config.json")
if err != nil {
fmt.Println("Error opening config file:", err)
}
defer file.Close()
decoder := json.NewDecoder(file)
config := Config{}
err = decoder.Decode(&config)
if err != nil {
fmt.Println("Error decoding config file:", err)
}
return config
}
func (mycli *MyClient) eventHandler(evt interface{}) {
switch v := evt.(type) {
case *events.Message:
newMessage := v.Message
msg := newMessage.GetConversation()
// some messages are empty, so we need to check for that
msg_raw := newMessage.GetExtendedTextMessage()
fmt.Println("Message from - Raw:", v.Info.Sender.User, "->", msg_raw)
fmt.Println("Message from - Conv:", v.Info.Sender.User, "->", msg)
fmt.Println("---------------------->", msg)
if msg == "" {
msg = msg_raw.GetText()
}
cfgData := readConfig()
phoneNumbers := cfgData.PhoneNumbers
if !contains(phoneNumbers, v.Info.Sender.User) {
return
}
// if !contains(cfgData.HotWords, msg) {
// return
// }
// remove the hotwords from the message
// for _, hotword := range cfgData.HotWords {
// msg = msg[len(hotword):]
// }
// Make a http request to localhost:5001/chat?q= with the message, and send the response
// URL encode the message
urlEncoded := url.QueryEscape(msg)
url := "http://localhost:5002/chat?q=" + urlEncoded
// Make the request
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error making request:", err)
return
}
// Read the response
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
newMsg := buf.String()
// encode out as a string
response := &waProto.Message{Conversation: proto.String(string(newMsg))}
fmt.Println("Response:", response)
userJid := types.NewJID(v.Info.Sender.User, types.DefaultUserServer)
mycli.WAClient.SendMessage(context.Background(), userJid, "", response)
}
}
func main() {
dbLog := waLog.Stdout("Database", "DEBUG", true)
// Make sure you add appropriate DB connector imports, e.g. github.com/mattn/go-sqlite3 for SQLite
container, err := sqlstore.New("sqlite3", "file:examplestore.db?_foreign_keys=on", dbLog)
if err != nil {
panic(err)
}
// If you want multiple sessions, remember their JIDs and use .GetDevice(jid) or .GetAllDevices() instead.
deviceStore, err := container.GetFirstDevice()
if err != nil {
panic(err)
}
clientLog := waLog.Stdout("Client", "DEBUG", true)
client := whatsmeow.NewClient(deviceStore, clientLog)
// add the eventHandler
mycli := &MyClient{WAClient: client}
mycli.register()
if client.Store.ID == nil {
// No ID stored, new login
qrChan, _ := client.GetQRChannel(context.Background())
err = client.Connect()
if err != nil {
panic(err)
}
for evt := range qrChan {
if evt.Event == "code" {
// Render the QR code here
// e.g. qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
// or just manually `echo 2@... | qrencode -t ansiutf8` in a terminal
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
// fmt.Println("QR code:", evt.Code)
} else {
fmt.Println("Login event:", evt.Event)
}
}
} else {
// Already logged in, just connect
err = client.Connect()
if err != nil {
panic(err)
}
}
// Listen to Ctrl+C (you can also do something else that prevents the program from exiting)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
client.Disconnect()
}