Twilio + SpringBoot: How to send SMS and voice calls using Twilio and SpringBoot?

Hello everyone, today we will learn how to send SMS/voice calls using TWILIO and SPRING BOOT. We know, with the increase in the number of mobile devices around the globe today, and numerous mobile applications available to us, SMS is becoming the de facto standard for verification. 

Let's begin our hunt by creating a free account on Twilio, click here 

After successful registration go to your dashboard, there you can find your ACCOUNT SID and AUTH TOKEN,


It is quite simple, right? 👍 Next, we are going to explore how to use Twilio for sending SMS in a Spring Boot application.

Prerequisites 

  • Twilio account 
  • Springboot
  • Maven/Gradle 
  • Postman for testing
  • Eclipse

Project structure:



Project Dependency - pom.xml(maven):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.sibin.dev</groupId>
<artifactId>twilio_springboot_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>twilio_springboot_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>7.16.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


application.properties

twilio_account_sid=<YOUR ACCOUNT SID>
twilio_auth_token=<YOUR AUTH TOKEN>
twilioPhoneNumber=<YOUR TWILIO NUMBER>


MessageController

package com.knf.sibin.dev.controller;

import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

/**
* @author KNF
*
*/
@RestController
public class MessageController {

@Value("${twilioPhoneNumber}")
private String twilioPhoneNumber;

@Autowired
public MessageController(@Value("${twilio_account_sid}")  
String twilio_account_sid,
@Value("${twilio_auth_token}") String twilio_auth_token) {
Twilio.init(twilio_account_sid, twilio_auth_token);
}

@PostMapping("/send-message")
@ResponseStatus(HttpStatus.ACCEPTED)
public String sendMessage(@RequestBody MessageDetails messageDetails) {
Message.creator(new PhoneNumber(messageDetails.number),
 new PhoneNumber(twilioPhoneNumber),
messageDetails.message).create();

return "message is inbound!";
}

@PostMapping("/send-voicemessage")
@ResponseStatus(HttpStatus.ACCEPTED)
public String sendVoiceCall(@RequestBody MessageDetails messageDetails) 
throws URISyntaxException {
Message.creator(new PhoneNumber(messageDetails.number),  
new PhoneNumber(twilioPhoneNumber),
messageDetails.message).create();
Call.creator(new PhoneNumber(messageDetails.number),  
new PhoneNumber(twilioPhoneNumber),
new URI("http://demo.twilio.com/docs/voice.xml")).create();

return "call incoming";
}

public static class MessageDetails {
public String number;
public String message;
}
}


Driver class

package com.knf.sibin.dev;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TwilioSpringbootDemoApplication {

public static void main(String[] args) {
SpringApplication.run(TwilioSpringbootDemoApplication.class, args);
}
}


Run:

spring-boot:run

Test our application using postman or any other API tester

localhost:8080/send-message



localhost:8080/send-voicemessage

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete