Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
Component Sharing
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Gagandeep Singh Bhatia
Component Sharing
Commits
0ecb8d0f
Commit
0ecb8d0f
authored
Apr 12, 2024
by
Sonawane Amol Madhjuar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
73a117d7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
155 additions
and
0 deletions
+155
-0
Amol/TelegramBot/MyTelegramBot.java
Amol/TelegramBot/MyTelegramBot.java
+155
-0
No files found.
Amol/TelegramBot/MyTelegramBot.java
0 → 100644
View file @
0ecb8d0f
import
com.google.gson.Gson
;
import
org.telegram.telegrambots.bots.TelegramLongPollingBot
;
import
org.telegram.telegrambots.meta.TelegramBotsApi
;
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.exceptions.TelegramApiException
;
import
org.telegram.telegrambots.updatesreceivers.DefaultBotSession
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
public
class
MyTelegramBot
extends
TelegramLongPollingBot
{
private
Map
<
Long
,
Boolean
>
firstMessages
=
new
HashMap
<>();
private
Set
<
Long
>
processedChatIds
=
new
HashSet
<>();
@Override
public
void
onUpdateReceived
(
Update
update
)
{
if
(
update
.
hasMessage
()
&&
update
.
getMessage
().
hasText
())
{
Message
message
=
update
.
getMessage
();
Long
chatId
=
message
.
getChatId
();
// Print the chat ID only once
System
.
out
.
println
(
"New Chat_Id: "
+
chatId
);
String
text
=
message
.
getText
().
trim
().
toLowerCase
();
// Respond to the user's message
SendMessage
response
=
new
SendMessage
();
response
.
setChatId
(
chatId
.
toString
());
if
(
text
.
equals
(
"send table"
))
{
// Check if the table has already been sent to this chat ID
if
(
processedChatIds
.
contains
(
chatId
))
{
// If the table has already been sent, do not send it again
response
.
setText
(
"Table has already been sent to this chat."
);
}
else
{
// Example JSON data string
String
jsonData
=
"{\n"
+
" \"data\": [\n"
+
" {\"Intents\": \"Area Sales\", \"Invocation\": \"Customer outstanding\"},\n"
+
" {\"Intents\": \"Current GL balance of an account\", \"Invocation\": \"What is the outstanding for customer\"},\n"
+
" {\"Intents\": \"Customer Sales\", \"Invocation\": \"Show Customer Sales\"},\n"
+
" {\"Intents\": \"Division Sale\", \"Invocation\": \"Show Division Sales\"}\n"
+
" ]\n"
+
"}"
;
// Format JSON data into tabular form
SendMessage
tabularResponse
=
formatJsonToTable
(
jsonData
);
try
{
execute
(
tabularResponse
);
// Add the chat ID to the set of processed chat IDs
processedChatIds
.
add
(
chatId
);
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
();
}
return
;
// Exit the method to prevent sending duplicate responses
}
}
else
{
response
.
setText
(
"You say: "
+
text
);
}
// Send the response
try
{
execute
(
response
);
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
();
}
}
}
@Override
public
String
getBotUsername
()
{
return
"Telegmtestingbot"
;
}
@Override
public
String
getBotToken
()
{
return
"7171570219:AAGkJ9wQEJyJ1f2f2y_eTPQWEaWrn-6fLf0"
;
}
public
static
void
main
(
String
[]
args
)
{
MyTelegramBot
bot
=
new
MyTelegramBot
();
try
{
TelegramBotsApi
botsApi
=
new
TelegramBotsApi
(
DefaultBotSession
.
class
);
botsApi
.
registerBot
(
bot
);
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
();
}
}
// Format JSON data into tabular form
public
SendMessage
formatJsonToTable
(
String
jsonData
)
{
SendMessage
response
=
new
SendMessage
();
response
.
setParseMode
(
"HTML"
);
// Parse JSON string into Java objects
Gson
gson
=
new
Gson
();
Data
data
=
gson
.
fromJson
(
jsonData
,
Data
.
class
);
// Format data into a tabular structure
StringBuilder
table
=
new
StringBuilder
();
table
.
append
(
"<b>Table:</b>\n\n"
);
table
.
append
(
"<pre>"
);
table
.
append
(
"Intents | Invocation\n"
);
table
.
append
(
"--------|-------\n"
);
for
(
Row
row
:
data
.
getData
())
{
table
.
append
(
String
.
format
(
"%-8s| %-6d\n"
,
row
.
getName
(),
row
.
getNumber
()));
}
table
.
append
(
"</pre>"
);
// Set the tabular data as the message text
response
.
setText
(
table
.
toString
());
return
response
;
}
// Define classes to represent JSON data
static
class
Data
{
private
List
<
Row
>
data
;
public
List
<
Row
>
getData
()
{
return
data
;
}
public
void
setData
(
List
<
Row
>
data
)
{
this
.
data
=
data
;
}
}
static
class
Row
{
private
String
Name
;
private
int
Number
;
public
String
getName
()
{
return
Name
;
}
public
void
setName
(
String
name
)
{
Name
=
name
;
}
public
int
getNumber
()
{
return
Number
;
}
public
void
setNumber
(
int
number
)
{
Number
=
number
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment