This guide will show you how to send a WhatsApp text message to your phone.
You can check the complete code example at the bottom of this guide.
What are WhastApp text messages?
You can only send text messages to your customer if they have contacted you first. This opens a 24-hour window for two-way communication. Otherwise, use a template message.
Difficulty level
This guide assumes basic knowledge of Rust and APIs.
Prerequisites
- A running Rust project
- An Infobip account
- A WhatsApp Business account (for testing purposes use the Infobip test sender)
Steps summary
Here’s a summary of the steps you will follow:
- Open a 24-hour window for text communication.
- Install the Infobip Rust SDK.
- Import the required structs.
- Create a main async function.
- Create a WhatsApp client instance.
- Configure a message payload.
- Send a message.
- Check a response (optional).
Steps
Go through the following steps to send the text message.
Open a 24-hour window for text communication
If you have a WhatsApp Business account, proceed to send a message with any content to your account. This will open the communication window.
If you don’t have a Whatsapp Business account, you can use the Infobip test sender. Please follow the “Activate Infobip Test Sender” instructions on the WhatsApp configuration page.
Install the Infobip Rust SDK
We first need to install the Infobip Rust SDK and add a couple of dependencies.
cargo add infobip_sdk | |
cargo add tokio | |
cargo add reqwest |
The above commands will install the actual SDK, the Tokio runtime, which allows you to run asynchronous code, and the Reqwest HTTP client, which the SDK uses to make HTTP requests.
Import the required structs
The following use
s are needed to import the required structs.
use infobip_sdk::api::whatsapp::WhatsappClient; | |
use infobip_sdk::configuration::Configuration; | |
use infobip_sdk::model::whatsapp::{SendTextRequestBody, TextContent}; | |
use reqwest::StatusCode; |
The WhatsappClient
contains the main interface which you will use to call endpoints. The Configuration
is used to
set up credentials and the base URL. The SendTextRequestBody
and TextContent
contain the data you will send to the
endpoint. StatusCode is a reqwest enum to represent the result HTTP status codes.
Create a main async function
Because the Rust SDK’s default behavior is asynchronous, you need to call it in an async context. To do this, use the Tokio runtime and create a #[tokio::main]
main function.
#[tokio::main] | |
async fn main() { |
The tokio::main
allows the main function to be async, so you can await your API calls.
Create a WhatsApp client instance
Inside the function we’ve just created, add an instance of the WhatsApp client, which will give you access to all the endpoints as methods of the struct. Note that to build the Configuration
instance, you must set up environment variables IB_API_KEY
and IB_BASE_URL
for the code to work as expected.
let client = WhatsappClient::with_configuration(Configuration::from_env_api_key().unwrap()); |
Or, alternatively, you can write your key and URL in a separate configuration line directly in the code like this:
let config = Configuration::with_api_key("URL".into(), ApiKey::new("key".into())); |
Replace URL
and key
with your specific base URL and API key values that you can find in
your Infobip account or on
the Infobip API reference provided you’re logged in.
Configure a message payload
Now, we need to create a SendText request body that contains our from
number, the destination
number, and the desired text message.
let request_body = SendTextRequestBody::new( | |
"44444444444", | |
"55555555555", | |
TextContent::new("Hello, Rustacean!") | |
); |
The request object supports many more fields! See WhatsApp text endpoint reference, for more details.
Key points:
- Use the international number formatting when adding a number to the
to
field. For example, for British numbers the value will look like this:44754322576
. - For free trial accounts, the number used as a recipient must be the same number registered when signing up with Infobip.
- If you want to send multiple messages to multiple recipients in one request, use a WhatsApp template message.
Send a message
Send a message and await a response.
let response = client.send_text(request_body).await.unwrap(); |
The send_text
method returns a Result
object, which you can unwrap to get a response. The response contains the
status code, and the body of the response. In case of an error, you can see what went wrong in the Error branch of the
Result
object, as a special SdkError
instance.
Check a response (optional)
We are done! If you want, you can check whether the response status is OK and print it.
assert_eq!(response.status, StatusCode::OK); | |
println!("Response: {:?}", response); |
The complete example
All of the above is condensed into the following code snippet.
use infobip_sdk::api::whatsapp::WhatsappClient; | |
use infobip_sdk::configuration::Configuration; | |
use infobip_sdk::model::whatsapp::{SendTextRequestBody, TextContent}; | |
use reqwest::StatusCode; | |
#[tokio::main] | |
async fn main() { | |
let client = WhatsappClient::with_configuration(Configuration::from_env_api_key().unwrap()); | |
let request_body = SendTextRequestBody::new( | |
"44444444444", | |
"55555555555", | |
TextContent::new("Hello, Rustacean!") | |
); | |
let response = client.send_text(request_body).await.unwrap(); | |
assert_eq!(response.status, StatusCode::OK); | |
println!("Response: {:?}", response); | |
} |
Take a look at the official documentation
You can find this code example and many more in our WhatsApp documentation of the Rust SDK! Take a look at the WhatsApp client methods to discover the possibilities.
Thanks for reading!