Send Automated Messages On WhatsApp Using Kotlin

May 29, 2024

Transform your marketing strategy with the power of automated WhatsApp messages. Integrating the Wassenger API with Kotlin allows you to send your customers reminders, personalized messages, and promotional content seamlessly. This robust tool helps businesses boost sales and enhance customer engagement.

In this article, we’ll walk you through setting up and using the Wassenger API with Kotlin to automate your WhatsApp messaging. Ensure timely and targeted interactions with your audience, elevate your marketing efforts, enhance customer satisfaction, and drive significant sales growth with this comprehensive guide.

🫣 Don’t want to use programming? No problem! Explore our new no-code WhatsApp Campaigns feature. Import your contacts, define a message, set a delivery date and relax! 🥳 🥳

🤩 🤖 Wassenger is a complete communication platform and API solution for WhatsApp. Explore more than 100+ API use cases and automate anything on WhatsApp by signing up for a free trial and getting started in minutes!

Requirements

  • To have a WhatsApp number already linked to the platform and online.
  • Message recipient phone number with international prefix in E164 format. Example: +12345678900. Validate the phone number format here.

API endpoint

We will use the following API endpoint to send messages to a chat:

Prepare the request

Target API URL using the POST method

https://api.wassenger.com/v1/messages

Required HTTPS headers > Obtain your API key here

Content-Type: application/json
Token: $API_TOKEN

Use body in JSON format

{
  "phone": "+1234567890",
  "message": "Hello world, this is a sample message"
}

🖥️ Looking for a code example? Go to the API live tester and get ready-to-use code examples in 15+ programming languages, including Python, JavaScript, PHP, C#, Java, Ruby, Go, Powershell, cURL and more.

🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!

Send automated messages with Kotlin

To send automated WhatsApp messages using Kotlin with the OkHttp library, follow these steps:

  1. Install OkHttp: Ensure you have the OkHttp package installed. Installation via Maven.
  2. Prepare the Request: Use OkHttp to create and send a POST request with the necessary headers and JSON body.

// This code requires you to have installed okhttp package. // Documentation: https://square.github.io/okhttp/recipes/ // Installation via Maven: https://square.github.io/okhttp/#releases

// Send text message to a phone number val client = OkHttpClient()

val mediaType = MediaType.parse("application/json") val body = RequestBody.create(mediaType, "{\"phone\":\"+1234567890\",\"message\":\"Hello world! This is a test message.\"}") val request = Request.Builder() .url("https://api.wassenger.com/v1/messages") .post(body) .addHeader("Content-Type", "application/json") .addHeader("Token", "") .build()

val response = client.newCall(request).execute()

You can also send various types of messages depending on your needs. Our API is versatile and robust.

1. Send text messages with high priority to a group

val client = OkHttpClient()
val mediaType = MediaType.parse("application/json")
val body = RequestBody.create(mediaType, "{\"group\":\"123456789000000000@g.us\", \"priority\":\"high\", \"message\":\"Hello world! This is a simple test message.\"}")
val request = Request.Builder()
.url("https://api.wassenger.com/v1/messages")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Token", "<api token goes here>")
.build()
val response = client.newCall(request).execute()

2. Send media messages to users. Note the file must be updated first

Learn how to send multimedia messages and upload files here

val client = OkHttpClient()
val mediaType = MediaType.parse("application/json")
val body = RequestBody.create(mediaType, "{\"phone\":\"+1234567890\", \"message\":\"Hello world! This is a test media message.\", \"media\":{\"file\":\"<24 characters length file ID>\"}}")
val request = Request.Builder()
.url("https://api.wassenger.com/v1/messages")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Token", "<api token goes here>")
.build()
val response = client.newCall(request).execute()

3. Send a text message that should be delivered now

val client = OkHttpClient()
val mediaType = MediaType.parse("application/json")
val body = RequestBody.create(mediaType, "{\"phone\":\"+1234567890\", \"message\":\"Hello world! This is a simple test message.\", \"enqueue\":\"never\"}")
val request = Request.Builder()
.url("https://api.wassenger.com/v1/messages")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Token", "<api token goes here>")
.build()
val response = client.newCall(request).execute()

4. Send scheduled messages with a custom delay

val client = OkHttpClient()
val mediaType = MediaType.parse("application/json")
val body = RequestBody.create(mediaType, "{\"phone\":\"+1234567890\", \"schedule\":{\"delayTo\":\"1h\"}, \"message\":\"Hello world! This is a simple test message.\"}")
val request = Request.Builder()
.url("https://api.wassenger.com/v1/messages")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Token", "<api token goes here>")
.build()
val response = client.newCall(request).execute()

5. Send a scheduled message at a specific date with a valid ISO 8601 date

val client = OkHttpClient()
val mediaType = MediaType.parse("application/json")
val body = RequestBody.create(mediaType, "{\"phone\":\"+1234567890\", \"deliverAt\":\"2019-01-01T11:00:00.410Z\", \"message\":\"Hello world! This is a simple test message.\"}")
val request = Request.Builder()
.url("https://api.wassenger.com/v1/messages")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Token", "<api token goes here>")
.build()
val response = client.newCall(request).execute()

🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!

Live API testing

You can live-test and play with the API directly from your browser.

Once you are done testing, get the auto-generated code example in your preferred programming language and you will be ready to go.

🤩 🤖 Wassenger is a complete communication platform and API solution for WhatsApp. Explore more than 100+ API use cases and automate anything on WhatsApp by signing up for a free trial and getting started in minutes!

FAQ

From where can I obtain the API code examples?

Please check the API documentation and select the ready-to-use code example in the desired language.

We provide code examples for most common languages and CLI tools, such as JavaScript, Node.js, Python, Ruby, PHP, Java, C#, Swift, Go and more.

Is the number of API keys related to the account or the device? If I have two devices do I have two API keys?

Each account has 1 API key, so if you have two devices in the same account you would only have 1 API key.

If you need 2 API keys, you can create 2 different accounts with one device each. There are no additional costs.

How to validate if a phone number can receive WhatsApp messages 📳

You can validate if a given phone number is linked to a WhatsApp account and can receive messages.

The API provides an endpoint that can validate whether a given phone number exists in WhatsApp.

The only requirement is to have at least one WhatsApp number connected to the platform in your current account.

For more details, please check out the API endpoint documentation here.

Before you check if a phone number exists on WhatsApp, you can also validate and normalize the format of a list of phone numbers by using the numbers validator API endpoint. This endpoint only validates the correct E164 format but does not check whether the phone number is effectively on WhatsApp.

Note: The number of WhatsApp check validations is limited per month based on your subscription plan. Please check out the pricing table for more details about the limits.

Looking for more answers? Check out the extended FAQs.

Further useful resources

API Documentation 🖥️

For more details about the endpoint API, please check the documentation where you will find all the details about the accepted request params, possible success or error responses and ready-to-use code examples in multiple programming languages:

https://app.wassenger.com/docs/#tag/Messages/operation/createMessage

Ready to transform your WhatsApp communication?

Start automating your customer interactions today with Wassenger

Get Started Free