Hey there, fellow Go enthusiast! Ready to dive into the world of WhatsApp API integration? You're in for a treat. We'll be using the awesome go-whatsapp
package to build a robust integration that'll have you sending and receiving messages like a pro. Let's get cracking!
Before we jump in, make sure you've got:
go-whatsapp
package (go get github.com/Rhymen/go-whatsapp
)Let's kick things off by creating a new Go project:
mkdir whatsapp-integration cd whatsapp-integration go mod init whatsapp-integration
Now, let's import the packages we'll need:
package main import ( "github.com/Rhymen/go-whatsapp" "fmt" "log" "os" "time" ) func main() { // We'll fill this in soon! }
Time to get connected! Here's how we initialize the WhatsApp connection:
wac, err := whatsapp.NewConn(5 * time.Second) if err != nil { log.Fatalf("error creating connection: %v\n", err) } qr := make(chan string) go func() { fmt.Printf("QR code: %v\n", <-qr) }() session, err := wac.Login(qr) if err != nil { log.Fatalf("error during login: %v\n", err) }
This code initializes the connection and handles QR code authentication. You'll need to scan the QR code with your WhatsApp app to authenticate.
Now for the fun part - sending messages!
msg := whatsapp.TextMessage{ Info: whatsapp.MessageInfo{ RemoteJid: "[email protected]", }, Text: "Hello from Go!", } _, err = wac.Send(msg) if err != nil { log.Fatalf("error sending message: %v\n", err) }
Want to send an image? No problem:
img, err := os.Open("cool_image.jpg") if err != nil { log.Fatalf("error opening image: %v\n", err) } _, err = wac.Send(whatsapp.ImageMessage{ Info: whatsapp.MessageInfo{ RemoteJid: "[email protected]", }, Type: "image/jpeg", Content: img, }) if err != nil { log.Fatalf("error sending image: %v\n", err) }
Let's set up an event handler to respond to incoming messages:
wac.AddHandler(&waHandler{}) type waHandler struct{} func (*waHandler) HandleError(err error) { fmt.Fprintf(os.Stderr, "error occured: %v\n", err) } func (*waHandler) HandleTextMessage(message whatsapp.TextMessage) { fmt.Printf("Message from %v: %v\n", message.Info.RemoteJid, message.Text) // Respond to the message here }
Always be prepared for the unexpected:
wac.AddHandler(&reconnectHandler{wac: wac}) type reconnectHandler struct { wac *whatsapp.Conn } func (h *reconnectHandler) HandleError(err error) { if e, ok := err.(*whatsapp.ErrConnectionFailed); ok { log.Printf("Connection failed, underlying error: %v\n", e.Err) log.Println("Waiting 30sec...") <-time.After(30 * time.Second) log.Println("Reconnecting...") err := h.wac.Restore() if err != nil { log.Fatalf("Restore failed: %v\n", err) } } else { log.Printf("error occured: %v\n", err) } }
Want to create a group? Here's how:
groupName := "Go Enthusiasts" participants := []string{"[email protected]", "[email protected]"} groupID, err := wac.CreateGroup(groupName, participants) if err != nil { log.Fatalf("error creating group: %v\n", err) } fmt.Printf("Created group: %v\n", groupID)
Remember to implement rate limiting to avoid getting blocked:
limiter := time.Tick(time.Second) for range limiter { // Send your message here }
Always test your code thoroughly before deploying. Here's a simple test:
func TestSendMessage(t *testing.T) { // Set up your test WhatsApp connection // Send a test message // Assert the message was sent successfully }
And there you have it! You've just built a WhatsApp API integration in Go. Pretty cool, right? Remember, this is just scratching the surface. There's so much more you can do with the go-whatsapp
package. Keep exploring, keep coding, and most importantly, have fun!
For more details, check out the go-whatsapp documentation. Now go forth and build something awesome!