Skip to content

Telebot #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 58 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
9058543
backend reinitialised from previous repo
Sreyas62 Apr 20, 2024
2f2d882
register end point
Sreyas62 Apr 20, 2024
06378f3
bot service initial commit
ALVINJOHNS Apr 20, 2024
cc1b136
massdata model created
nandkishorr Apr 20, 2024
b1f3bd7
massdata model updated
nandkishorr Apr 20, 2024
aab70fb
massdata model updated
nandkishorr Apr 20, 2024
6612423
bot service updated to handle multiple users at the same time
ALVINJOHNS Apr 20, 2024
ad739c8
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
ALVINJOHNS Apr 20, 2024
b599b23
keys encrypted
nandkishorr Apr 20, 2024
68fdb83
Signin dept
Sreyas62 Apr 20, 2024
3567b15
Signin complete
Sreyas62 Apr 20, 2024
0bf4535
mass data service completed
nandkishorr Apr 20, 2024
07ee314
api get mass data complete
nandkishorr Apr 20, 2024
817d700
api check mass data user verification complete
nandkishorr Apr 20, 2024
d981dcf
api for userdata created
nandkishorr Apr 21, 2024
9bcd7f3
Signin completed
Sreyas62 Apr 21, 2024
55b3557
userdata model created
nandkishorr Apr 21, 2024
07fa10a
started conned=cting api in bot
ALVINJOHNS Apr 21, 2024
b7cf400
api for userdata check created
nandkishorr Apr 21, 2024
eb2496b
to pull for api
ALVINJOHNS Apr 21, 2024
221b990
Superadmin started
Sreyas62 Apr 21, 2024
e87d5de
api for userdata check created
nandkishorr Apr 21, 2024
4cc286f
Merge branch 'main' of https://github.com/Sreyas62/Refracto-backend
nandkishorr Apr 21, 2024
849700f
updated the latest changes
nandkishorr Apr 21, 2024
456d600
updated the userdata model
nandkishorr Apr 21, 2024
9e733b6
updated the userdata services
nandkishorr Apr 21, 2024
8be0ff5
1st api call success
ALVINJOHNS Apr 21, 2024
3c43054
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
ALVINJOHNS Apr 21, 2024
67ab5b2
superadmin
Sreyas62 Apr 21, 2024
bb22bb6
added get department by name api
nandkishorr Apr 21, 2024
a8e681a
superadmin
Sreyas62 Apr 21, 2024
bfbb5c1
updated check user api
nandkishorr Apr 21, 2024
3b4b8d4
send for verification and verification status received
ALVINJOHNS Apr 21, 2024
f0d597e
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
ALVINJOHNS Apr 21, 2024
5860774
complaint model created
nandkishorr Apr 21, 2024
fe08dda
complaint model created
nandkishorr Apr 21, 2024
75b895f
superadmin
Sreyas62 Apr 21, 2024
2c35f85
chatgpt api created
nandkishorr Apr 21, 2024
00531e6
till verification success
ALVINJOHNS Apr 21, 2024
714b8b1
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
ALVINJOHNS Apr 21, 2024
6e479cb
Merge branch 'telebot' into main
Sreyas62 Apr 21, 2024
657c11a
Merge pull request #1 from Sreyas62/main
Sreyas62 Apr 21, 2024
6d519a2
chatgpt implemented
nandkishorr Apr 21, 2024
215b14b
api functions ready
ALVINJOHNS Apr 21, 2024
8631620
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
ALVINJOHNS Apr 21, 2024
197901c
Merge pull request #2 from Sreyas62/telebot
Sreyas62 Apr 21, 2024
5adf5de
chatgpt implemented
nandkishorr Apr 21, 2024
4840cda
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
nandkishorr Apr 21, 2024
52ca582
Merge pull request #3 from Sreyas62/telebot
Sreyas62 Apr 21, 2024
5ab80c0
api to send problem
ALVINJOHNS Apr 21, 2024
50934d2
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
ALVINJOHNS Apr 21, 2024
40d1904
gpt
Sreyas62 Apr 21, 2024
235b249
done till request to track details
ALVINJOHNS Apr 21, 2024
c5d4647
Merge pull request #4 from Sreyas62/main
Sreyas62 Apr 21, 2024
183a45e
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
ALVINJOHNS Apr 21, 2024
11d65d4
chatgpt implemented
nandkishorr Apr 21, 2024
89b6159
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
nandkishorr Apr 21, 2024
a1f07c1
Merge branch 'telebot' of https://github.com/Sreyas62/Refracto-backen…
nandkishorr Apr 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
*.env
node_modules/
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

170 changes: 170 additions & 0 deletions bot.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const TelegramBot = require('node-telegram-bot-api');
console.log('Bot has been started ...');const axios = require('axios');

const dotenv = require('dotenv');
const { use } = require('./src/routes');
dotenv.config();
const token = process.env.TELE_API_KEY; // Replace with your own bot token
const bot = new TelegramBot(token, { polling: true });

const users = {};

// Function to delete entries older than 5 minutes
function deleteOldEntries() {
const currentTime = Date.now();
for (const chatId in users) {
const entryTime = users[chatId].timestamp;
if (currentTime - entryTime > 5 * 60 * 1000) {
delete users[chatId];
}
console.log(users);
}
}

// Call the deleteOldEntries function every 5 minutes
setInterval(deleteOldEntries, 5 * 60 * 1000);

const apiCallFroDetails = async (user_id) => {
try {
const response = await axios.get('http://localhost:3000/user',{ params: { user_id } } );
return response.data;

} catch (error) {
console.error('Error calling backend API:', error);
throw new Error('Error calling backend API');
}
}

const apiCallToSendForVerification = async (user) => {
try {
const response = await axios.post('http://localhost:3000/massdatas',user );
console.log(response.data);
return response.data;

} catch (error) {
console.error('Error calling backend API:', error);
throw new Error('Error calling backend API');
}
}
const apiCallToSendProblem = async (problem) => {
try {
const response = await axios.post('http://localhost:3000/chatgpt/handlecomplaints',problem );
console.log(response.data);
return response.data;

} catch (error) {
console.error('Error calling backend API:', error);
throw new Error('Error calling backend API');
}
}

const apiCallToTrackComplaint = async (user_id) => {
try {
const response = await axios.get('http://localhost:3000/massdatas',{ params: { user_id } } );
return response.data;
} catch (error) {
console.error('Error calling backend API:', error);
throw new Error('Error calling backend API');
}
}

bot.onText(/\/start/,async (msg) => {
const chatId = msg.chat.id;
const user =await apiCallFroDetails(chatId);
console.log(user);
if (user.message==="User not found") {
users[chatId] = { state: 'waitingForPhoneNumber' };
users[chatId].timestamp = Date.now();
bot.sendMessage(chatId, 'Hello! Please enter your phone number:');
}
else if (user[0].state !== 'waitingForPhoneNumber' && user[0].state !== 'waitingForAadhaar') {
users[chatId] = user[0];
bot.sendMessage(chatId, 'Hello! Choose an option:', {
reply_markup: {
inline_keyboard: [
[{ text: 'Create a complaint', callback_data: 'CreateNewComplaint' }],
[{ text: 'Track existing complaint', callback_data: 'TrackExistingComplaint' }]
]
}
});
}
else if (users[chatId].state !== 'waitingForPhoneNumber' && users[chatId].state !== 'waitingForAadhaar') {
bot.sendMessage(chatId, 'Hello! Choose an option:', {
reply_markup: {
inline_keyboard: [
[{ text: 'Create a complaint', callback_data: 'CreateNewComplaint' }],
[{ text: 'Track existing complaint', callback_data: 'TrackExistingComplaint' }]
]
}
});
}
});

bot.on('message', async(msg) => {
const chatId = msg.chat.id;
const user = users[chatId];
if (!user) return;

switch (user.state) {
case 'waitingForPhoneNumber':
user.phoneNumber = msg.text;
if (user.phoneNumber.length !== 10 || isNaN(user.phoneNumber)) {
bot.sendMessage(chatId, 'Please enter a valid 10-digit phone number:');
return;
}
user.state = 'waitingForAadhaar';
bot.sendMessage(chatId, 'Please enter your Aadhaar number:');
break;
case 'waitingForAadhaar':
user.aadhaar = msg.text;
if (user.aadhaar.length !== 12 || isNaN(user.aadhaar)) {
bot.sendMessage(chatId, 'Please enter a valid 12-digit Aadhaar number:');
return;
}
user.state = 'waitingForVerification';
bot.sendMessage(chatId, 'Send for verification');
apiCallToSendForVerification({user_id:chatId,aadhaar_id:user.aadhaar,phone_no:user.phoneNumber,state:'waitingForProblem'});
break;
case 'waitingForVerification' :
bot.sendMessage(chatId, '/start');
case 'waitingForProblem':
user.problem = msg.text;
bot.sendMessage(chatId, '/start');
break;
case 'problemTitleWaiting':
user.problemTitle = msg.text;
user.state = 'problemTitleReceived';
bot.sendMessage(chatId, 'Please enter the details of your problem:');
break;
case 'problemTitleReceived':
user.problemDetails = msg.text;
user.state = 'problemDetailsReceived';
bot.sendMessage(chatId, 'Your complaint has been successfully submitted. Thank you!');
apiCallToSendProblem({user_id:chatId,problemTitle:user.problemTitle,problemDetails:user.problemDetails,state:'problemDetailsReceived'})
console.log('Problem received:');
break;
}
console.log(users);
});

bot.on('callback_query', async(query) => {
const chatId = query.message.chat.id;
const user = users[chatId];
console.log(user);
if (!user) return;

switch (query.data) {
case 'CreateNewComplaint':
bot.sendMessage(chatId, 'You have chosen to create a complaint.\n Please name your problem:');
user.state = 'problemTitleWaiting';
break;
case 'TrackExistingComplaint':
users.state='waitingForTrack';
const track=await apiCallToTrackComplaint(chatId);
bot.sendMessage(chatId, 'Your complaint tracking details is:');
break;
default:
// Handle unrecognized callback data
break;
}
});
Loading