Receive WhatsApp Text Messages Using Go

Overview

This guide covers how to receive WhatsApp text messages when a user sends it to your registered phone number.

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a Go development environment.

To receive incoming messages, you must have an onboarded WhatsApp account with inbound capable numbers registered against the same. If the number is in connected state, it can receive incoming messages.

Create a Go server to receive messages

Create a file called receive_whatsapp.go and paste into it this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

import (
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fromnumber := r.FormValue("From")
	tonumber := r.FormValue("To")
	text := r.FormValue("Body")
	print("Message Received - ", fromnumber, " ", tonumber, " ", text)
}

func main() {
	http.HandleFunc("/receive_whatsapp/", handler)
	http.ListenAndServe(":8080", nil)
}

Configure Webhook URL in your WhatsApp Business Account

As shown below, you can very easily add/update webhook url against a WhatsApp Business Account. Once this step is concluded, you are good to receive incoming messages.

Test

Send a text message to the Plivo number you specified using any phone.