Engineering College Chat Bot with giving Analysis Using SpringBoot













This application can be used in engineering colleges.Here we can provide analysis and deeperanalysis according to the user requirement.Here everything is given from where to start the application.This is basically a EngineeringCollegeBot Using Telegram. 



In telegram,Search you BotFather(The above picture shows the BotFather).There In the list /newbot from the list ,type it




Follow the instruction after entering everthing ,we finally create a Bot name given,In this case Bot is EngineeringCollegeBot.In the above one HTTP API and a token is generated.You have to keep token for your further use.BotFather gives the token to newly created Bot.

With the help of Telegram App, we created the bot ,In this case EngineeringCollegeBot and with SpringBoot we will make Application run.Here we use Eclipse IDE .Below we create the complete package folder for the application

                                                   


  pom.xml with the dependencies need in this project is given below

                                                             pom.xml

<?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>3.2.0-SNAPSHOT</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.example.engineeringcollege</groupId>

<artifactId>EngineeringColleges</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>EngineeringColleges</name>

<description>Demo project for EngineeringColleges</description>

<properties>

<java.version>17</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>com.github.pengrad</groupId>

<artifactId>java-telegram-bot-api</artifactId>

<version>6.3.0</version>

</dependency>

<dependency>

<groupId>org.telegram</groupId>

<artifactId>telegrambots-spring-boot-starter</artifactId>

<version>6.8.0</version>

</dependency>

<dependency>

<groupId>org.telegram</groupId>

<artifactId>telegrambots</artifactId>

<version>5.2.0</version>

</dependency>

</dependencies>


<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

<repositories>

<repository>

<id>spring-milestones</id>

<name>Spring Milestones</name>

<url>https://repo.spring.io/milestone</url>

<snapshots>

<enabled>false</enabled>

</snapshots>

</repository>

<repository>

<id>spring-snapshots</id>

<name>Spring Snapshots</name>

<url>https://repo.spring.io/snapshot</url>

<releases>

<enabled>false</enabled>

</releases>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>spring-milestones</id>

<name>Spring Milestones</name>

<url>https://repo.spring.io/milestone</url>

<snapshots>

<enabled>false</enabled>

</snapshots>

</pluginRepository>

<pluginRepository>

<id>spring-snapshots</id>

<name>Spring Snapshots</name>

<url>https://repo.spring.io/snapshot</url>

<releases>

<enabled>false</enabled>

</releases>

</pluginRepository>

</pluginRepositories>


</project>


Basically telegramBot dependencies and spring dependencies are used.


EngineeringCollegesApplication.java



package com.example.engineeringcollege.EngineeringColleges;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.telegram.telegrambots.meta.TelegramBotsApi;

import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;


import com.example.engineeringcollege.BotClass.EngineeringCollegeBot;


@SpringBootApplication

public class EngineeringCollegesApplication {


public static void main(String[] args) {

SpringApplication.run(EngineeringCollegesApplication.class, args);

try {

TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);

botsApi.registerBot(new EngineeringCollegeBot());

System.out.println("bot registered successfully");

} catch (TelegramApiException e) {

e.printStackTrace();

}

}


}

In try block the registration with telegram API our bot class is performed .That is what is performed.Its a main class where all is started.


                                                 EngineeringCollegeBot.java  

package com.example.engineeringcollege.BotClass;



import com.pengrad.telegrambot.TelegramBot;

//import com.pengrad.telegrambot.request.SendMessage;


import java.util.HashMap;

import java.util.Map;


//import com.pengrad.telegrambot.TelegramBotAdapter;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.stereotype.Component;

import org.telegram.telegrambots.bots.DefaultAbsSender;

import org.telegram.telegrambots.bots.TelegramLongPollingBot;

import org.telegram.telegrambots.meta.api.methods.send.SendMessage;

import org.telegram.telegrambots.meta.api.objects.Message;

import org.telegram.telegrambots.meta.api.objects.Update;

import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;

import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardRemove;

import org.telegram.telegrambots.meta.bots.AbsSender;

import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import static com.example.engineeringcollege.UserStateUsing.UserStateEnume.AWAITING_CONFIRMATION;

import static com.example.engineeringcollege.UserStateUsing.UserStateEnume.AWAITING_NAME;

import static com.example.engineeringcollege.UserStateUsing.UserStateEnume.ENGINEERING_BRANCH_SELECTION;

import static com.example.engineeringcollege.UserStateUsing.UserStateEnume.ENGINEERING_DEEPER_ANALYSIS;

import com.example.engineeringcollege.KeyBoardFactoryUsing.*;

import com.example.engineeringcollege.UserStateUsing.UserStateEnume;




@Component

public class EngineeringCollegeBot extends TelegramLongPollingBot {

private final HashMap<Long, UserStateEnume> chatStates =new HashMap<>();

@Override

public void onUpdateReceived(Update update) {

String userMessage=update.getMessage().getText();

Long chatId=update.getMessage().getChatId();

SendMessage message = new SendMessage();

if(userMessage.equalsIgnoreCase("/start")) {

replyToStart(chatId);

}

if(!userMessage.equalsIgnoreCase("/start")) {

switch (chatStates.get(chatId)) { case AWAITING_NAME ->

replyToBranch(chatId, update); case ENGINEERING_BRANCH_SELECTION ->

replyToEngineeringBranchAnalysis(chatId, update);

case ENGINEERING_DEEPER_ANALYSIS ->

replyToDeeperEngineeringCollegeAnalysis(chatId, update); case AWAITING_CONFIRMATION ->

replyToFurther(chatId, update);}

}

}

public void replyToStart(Long chatId) {

try {

SendMessage message= new SendMessage();

message.setChatId(chatId.toString());

message.setText("Welcome to PPP Engineering College,We the leading College in India,Welcome to our Bot!!!!Enter your name");

execute(message);

chatStates.put(chatId,AWAITING_NAME );

}catch (TelegramApiException e) {

e.printStackTrace();

}

}

private void replyToBranch(Long chatId, Update message) {

promptWithKeyboardForState(chatId, "Hello " + message.getMessage().getText() + ". Select the branch you are looking for,we will provide analysis ",

KeyboardFactory.getBranchKeyword(),

ENGINEERING_BRANCH_SELECTION);

}

private void promptWithKeyboardForState(Long chatId, String text, ReplyKeyboard YesOrNo, UserStateEnume branchSelection) {

SendMessage sendMessage = new SendMessage();

sendMessage.setChatId(chatId.toString());

sendMessage.setText(text);

sendMessage.setReplyMarkup(YesOrNo);

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


chatStates.put(chatId, branchSelection);

}

private void replyToEngineeringBranchAnalysis(Long chatId, Update message) {

SendMessage sendMessage = new SendMessage();

sendMessage.setChatId(chatId.toString());

if ("ECE".equalsIgnoreCase(message.getMessage().getText())) {

sendMessage.setText("Welcome to ECE ,HOD of this branch is Mr SuryaKumar.You can have the opp to enter this branch with EAMCET rank less then 5000!!!! ");

sendMessage.setReplyMarkup(KeyboardFactory.getBranchKeyword());

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

chatStates.put(chatId, ENGINEERING_DEEPER_ANALYSIS);

}

if ("EEE".equalsIgnoreCase(message.getMessage().getText())) {

sendMessage.setText("Welcome to EEE ,HOD of this branch is Mr Saketh.You can have the opp to enter this branch with EAMCET rank less then 10000!!!! ");

sendMessage.setReplyMarkup(KeyboardFactory.getBranchDeeperAnalysis());

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

chatStates.put(chatId, ENGINEERING_DEEPER_ANALYSIS);

}

if ("CSE".equalsIgnoreCase(message.getMessage().getText())) {

sendMessage.setText("Welcome to CSE,HOD of this branch is Mr kUMARI.You can have the opp to enter this branch with EAMCET rank less then 1000!!!! ");

sendMessage.setReplyMarkup(KeyboardFactory.getBranchDeeperAnalysis());

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

chatStates.put(chatId, ENGINEERING_DEEPER_ANALYSIS);

}

if ("MEC".equalsIgnoreCase(message.getMessage().getText())) {

sendMessage.setText("Welcome to MEC ,HOD of this branch is Mr Kyder.You can have the opp to enter this branch with EAMCET rank less then 15000!!!! ");

sendMessage.setReplyMarkup(KeyboardFactory.getBranchDeeperAnalysis());

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

chatStates.put(chatId, ENGINEERING_DEEPER_ANALYSIS);

}

if ("CIVIL".equalsIgnoreCase(message.getMessage().getText())) {

sendMessage.setText("Welcome to CIVIL ,HOD of this branch is Mr Praba.You can have the opp to enter this branch with EAMCET rank less then 20000!!!! ");

sendMessage.setReplyMarkup(KeyboardFactory.getBranchDeeperAnalysis());

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

chatStates.put(chatId, ENGINEERING_DEEPER_ANALYSIS);

}


}

private void replyToDeeperEngineeringCollegeAnalysis(Long chatId, Update message) {

if ("DAECE".equalsIgnoreCase(message.getMessage().getText())) {

promptWithKeyboardForState(chatId, "deep analysis of the college PPP ",

KeyboardFactory.getYesOrNo(), AWAITING_CONFIRMATION);

}

if ("DAEEE".equalsIgnoreCase(message.getMessage().getText())) {

promptWithKeyboardForState(chatId, "deep analysis of the college PPP",

KeyboardFactory.getYesOrNo(), AWAITING_CONFIRMATION);

}

if ("DACSE".equalsIgnoreCase(message.getMessage().getText())) {

promptWithKeyboardForState(chatId, "deep analysis of the college PPP",

KeyboardFactory.getYesOrNo(), AWAITING_CONFIRMATION);

}

if ("DAMEC".equalsIgnoreCase(message.getMessage().getText())) {

promptWithKeyboardForState(chatId, "deep analysis of the college PPP",

KeyboardFactory.getYesOrNo(), AWAITING_CONFIRMATION);

}

if ("DACIVIL".equalsIgnoreCase(message.getMessage().getText())) {

promptWithKeyboardForState(chatId, "deep analysis of the college PPP",

KeyboardFactory.getYesOrNo(), AWAITING_CONFIRMATION);

}

}

private void replyToFurther(Long chatId, Update message) {

SendMessage sendMessage = new SendMessage();

sendMessage.setChatId(chatId.toString());

if ("yes".equalsIgnoreCase(message.getMessage().getText())) {

sendMessage.setText("Completed our deep analysis !!! want analysis for other branch");

sendMessage.setReplyMarkup(KeyboardFactory.getBranchKeyword());

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

chatStates.put(chatId, ENGINEERING_BRANCH_SELECTION);

} else if ("no".equalsIgnoreCase(message.getMessage().getText())) {

stopChat(chatId);

} else {

sendMessage.setText("Please select yes or no");

sendMessage.setReplyMarkup(KeyboardFactory.getYesOrNo());

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

private void stopChat(Long chatId) {

SendMessage sendMessage = new SendMessage();

sendMessage.setChatId(chatId.toString());

sendMessage.setText("Analysis ended!!!Thankyou for your time ,PPP Engineering college looking for your enrollment ");

chatStates.remove(chatId);

sendMessage.setReplyMarkup(new ReplyKeyboardRemove(true));

try {

execute(sendMessage);

} catch (TelegramApiException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public String getBotUsername() {

return "EngineeringSatCollegeBot";

}


@Override

public String getBotToken() {

return "66954767547326486446747:AAEOcMP-hgHJyJ9fa-KNzdMIVpKz16-pCM4";

(put the token you get from telegram bot father)

}


}


The above is the Bot class which is extended from TelegramLongPollingBot .


KeyboardFactory.java

package com.example.engineeringcollege.KeyBoardFactoryUsing; import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard; import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup; import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow; import java.util.List; public class KeyboardFactory { public static ReplyKeyboard getBranchDeeperAnalysis() { KeyboardRow row = new KeyboardRow(); row.add("DAECE"); row.add("DAEEE"); row.add("DACSE"); row.add("DAMEC"); row.add("DACIVIL"); return new ReplyKeyboardMarkup(List.of(row)); } public static ReplyKeyboard getBranchKeyword(){ KeyboardRow row = new KeyboardRow(); row.add("ECE"); row.add("EEE"); row.add("CSE"); row.add("MEC"); row.add("CIVIL"); return new ReplyKeyboardMarkup(List.of(row)); } public static ReplyKeyboard getYesOrNo() { KeyboardRow row = new KeyboardRow(); row.add("Yes"); row.add("No"); return new ReplyKeyboardMarkup(List.of(row)); } }


This is normal keyboard which is used in telegram app(Engineering College)


UserStateEnume.java



package com.example.engineeringcollege.UserStateUsing;


public enum UserStateEnume {

AWAITING_NAME, ENGINEERING_BRANCH_SELECTION, ENGINEERING_DEEPER_ANALYSIS, AWAITING_CONFIRMATION


}


WE CAN SEE THE OUTPUT LIKE


Comments