Introduction
In this tutorial, you'll learn how to send messages to phone numbers stored in a CSV file using Go, the http
package, and the 1WAP™ API. We will read the phone numbers and messages from the CSV file, and then send them using the 1WAP™ API.
Prerequisites
- Basic understanding of Go programming language
- Go installed on your computer
- A text editor for writing code (e.g: Visual Studio Code, Atom, Vim)
- A 1WAP™ API token (you can obtain this by signing up here)
Create the CSV file
Create a new file named numbers.csv
in your project directory with two columns:
- First column: phone number in E164 format with the country prefix.
- Second column: text message to send to the target phone number.
The spreadsheet document should look like this:
Phone number | Message body |
---|---|
+1234567890 | 👋 Welcome to {{your-business-name}}! Thanks for signing up. We are just a message away! |
+1234567890 | 💐 Your order has been shipped. Tracking number is {{tracking-number}}. Don't hesitate to reach out to if you need help! 🤗 |
The equivalent spreadsheet document exported as CSV should look like this:
+1234567890,"👋 Welcome to {{your-business-name}}! Thanks for signing up. We are just a message away!"
+1234567890,"💐 Your order has been shipped. Tracking number is {{tracking-number}}. Don't hesitate to reach out to if you need help! 🤗"
You can export any Office Excel or Google Sheets document as CSV file by following these instructions:
- Learn how to export an Excel spreadsheet as a CSV file
- Learn how to export Google Sheet as a CSV file
Create a file with the code
Create a new file named send_messages.go
in your project directory and add the following code:
package main
import (
"encoding/csv"
"fmt"
"io"
"net/http"
"os"
"strings"
)
const csvFile = "numbers.csv"
// Replace this with your 1WAP™ API token
var apiToken = "ENTER API KEY HERE"
// Optionally specify the target WhatsApp device ID connected to 1WAP™
// you want to use for messages delivery (24 characters hexadecimal value)
var device = "DEVICE ID GOES HERE"
var headers = map[string]string{
"Content-Type": "application/json",
"Authorization": apiToken,
}
var url = "https://api.1wap.app/v1/messages"
func sendMessage(phone, message string) {
json := fmt.Sprintf(`{"phone":"%s","message":"%s","device":"%s"}`, phone, message, device)
req, err := http.NewRequest("POST", url, strings.NewReader(json))
if err != nil {
fmt.Printf("Failed to create request for %s: %v\n", phone, err)
return
}
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("Failed to send message to %s: %v\n", phone, err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusCreated {
fmt.Printf("=> Message created: %s\n", phone)
} else {
fmt.Printf("Failed to create message for %s: %s\n", phone, resp.Status)
}
}
func main() {
file, err := os.Open(csvFile)
if err != nil {
fmt.Printf("Error opening CSV file: %v\n", err)
return
}
defer file.Close()
reader := csv.NewReader(file)
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
fmt.Printf("Error reading CSV file: %v\n", err)
return
}
phone, message := record[0], record[1]
sendMessage(phone, message)
}
}
Replace the API token
In the send_messages.go
file, make sure you have defined the API token of your actual 1WAP™ account:
var apiToken = "ENTER API KEY HERE"
Optionally, if you have multiple WhatsApp numbers connected in your 1WAP™ account, you can specify which WhatsApp number you want to use for the messages delivery by specifying the 1WAP™ unique device ID (24 characters hexadecimal value) in the following line:
var deviceID = "DEVICE ID GOES HERE"
Run the program
Open a terminal in your project directory and run the following command to execute the send_messages.go
script:
go run send_messages.go
If everything is set up correctly, you should see output indicating the messages have been created successfully:
=> Message created: +1234567890
=> Message created: +1234567890
=> Message created: +1234567890
Note messages will be added to your number's message delivery queue and delivered asynchronously in background over time based on your number's subscription message delivery speed per minute limit or the manually configured delivery speed you have defined in your number's settings.
Conclusion
In this tutorial, you learned how to send messages to phone numbers stored in a CSV file using Go language and the 1WAP™ API.
You can update numbers.csv
file and run the program again anytime you want to send new messages through your 1WAP™ connected WhatsApp number.
You can further customize the script to handle additional columns, create different types of messages, or integrate it with your own software as needed.