Reputation: 239
I am trying to get the previous Message. First the User ist sending Message: "Search Student". Then the Bot replies on that message with: "Enter the name of student". Then the user has to write something and after that I would like to call a specific function.
Here is my code:
switch (message.getText()){
case "Search Student":
sendMsg(message, "Enter the name of Stunde: ");
//And somewhere here i would like to call a function that is searching for a student (I have
//already created the function)
break;
}
Upvotes: 1
Views: 1811
Reputation: 239
if(message.getReplyToMessage().getText().contains("Enter the name of Schueler:")){
List<Schueler> foundSchueler = search.searchSchueler(message.getText(), schueler);
String foundSchuelerStr = "Found Schueler: \n\n";
for (Schueler s: foundSchueler) {
foundSchuelerStr += s.toString();
}
sendMsg(message, foundSchuelerStr);
}
This is my solution, and it works fantastic! You simply have to check if the user had replied on message with a specific text
Upvotes: 0
Reputation: 171
I have also faced such a problem recently and I have recreated this method. But I am not sure if this solution is a good one! I also believe that there is no very specific answer to this question.
The idea is to write the last action of the user/student/profile to the database by opening one table called
telegram_action
table including following columns:
telegram action {
actionName: String; // your case, the action is "Search Student"
messageId: Integer; // message Id that you want to as previous one
}
Your changed code:
switch (message.getText()){
case "Search Student":
sendMsg(message, "Enter the name of Stunde: ");
//And somewhere here i would like to call a function that is searching for a student (I have
//already created the function)
createTelegramUserAction("Search Student", message.getMessageId());
break;
default:
//I didn't implement this method, idea is simply choose from TelegramAction database
//that ACTIVE STATUS or not deleted one(there will be only one) with action name "Search Student";
// Then you will have previous **mesageId** and you will know this message is action of Searching student
// every time you create new action, `createTelegramAction function` will delete you previous actions, don't worry!
actionCheck(message);
}
So, I created one createTelegramUserAction()
method to save last action as following:
public void createTelegramAction(String actionName, Integer messageId, Long chatId) {
// Disable or Delete all previous actions from database before creating new one!!!
disableAllActions("Search Students");
// create action that Student wants search, in your case actionName = "Search Student";
// save this action to telegram_action table, **didn't implemented**
create(actionName, messageId);
}
public void disableAllActions(String actionName) {
// Note that I have chosen to disable the status in here, however you can simply delete all actions
// In my case, it was necessary to save last action
// In Deleting case you don't need ActionStatus enum
// ActionStatus is enum with values {ACTIVE and INACTIVE}
List<TelegramAction> telegramActions = telegramActionRepository
.findAllByActionNameAndUserActionStatus(actionName, ActionStatus.ACTIVE);
for (TelegramAction telegramAction : telegramActions) {
// here you can either delete the actions or make INACTIVE.
telegramAction.setActionStatus(ActionStatus.INACTIVE);
telegramActionRepository.save(telegramAction);
}
}
Note that this createTelegramAction
method can be used for other methods as a generic method. But there is always space for improving the idea to make it more generic functions
Upvotes: 3