Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取上一条消息(使用JAVA的电报机器人)_Java_Telegram Bot - Fatal编程技术网

如何获取上一条消息(使用JAVA的电报机器人)

如何获取上一条消息(使用JAVA的电报机器人),java,telegram-bot,Java,Telegram Bot,我正试图得到上一条信息。首先,用户正在发送消息:Search Student。然后机器人回复该消息:输入学生姓名。然后用户必须写一些东西,然后我想调用一个特定的函数 这是我的密码: 我最近也遇到过这样的问题,我重新创建了这种方法。但我不确定这个解决方案是否好!我还认为,这个问题没有非常具体的答案 其思想是通过打开一个名为 电报行动表,包括以下列: telegram action { actionName: String; // your case, the action is "Searc

我正试图得到上一条信息。首先,用户正在发送消息:Search Student。然后机器人回复该消息:输入学生姓名。然后用户必须写一些东西,然后我想调用一个特定的函数

这是我的密码:


我最近也遇到过这样的问题,我重新创建了这种方法。但我不确定这个解决方案是否好!我还认为,这个问题没有非常具体的答案

其思想是通过打开一个名为 电报行动表,包括以下列:

telegram action {
  actionName: String; // your case, the action is "Search Student"
  messageId: Integer; // message Id that you want to as previous one
} 
您更改的代码:

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);

}
因此,我创建了一个createTelegramUserAction方法来保存最后一个操作,如下所示:

 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);
        }
}

注意,这个createTelegramAction方法可以作为泛型方法用于其他方法。但总有改进的空间,使其更通用的功能

我最近也遇到了这样的问题,我重新创建了这种方法。但我不确定这个解决方案是否好!我还认为,这个问题没有非常具体的答案

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);
    }
其思想是通过打开一个名为 电报行动表,包括以下列:

telegram action {
  actionName: String; // your case, the action is "Search Student"
  messageId: Integer; // message Id that you want to as previous one
} 
您更改的代码:

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);

}
因此,我创建了一个createTelegramUserAction方法来保存最后一个操作,如下所示:

 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);
        }
}
注意,这个createTelegramAction方法可以作为泛型方法用于其他方法。但总有改进的空间,使其更通用的功能

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);
    }
这是我的解决方案,效果非常好! 您只需检查用户是否用特定文本回复了消息

这是我的解决方案,效果非常好!
您只需检查用户是否在邮件中回复了特定文本

谢谢,我将尝试这样实现谢谢,我将尝试这样实现