Skip to content

This is a Go-Lang Library that helps you send messages using pre-made templates on WhatsApp cloud API.

License

Notifications You must be signed in to change notification settings

IamNator/go-whatsapp

Repository files navigation

go-whatsapp

GoDoc Go Report Card Coverage Status [License]

This is a Go-Lang library that helps you send messages using pre-made templates on WhatsApp's business cloud platform.

Send message using pre-made templates


package main

import (
	"context"
	"fmt"

	whatsapp "github.com/IamNator/go-whatsapp/v3"
	"github.com/IamNator/go-whatsapp/v3/template"
)

func main() {


	//build the template
	tmpl := template.New("templateName", template.EN_US).
		AddHeader("header").
		AddBody("body").
		AddBody("body").
		AddBody("body").
		Done()

	recipient := "2349045057268"

	// create a new client
	client := whatsapp.New("phoneNumberID", "appAccessToken", whatsapp.WithApiVersion(whatsapp.V15))
	// send the request
	response, er := client.SendTemplate(context.Background(), recipient, tmpl)
	if er != nil {
		fmt.Println("error: ", er.Error())
		return
	}

	fmt.Println("Response: ", response)
}

Send raw text

package main

import (
	"context"
	"fmt"

	whatsapp "github.com/IamNator/go-whatsapp/v3"
)

func main() {
	client := whatsapp.New("phoneNumberID", "appAccessToken", whatsapp.WithApiVersion(whatsapp.V15))

	recipients := "2349045057268"
	text := "Hello World"

	// send the request
	response, er := client.SendText(context.Background(), recipients, text)
	if er != nil {
		fmt.Println("error: ", er.Error())
		return
	}

	// check the response for errors
	if response.Error != nil {
		fmt.Println("error: ", response.Error.Error())
		return
	}

	fmt.Println("Response: ", response)
}


Template Builder [example]

Whatsapp cloud api allows businesses to send messages to their customers using pre-defined templates, in order to use the template, one would have to send the request in a particular data structure.

The whatsapp/template package in this library simplify's the process of building/contructing that request payload.

Below is a step by step description of the example above.

  1. Create a new template using the template.New() function, providing a name and language for the template.
tmpl := template.New("templateName", template.EN_US)
  1. Add a header component to the template using the AddHeader() method, specifying the header text as an argument.
tmpl = tmpl.AddHeader("Daniel")
  1. Add body components to the template using the AddBody() method, providing the body texts as arguments.
tmpl = tmpl.AddBody("Daniel")
tmpl = tmpl.AddBody("4523")
tmpl = tmpl.AddBody("30")
  1. Finalize the template construction by calling the Done() method. It returns the constructed data structure.
constructedTemplate := tmpl.Done()

Data Structure.

The resulting data structure:

[
    {
        "type": "header",
        "parameters": [
            {
                "type": "text",
                "text": "Daniel"
            }
        ]
    },
    {
        "type": "body",
        "parameters": [
            {
                "type": "text",
                "text": "Daniel"
            },
            {
                "type": "text",
                "text": "4523"
            },
            {
                "type": "text",
                "text": "30"
            }
        ]
    }
]