|
package main |
|
|
|
/* |
|
main.go go.mod go.sum |
|
|
|
[!] https://github.com/tulir/whatsmeow |
|
[!] https://github.com/tulir/whatsmeow/blob/main/client_test.go |
|
[!] https://github.com/danielgross/whatsapp-gpt |
|
[!] https://github.com/badboysm890/Whatsapp-GPT3-BOT |
|
|
|
wspReq.db |
|
|
|
$ echo '{ "BlackList": ["56900000001", "56900000004"] }' > wspReq.json |
|
*/ |
|
|
|
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 |
|
} |
|
|
|
// { "phoneNumbers": [], "HotWord": [], "author": "" } |
|
type Config struct { |
|
BlackList []string |
|
// 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("wspReq.json") |
|
if err != nil { |
|
fmt.Println("Error opening JSON file:", err) |
|
} |
|
defer file.Close() |
|
decoder := json.NewDecoder(file) |
|
config := Config{} |
|
err = decoder.Decode(&config) |
|
if err != nil { |
|
fmt.Println("Error decoding JSON file:", err) |
|
} |
|
return config |
|
} |
|
|
|
func stringInArray(str string, arr []string) bool { |
|
for _, element := range arr { |
|
if element == str { |
|
return true |
|
} |
|
} |
|
return false |
|
} |
|
|
|
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 |
|
} |
|
|
|
//newMessage := v.Message |
|
//msg := newMessage.GetConversation() |
|
|
|
// 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() |
|
} |
|
|
|
// 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_raw.GetText()) |
|
|
|
if msg == "" { |
|
//fmt.Println("TEST - msg - return") |
|
//msg = msg_raw.GetText() |
|
return |
|
} |
|
|
|
cfgData := readConfig() |
|
|
|
denyPhoneNumbers := cfgData.BlackList |
|
if stringInArray(v.Info.Sender.User, denyPhoneNumbers) { |
|
//fmt.Println("TEST - found - return") |
|
return |
|
} |
|
|
|
/* |
|
phoneNumbers := cfgData.PhoneNumbers |
|
if !contains(phoneNumbers, v.Info.Sender.User) { |
|
//fmt.Println("TEST - contains PhoneNumbers - return") |
|
return |
|
} |
|
if !contains(cfgData.HotWords, msg) { |
|
//fmt.Println("TEST - contains HotWords - return") |
|
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) |
|
// localhost:5001/chat?q={MSG} |
|
//url := "http://localhost:5001/chat?q=" + urlEncoded |
|
urlEncodedUser := url.QueryEscape(v.Info.Sender.User) |
|
// localhost:5001/chat?f={FROM}&q={MSG} |
|
url := "http://localhost:5001/chat?f=" + urlEncodedUser + "&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) |
|
respp, err := mycli.WAClient.SendMessage(context.Background(), userJid, "", response) |
|
if err != nil { |
|
fmt.Println("Error sending message: %v", err) |
|
} else { |
|
fmt.Println("Message sent (server timestamp: %s)", respp.Timestamp) |
|
} |
|
} |
|
} |
|
|
|
/* |
|
func eventHandler(evt interface{}) { |
|
switch v := evt.(type) { |
|
case *events.Message: |
|
fmt.Println("Received a message!", v.Message.GetConversation()) |
|
} |
|
} |
|
*/ |
|
|
|
func main() { |
|
dbLog := waLog.Stdout("Database", "INFO", true) // DEBUG INFO |
|
// Make sure you add appropriate DB connector imports, e.g. github.com/mattn/go-sqlite3 for SQLite |
|
container, err := sqlstore.New("sqlite3", "file:wspReq.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", "INFO", true) // DEBUG INFO |
|
client := whatsmeow.NewClient(deviceStore, clientLog) |
|
//client.AddEventHandler(eventHandler) |
|
// 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() |
|
} |