Digital communication has become one of the critical factors in almost all the areas, such as banking, health, government, education, and social media, for many purposes such as user authorization, appointment booking, and many more. Among different approaches to communication, SMS is one of the well-known methods.
Developers use many APIs and related approaches to implement SMS functionality in those areas. In the modern world, many developers use Golang during their development process. As a result, SMS implementation using Golang is getting popular.
This post discusses three ways to send an SMS using Golang.
01 - Using Twilio API
Twilio APIs provide seamless communication with users via voice, SMS, video, or chat. Developers can send SMS easily using the Twilio Go Helper Library.
Twilio SMS pricings and services depend on the country you live in. For example, in the UK, only sending and receiving picture messages are free of charge, and alphanumeric sender id services have a different approach.
For outbound services for UK users, the pricing depends on the country.
As prerequisites, you must have a Twilio account and a Twilio mobile number. Twilio offers a $15 credit for new users when upgrading to a paid version. When using Twilio, it is essential to know its features and limitations.
Pros of Twilio
- Powerful communication strategies and insanely fast service.
- Developer-friendly descriptive documentation.
- Support for many programming languages.
Cons of Twilio
- Complex user interface for non-developers.
- Limitations of features when you have a trial account (limited caller time, can only send SMS to non-Twilio numbers which have verified with Twilio)
- Twilio has different features based on the region.
Tutorial on Sending SMS with Twilio
01 - First, you must create a trial account by navigating to the Twilio signup page. After filling in all the essential details, you will be prompted to the welcome page. You will have the option to select how you need to use Twilio. For example, you can use Twilio with no code at all.
However, here you need to select either with code or with minimal code and select the language as Golang.
02 - After starting the trial, you will be prompted to the dashboard, where you will have free credits of $15.
Now, you can create your own
main.go file and start the most exciting part.
03 - Then, you must import all the packages in the import block, and in the main package, initialize
AuthToken. You can find your
AuthToken in the account info section.
urlString is the Twilio API that we use for SMS messages. The
message is defined as the possible message bodies.
04 - Then, set up the
messageDate by initializing the sender, the receiver number, and the message body.
05 - Next, create the HTTP request client by initializing the request and wrapping credentials using the
06 - Then, make the POST request using HTTP
07 - Finally, run the following command to send the SMS.
Furthermore, the Twilio dashboard can send SMS using programming languages like Java, C#, Ruby, Python, Php, and NodeJS.
02 - Using TextMagic API
TextMagic API is a much simpler way of sending SMS using Golang. It provides a communication link between the application layer and TextMagic's API gateway. Furthermore, it allows you full access even during the trial period.
Pros of TextMagic API
- A secure way to send and receive SMS.
- It allows users to check the status of the message (sent or received).
- It will enable you to attach images, PDFs, and word files in sizes less than 10MB.
Cons of TextMagic API
- If you send a large amount of SMS and receive messages from your clients back, the service can be costly.
- Currently, only possible to send personal text messages or use TextMagic virtual numbers for 2FA or other verification purposes because of local network carrier restrictions in some countries.
The pricing for TextMagic works is similar to Twilio APIs. For the UK, the price for an SMS, MMS, and carrier lookup is $0.05.
Tutorial on Sending SMS with TextMagic
As a prerequisite, what TextMagic API need is to have the Go distribution on your computer.
01 - First, you must create an account in TextMagic. Here, I will sign up with Google.
02 - Next, go to the API Settings page and create the API keys. Here click on the Add new API key button, enter a name, and click on the Generate new key button.
03 - You must install the TextMagic in Golang helper by running the following command.
04 - You can use the following code example to send an SMS.
05 - Finally, run the
main.go file to send SMS using the following command.
03 - Using Multi-Channel Notification API - Courier
When considering requirements in the modern world, there are many scenarios where organizations need different approaches for communication, such as Email, Slack, WhatsApp, and even Twilio-like SMS services.
Courier is a multi-channel platform that allows seamless communication with various channels. With Courier, you can handle all such channels using only a single Golang API.
Pros of Courier
- Provide excellent documentation, which allows you to get straight into your tasks.
- Easy to understand the process, even for a person coming from a non-technical background.
- The analytic window allows the user to track the activities during the messaging process (check if the message is received/opened).
Cons of Courier
- It can be costly when operating with multiple notification channels.
01 - First, you need to sign-up for Courier and create a free account by following the descriptive guide and selecting SMS as the required service.
02 - Then connect with an SMS provider. Here, I will click on Twilio since I have already created an account with Twilo. Other than Twilio, there are many SMS providers which you can select.
03 - Enter your account SID and the token available on Twilio Dashboard.
04 - On the next screen, set the receiver's phone number.
05 - In the next screen, you can select the method or language needed in the service. You can also run the cURL command in the terminal to check your SMS gateway. I will choose Golang and continue to the next step.
06 - You must download the Golang package (SDK) using the following command.
07 - Next, you can create the
main.go file and paste the following code snippet generated in the Courier user interface.
08 - Finally, run the following command and check if you can receive the message.
This article proposed three ways to send SMS notifications via Golang and guided you in selecting the best SMS notification service for your Golang application. Also, we have discussed the pros and cons of using each method. We hope now you can select the most suitable approach for your future projects.
Thank you for reading.