Closed rbadillo closed 3 years ago
What is the code you are using, it seems an error with your implementation.
This is my code:
package main
import (
"context"
"encoding/gob"
"encoding/json"
"fmt"
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
"github.com/Rhymen/go-whatsapp"
"github.com/go-co-op/gocron"
"github.com/go-redis/redis/v8"
"os"
"strings"
"time"
)
////////////////////////////////
// Whatsapp Functions
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)
os.Exit(1)
}
// HandleTextMessage receives whatsapp text messages and checks if the message was send by the current
// user, if it does not contain the keyword '@echo' or if it is from before the program start and then returns.
// Otherwise the message is echoed back to the original author.
func (wh *waHandler) HandleTextMessage(message whatsapp.TextMessage) {
.........
}
func login(wac *whatsapp.Conn) error {
session, err := readSession()
if err == nil {
session, err = wac.RestoreWithSession(session)
if err != nil {
return fmt.Errorf("restoring session failed: %v", err)
}
} else {
qr := make(chan string)
go func() {
terminal := qrcodeTerminal.New()
terminal.Get(<-qr).Print()
}()
session, err = wac.Login(qr)
if err != nil {
return fmt.Errorf("error during login: %v", err)
}
}
if err = writeSession(session); err != nil {
return fmt.Errorf("error saving session: %v", err)
}
return nil
}
func readSession() (whatsapp.Session, error) {
session := whatsapp.Session{}
homeDir, _ := os.UserHomeDir()
file, err := os.Open(homeDir + "/bot/" + "whatsappSession.gob")
if err != nil {
return session, err
}
defer file.Close()
decoder := gob.NewDecoder(file)
if err = decoder.Decode(&session); err != nil {
return session, err
}
return session, nil
}
func writeSession(session whatsapp.Session) error {
homeDir, _ := os.UserHomeDir()
file, err := os.Create(homeDir + "/bot/" + "whatsappSession.gob")
if err != nil {
return err
}
defer file.Close()
encoder := gob.NewEncoder(file)
if err = encoder.Encode(session); err != nil {
return err
}
return nil
}
////////////////////////////////
// MAIN
func main() {
fmt.Println("Trying to connect")
wac, err := whatsapp.NewConn(60 * time.Second)
fmt.Println("After NewConnect")
if err != nil {
fmt.Fprintf(os.Stderr, "error creating connection: %v\n", err)
return
}
fmt.Println("Adding Handler")
wac.AddHandler(&waHandler{wac, uint64(time.Now().Unix())})
fmt.Println("Done Adding Handler")
if err = login(wac); err != nil {
fmt.Fprintf(os.Stderr, "error logging in: %v\n", err)
return
}
fmt.Println("Waiting for Messages")
c := make(chan struct{})
<-c
}
Which code should I be using ? My bot has been running for 3 months no issues with the same code.
its still working smoothly ! without problem. 24 June 2021
Pada tanggal Rab, 23 Jun 2021 pukul 06.03 Roberto Badillo < @.***> menulis:
This is my code:
func main() {
fmt.Println("Trying to connect") wac, err := whatsapp.NewConn(60 * time.Second) fmt.Println("After NewConnect") if err != nil { fmt.Fprintf(os.Stderr, "error creating connection: %v\n", err) return }
fmt.Println("Adding Handler") wac.AddHandler(&waHandler{wac, uint64(time.Now().Unix())}) fmt.Println("Done Adding Handler")
if err = login(wac); err != nil { fmt.Fprintf(os.Stderr, "error logging in: %v\n", err) return }
fmt.Println("Waiting for Messages") c := make(chan struct{}) <-c }
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Rhymen/go-whatsapp/issues/582#issuecomment-866392517, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALJQY2AHE2BHZHH5VE55RH3TUEJDRANCNFSM47ETMCQQ .
The library continues to operate as of June 24, 2021.
In your HandleError
function do not exit. Consider that there may be types of messages not implemented by the library. Just ignore those kinds of messages and don't end your boot.
I was able to get it working by adding this line of code:
wac.SetClientVersion(2, 2123, 7)
Thank you guys for your help.
I was able to get it working by adding this line of code:
wac.SetClientVersion(2, 2123, 7)
Thank you guys for your help.
where to add the code?
@excaliburapp
wac, err := whatsapp.NewConn(60 * time.Second)
wac.SetClientVersion(2, 2123, 7)
I'm getting this error:
I had to bump the WAversion as per other comments I saw but is still not working. Can somebody help please?