This guide will show you how to send a WhatsApp text message to your phone using the Infobip API Go SDK.
You can check the complete code example at the bottom of this guide.
What are WhastApp text messages?
You can only send text messages to your customer if they have contacted you first. This opens a 24-hour window for two-way communication. Otherwise, use a template message.
Difficulty level
This guide assumes basic knowledge of Go and APIs.
Prerequisites
- A running Go project
- An Infobip account
- A WhatsApp Business account (for testing purposes use the Infobip test sender)
Steps summary
Here’s a summary of the steps you will follow:
- Open a 24-hour window for text communication.
- Install the Infobip Go SDK.
- Import the required modules.
- Create an Infobip client.
- Configure a message payload.
- Send a message.
- Check a response (optional).
Steps
Go through the following steps to send a text message.
Open a 24-hour window for text communication
If you have a WhatsApp Business account, proceed to send a message with any content to your account. This will open the communication window.
If you don’t have a Whatsapp Business account, you can use the Infobip test sender. Please follow the “Activate Infobip Test Sender” instructions on the WhatsApp reference landing page.
Install the Infobip Go SDK
Get the Infobip Go SDK using the following command.
go get "github.com/infobip-community/infobip-api-go-sdk/v3" |
Import the required modules
The following imports are needed to make the code work.
import ( | |
"context" | |
"fmt" | |
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip" | |
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/models" | |
) |
The infobip
module contains the Infobip client that handles the configuration and API.
The models
module contains the data structures used to create validated request and response objects.
Create an Infobip client
Create an instance of the Infobip client with your credentials. You can find your credentials in the Infobip API Reference landing page or in the “Developers” tab of your account.
client, _ := infobip.NewClient( | |
"base-url", | |
"api-key", | |
) |
Configure a message payload
Create a WhatsApp message with text content.
message := models.WATextMsg{ | |
MsgCommon: models.MsgCommon{ | |
From: "111111111111", | |
To: "222222222222", | |
}, | |
Content: models.TextContent{Text: "Hello from the Infobip API Go SDK!"}, | |
} |
The request object supports many more fields! See the send WhatsApp text endpoint reference for more details.
Key points:
- Use the international number formatting when adding a number to the
to
field. For example, for British numbers the value will look like this:44754322576
. - For free trial accounts, the number used as a recipient must be the same number registered when signing up with Infobip.
- If you want to send multiple messages to multiple recipients in one request, use a WhatsApp template message.
Send a message
Send a message and save the response and details into variables.
msgResp, respDetails, _ := client.WhatsApp.SendText(context.Background(), message) |
Similarly to the request object, the response has many fields which you can explore in the response body schema in the send WhatsApp text endpoint reference.
Print the response (optional)
We are done! Print the response and response details if you want.
fmt.Printf("%+v\n", msgResp) | |
fmt.Printf("%+v\n", respDetails) |
The complete example
All of the above is condensed into the following code snippet.
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip" | |
"github.com/infobip-community/infobip-api-go-sdk/v3/pkg/infobip/models" | |
) | |
func main() { | |
client, _ := infobip.NewClient( | |
"base-url", | |
"api-key", | |
) | |
message := models.WATextMsg{ | |
MsgCommon: models.MsgCommon{ | |
From: "111111111111", | |
To: "222222222222", | |
}, | |
Content: models.TextContent{Text: "Hello from the Infobip API Go SDK!"}, | |
} | |
msgResp, respDetails, _ := client.WhatsApp.SendText(context.Background(), message) | |
fmt.Printf("%+v\n", msgResp) | |
fmt.Printf("%+v\n", respDetails) | |
} |
Thank you for reading!