This is a Go-Lang library that helps you send messages using pre-made templates on WhatsApp's business cloud platform.
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)
}
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)
}
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.
- Create a new template using the
template.New()
function, providing a name and language for the template.
tmpl := template.New("templateName", template.EN_US)
- Add a header component to the template using the
AddHeader()
method, specifying the header text as an argument.
tmpl = tmpl.AddHeader("Daniel")
- 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")
- Finalize the template construction by calling the
Done()
method. It returns the constructed data structure.
constructedTemplate := tmpl.Done()
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"
}
]
}
]