Greg Holmes

Vonage Team Member

Former Developer Educator @Vonage. Coming from a PHP background, but not restricted to one language. An avid gamer and a Raspberry pi enthusiast. Often found bouldering at indoor climbing venues.

[Read more]
< Tutorial />

Receive Inbound SMS With Go

Last updated on Nov 03, 2020

In a previous post, we showed you how to send an SMS with Go. This post will show you how to receive SMS messages with your Vonage virtual number.

A publically accessible webhook is required and configured with your Vonage account to receive an inbound SMS. This tutorial will cover the process of setting up a publically accessible webhook, and the functionality to receive inbound SMS messages. 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.

Screenshot of new Meetings API session in progress
Start developing in minutes with free credits on us. No credit card required!

Set up the Code

When Vonage receives an SMS to your virtual number, it checks whether you have configured a webhook to forward this SMS. This configuration could be either account-wide or specific to one virtual phone number.

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

Create a file called inbound-sms.go and enter the following code:

package main

import (
    "fmt"
    "net/http"
)

func main() {

    http.HandleFunc("/webhooks/inbound-sms", func(w http.ResponseWriter, r *http.Request) {
        params := r.URL.Query()
        fmt.Println("From: " + params["msisdn"][0] + ", message: " + string(params["text"][0]))
    })

    http.ListenAndServe(":8080", nil)
}

This code will listen for any request sent to the /webhooks/inbound-sms endpoint and outputs the msisdn and text part of the body of the request.

Expose the Project To the Internet

For Vonage APIs to make requests to your webhook endpoint, it must be accessible publicly over the internet.

Ngrok is our go-to tool for running examples in development. As a result, ngrok is the service of choice in this tutorial. If ngrok isn't installed, a great introduction to ngrok and how to install it can be found in this tutorial.

Launch Ngrok with the following command:

ngrok http 8080

Make a note of the public URLs that ngrok creates for you. These will be similar to (but different from) the following:

http://56feb86007e8.ngrok.io -> http://localhost:8080
https://56feb86007e8.ngrok.io -> http://localhost:8080  

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 inbound-sms URL in the dashboard each time you run the command.

Purchase a Vonage Number

If you haven't already purchased a Vonage virtual phone number to follow this tutorial, please go ahead and buy one now. You can do this in the developer dashboard; however, there is another option, which is the Nexmo CLI. The CLI can assist in performing the account management tasks without having to leave your Terminal.

The command below searches for phone numbers capable of sending and receiving SMS, as well as making sure the number belongs to the country of your choosing. The two-character country code chooses the country; for example, for the USA, it would be US.

vonage numbers:search --features=SMS COUNTRY_CODE

Choose one of the phone numbers from the list of phone numbers that are listed when you make command. Replace VONAGE_VIRTUAL_NUMBER in the command below with the chosen phone number, and run the command.

vonage numbers:buy VONAGE_VIRTUAL_NUMBER COUNTRY_CODE

Configure Your Vonage Account

Your new Vonage virtual number and your Webhook URL need to be linked together so that Vonage knows where to send the inbound SMS messages.

As with the previous step, you could add your webhook URL to your Vonage virtual number, but this tutorial will show the example on how to make this change via the web portal.

Before you can setup, take note of the following:

  • VONAGE_VIRTUAL_NUMBER: The number you are trying to use.
  • WEBHOOK_URL: Your Ngrok URL, followed by /webhooks/inbound-sms, so it should look something like: https://56feb86007e8.ngrok.io/webhooks/inbound-sms

Go to Numbers page in your dashboard.

Click on the "Edit" icon (looks like a pen) under "Manage" column. In the pop up under SMS > Inbound Webhook URL paste you WEBHOOK_URL and click "Save".

Time to Test

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

go run ./

Now from your phone you can text your VONAGE_VIRTUAL_NUMBER.

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

From: VONAGETEST, message: This is a test message

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 Inbound SMS:

{
  "api-key": "abcd1234",
  "msisdn": "447700900001",
  "to": "447700900000",
  "messageId": "0A0000000123ABCD1",
  "text": "Hello world",
  "type": "text",
  "keyword": "TEST",
  "message-timestamp": "2020-01-01 12:00:00 +0000"
}

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 either involving Go or receiving SMS messages:

Don't forget, if you have any questions, advice or ideas you'd like to share with the community, then 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.