-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update.go
129 lines (119 loc) · 3.61 KB
/
Update.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
package main
import (
"bytes"
"context"
"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
}
func (mycli *MyClient) register() {
mycli.eventHandlerID = mycli.WAClient.AddEventHandler(mycli.eventHandler)
}
func (mycli *MyClient) eventHandler(evt interface{}) {
switch v := evt.(type) {
case *events.Message:
// Ignore group messages
// Group messages cause a panic, so we need to check if it's a group message
if v.Info.IsGroup {
return
}
// Support quoted messages
// Whenever someone replies to your message by swiping left on it
var newMessage *waProto.Message
newMessage = v.Message
quoted := newMessage.ExtendedTextMessage
var msg string
if quoted == nil {
msg = newMessage.GetConversation()
} else {
msg = quoted.GetText()
}
fmt.Println("Message from:", v.Info.Sender.User, "->", msg)
if msg == "" {
return
}
// 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:5001/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()
}