Skip to content

wongpinter/go-whatsapp

Repository files navigation

WhatsApp Go Library

Go Reference Go Report Card License: MIT

A comprehensive Go library for the WhatsApp Business Cloud API. Send messages, handle webhooks, manage templates, and build interactive Flows with a clean, type-safe interface.

✨ Features

  • 📱 Complete API Coverage - Messages, webhooks, business management, Flows
  • 🔒 Security Built-in - Webhook signature verification, secure token handling
  • 🏗️ Framework Agnostic - Works with Gin, Echo, standard HTTP, and more
  • 📝 Type Safe - Full type definitions with comprehensive error handling
  • 🧪 Testable - Interface-based design for easy mocking

🚀 Quick Start

Installation

go get github.com/wongpinter/go-whatsapp

Send a Message

package main

import (
    "context"
    "log"

    "github.com/wongpinter/go-whatsapp/cloudapi"
)

func main() {
    client := cloudapi.NewClient("PHONE_NUMBER_ID", "ACCESS_TOKEN")

    resp, err := client.SendText(context.Background(), "+1234567890", "Hello from Go!")
    if err != nil {
        log.Fatal(err)
    }

    log.Printf("Message sent: %s", resp.GetMessageID())
}

Handle Webhooks

package main

import (
    "context"
    "net/http"

    "github.com/wongpinter/go-whatsapp/webhook"
)

type Handler struct{}

func (h *Handler) OnTextMessage(ctx context.Context, msg *webhook.Message, metadata *webhook.Metadata) error {
    log.Printf("Received: %s from %s", msg.Text.Body, msg.From)
    return nil
}

func main() {
    handler := webhook.NewHandler("VERIFY_TOKEN", "APP_SECRET", nil)
    handler.SetMessageHandler(&Handler{})

    http.HandleFunc("/webhook", handler.ServeHTTP)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Build WhatsApp Flows

package main

import (
    "github.com/wongpinter/go-whatsapp/flows"
)

func main() {
    flow := flows.NewFlowBuilder().
        SetVersion("3.0").
        AddScreen("contact_form").
            SetTitle("Contact Information").
            AddTextInput("name", "Full Name").SetRequired(true).Done().
            AddEmailInput("email", "Email").SetRequired(true).Done().
            SetTerminal(true).
            Done().
        Build()

    flowJSON, _ := flow.ToJSON()
    log.Printf("Flow created: %s", flowJSON)
}

📦 Packages

Package Purpose Documentation
cloudapi Send messages (text, media, templates) API Reference
webhook Handle incoming events and messages Webhook Guide
flows Build and manage WhatsApp Flows Flows Guide
bm Business Management and templates BM Guide

🛠️ Configuration

Environment Variables

# Required
WHATSAPP_ACCESS_TOKEN=your_access_token
WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id

# For webhooks
WHATSAPP_VERIFY_TOKEN=your_verify_token
WHATSAPP_APP_SECRET=your_app_secret

Client Options

client := cloudapi.NewClient(phoneNumberID, accessToken,
    cloudapi.WithLogger(logger),
    cloudapi.WithRateLimiting(80.0),
    cloudapi.WithRetryConfig(3, time.Second),
)

📚 Documentation

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🔗 Links


Made with ❤️ for the Go community

About

A comprehensive Go library for the WhatsApp Business Cloud API. Send messages, handle webhooks, manage templates, and build interactive Flows with a clean, type-safe interface.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages