In this article, we will learn to build an application that has a feature to make a call to any mobile number. So, to implement this feature we will use a 3rd party tool named Twilio, which provides a feature to make a voice call as well as send the SMS programmatically.
Just follow all the mentioned steps.
Step 1: Create a project from Spring Initializr.
Please fill the following fields.
- Group name: com.pixeltrice
- Artifact Id: spring-boot-call-enabled-app
Step 2: Click on the Generate button and the project will start downloading on the bottom left corner of your screen.
Step 3: Unzip the project and import it in IDE such as Eclipse.
Select File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-call-enabled-app-> Finish.
Step 4: Add the Twilio dependency in pom.xml file
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>7.54.2</version>
</dependency>
Step 5: Signup to the Twilio account
This will hardly take 30 seconds to signup. Once you completed signup, you will get three important parameters such as ACCOUNT_SID, AUTH_ID, and your Twilio phone number.
Step 6: Inside the Spring Main class, declared the following parameter.
In this, we have to create a variable to store the important parameter values from which we are able to make a voice call to anyone.
private final static String SID_ACCOUNT = "Your Twilio SID"
private final static String AUTH_ID = "Twilio Auth ID"
private final static String FROM_NUMBER="Twilio Mobile number"
private final static String TO_NUMBER =" Mobile number To whom you want make a voice call"
Note: Make sure that mobile number should always start with country code, for example, if you are making a call to Indian mobile number then it should with +91, or if you are from the USA then +1. Similarly for all other countries.
Step 7: Initialize the Twilio account with ACCOUNT_SID and AUTH_ID with a static block as shown below.
Here we have initialized the Twilio tool in our Spring Boot application. We put in the static block because when we run the application at the time only the Twilio will initialize in our project.
static {
Twilio.init(SID_ACCOUNT, AUTH_ID);
}
Step 8: Add a Method in Spring Boot main class to make a voice call
In this step, we are calling a creator() method on the Object of Call Class which is declared or defined in the predefined package of Twilio library. In the creator() method we are passing the Phone number to who wants to make a call and our Mobile number generated from Twilio.
Call.creator(new PhoneNumber(TO_NUMBER), new PhoneNumber(FROM_NUMBER),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
Step 9: Our final Spring Boot Main class will look like as shown below.
Finally, after adding all the mentioned code, we will get our final main class. And Here I also implemented an interface ApplicationRunner. This interface having a method run() which will automatically get executed once the application starts running.
So, we put all the important codes which we want to get executed just after an application starts running. In this case, we have added a line of code to make a call.
Hence, once the application starts running it will automatically make a call to the mentioned mobile number.
package com.pixeltrice.springbootcallenabledapp;
import java.net.URI;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
@SpringBootApplication
public class SpringBootCallEnabledAppApplication implements ApplicationRunner {
private final static String SID_ACCOUNT = "Your Twilio SID";
private final static String AUTH_ID = "Twilio Auth ID";
private final static String FROM_NUMBER="Twilio Mobile number";
private final static String TO_NUMBER =" Mobile number To whom you want make a voice call";
static {
Twilio.init(SID_ACCOUNT, AUTH_ID);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootCallEnabledAppApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
Call.creator(new PhoneNumber(TO_NUMBER), new PhoneNumber(FROM_NUMBER),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
}
}
Note: Make sure all the imported pacakge should be same as in above code.
Step 10: Run the Application, you will definitely get a call on the number as you mentioned in variable TO_NUMBER.
Step 11: Recieve the call to listen the voice.
Summary
Thank You so much for reading this article. Today we learned to make a voice call from Spring Boot Application. I hope you learned something new today. If you have any queries or doubt please feel free to ask, I am always available to help you.
You might also like this article.
- Send an SMS or Message from Spring Boot Application to Mobile Phone
- How to send email using Spring Boot Application
- Spring Boot Security using OAuth2 with JWT
- Build Spring Boot Restful CRUD API with Hibernate and Postgresql from scratch
- Deploy the Spring Boot Application on External Tomcat Server
Main class not found error in this program
G
Hi Vino, This happens sometimes please follow the below steps, any of them can fix your problem.
1) Go to Spring Boot main class, Right-click –> Run As Java Application.
2) Update your maven project. Right-click on your project -> Maven -> Update Project.
3) Add the starter class tag inside the properties tag in the pom.xml file. Make sure you should give the fully qualified class name where the main method is present. As shown below.
properties (opening tag)
start-class (opening tag)
com.pixelTrice.firstSpringBootApplication.FirstSpringBootApplication(Fully qualified name)
start-class (closing tag)
properties (closing tag)
Note: Let me know if you are still getting any error. I would love to help you at any time.