DEV Community

Cover image for Receive SMS Delivery Receipts With Go
Greg Holmes for Vonage

Posted on • Originally published at learn.vonage.com on

Receive SMS Delivery Receipts With Go

In previous posts, we showed you how to send an SMS with Go and receive an SMS with Go. This post will show you how to receive SMS delivery receipts of SMS messages sent from your Vonage account.

This tutorial will cover setting up a publicly accessible webhook and the functionality to receive SMS delivery receipts. You can find the code used in this tutorial on our Go Code Snippets Repository.

Prerequisites

Vonage API Account

To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs. If you’ve just signed up, the initial cost of a number will be easily covered by your available credit.

Start building with Vonage

Set up the Code

When Vonage sends an SMS from your account, it checks whether you have configured a webhook to forward any delivery receipts to. This configuration is an account-wide setting.

If you have configured a webhook, Vonage will send a POST request to this webhook, so it's time to create the code to handle this webhook request.

Create a file called delivery-receipt.go and enter the following code:

package main

import (
    "fmt"
    "net/http"
)

func main() {

    http.HandleFunc("/webhooks/delivery-receipt", func(w http.ResponseWriter, r *http.Request) {

        if err := r.ParseForm(); err != nil {
            fmt.Fprintf(w, "ParseForm() err: %v", err)
            return
        }

        fmt.Println("Delivery receipt status: " + r.FormValue("status"))
    })

    http.ListenAndServe(":8080", nil)
}
Enter fullscreen mode Exit fullscreen mode

This code will listen for any request sent to the /webhooks/delivery-receipt endpoint and outputs the status part of the body of the request.

Expose the Project To the Internet

Your webhook endpoint needs to be accessible publicly over the internet for Vonage APIs to make requests to it.

Ngrok is our suggested tool used to run examples in development and is used in this tutorial to expose the webhook endpoint. If you haven't got ngrok installed, you can find a great introduction to this service and how to install it in this tutorial.

Launch ngrok with the command below:

ngrok http 8080
Enter fullscreen mode Exit fullscreen mode

Make a note of the public URLs that ngrok creates for you; these will be similar to the example below:

http://abc123.ngrok.io -> http://localhost:8080
https://abc123.ngrok.io -> http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

Note: This URL will be different every time you run the command if you're using the free plan. So you will have to update the delivery-receipt URL in the dashboard each time you run the command.

Configure Your Vonage Account

Your Vonage account needs configuring to know where to make the delivery receipt requests. You can add the delivery receipt webhook URL in your Vonage settings page, under the Delivery receipts label. The image below shows an example of this:

An example of the delivery receipts settings in the Vonage Dashboard

Time to Test

With Ngrok already running, in a new Terminal window, make sure you've navigated to the project directory containing your delivery-receipt.go file. Run the following command:

go run ./
Enter fullscreen mode Exit fullscreen mode

Now, within another Terminal window, run the following command, replacing YOUR_NUMBER with your phone number to receive the test SMS message.

nexmo sms -f VONAGETEST YOUR_NUMBER "This is a test message."
Enter fullscreen mode Exit fullscreen mode

If you check the Terminal window where you ran go run ./, you should see a line like what you see below appear:

Delivery receipt status: delivered
Enter fullscreen mode Exit fullscreen mode

The webhook receives more fields than are displayed above. You can see a full list of these fields with some examples below. The example below is from the API docs under Delivery Receipt:

{
  "msisdn": "447700900000",
  "to": "AcmeInc",
  "network-code": "12345",
  "messageId": "0A0000001234567B",
  "price": "0.03330000",
  "status": "delivered",
  "scts": "2001011400",
  "err-code": "0",
  "api-key": "abcd1234",
  "client-ref": "my-personal-reference",
  "message-timestamp": "2020-01-01 12:00:00 +0000",
  "timestamp": "1582650446",
  "nonce": "ec11dd3e-1e7f-4db5-9467-82b02cd223b9",
  "sig": "1A20E4E2069B609FDA6CECA9DE18D5CAFE99720DDB628BD6BE8B19942A336E1C"
}
Enter fullscreen mode Exit fullscreen mode

Further Reading

You can find the code shown in this tutorial on the Go code snippets repository.

Below are a few other tutorials we've written involving Go:

Don't forget that if you have any questions, advice, or ideas you'd like to share with the community, please feel free to jump on our Community Slack workspace. I'd love to hear back from anyone that has implemented this tutorial and how your project works.

Oldest comments (0)