Advertisement
tam0705

Help Line Chat bot

Jun 25th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. //I'm making a chat bot for LINE (it's a social media) and in my source code lies a problem
  2. //There are a lot of functions, so I will just mention the associated ones.
  3. //TOP OF MY CLASS
  4.     private String profileName = "";
  5.  
  6.     @Autowired
  7.     private LineMessagingClient lineMessagingClient; //@Autowired and LineMessagingClient are from LINE libraries
  8.  
  9.     ... //Many functions
  10.  
  11.     //This function will be called automatically when a user sends a message to my bot
  12.     private void handleTextContent(String replyToken, Event event, TextMessageContent content) {
  13.         String text = content.getText();
  14.         String userId = event.getSource().getUserId();
  15.  
  16.         //I want my bot to reply the profile name of the user when their message meets certain keywords
  17.         //This is one of the keyword criterias
  18.         if (text.equals("Hi!")) {
  19.             updateProfileName(replyToken, userId); //So I get the profile name first
  20.             String reply = profileName + ",hi!";
  21.             this.reply(replyToken, new TextMessage(reply)); //And then I tell the bot to reply
  22.         }
  23.     }
  24.  
  25.     private void updateProfileName(String replyToken, String userId) throws Exception {
  26.         profileName = "Unknown"; //Sometimes userId can be null if the user doesn't agree to LINE Terms, so firstly I set it to Unknown
  27.         //I copied the * part from internet, so I don't really know how it works
  28.         //I also don't really understand how synchronized works
  29.         synchronized (lineMessagingClient) {
  30.             try {
  31.                 if (userId != null) {
  32.                     //Start * part
  33.                     lineMessagingClient
  34.                         .getProfile(userId)
  35.                         .whenComplete((profile, throwable) -> {
  36.                             //What I know with this lambda is that it is slow in getting the username
  37.                             //So I use the wait & notify I found on the internet to make
  38.                             //this function finished after the username has been gained successfully
  39.                             if (throwable != null) {
  40.                                 this.reply(replyToken, new TextMessage(throwable.getMessage()));
  41.                                 return;
  42.                             }
  43.                             profileName = profile.getDisplayName();
  44.                             lineMessagingClient.notifyAll(); //The & part
  45.                         });
  46.                     //End * part
  47.                      lineMessagingClient.wait(); //The ^ part
  48.                 }  
  49.             } catch (Exception e) {
  50.                 System.out.println("EXCEPTION!");
  51.             }
  52.         }
  53.     }
  54.    
  55.     //Now the problem is that the & part fails to stop the ^ part
  56.     //In other words when I send "Hi!" to my bot there is no further response
  57.     //Before I added wait & notify my bot responded, but it responded "Unknown, hi!" instead of my name and hi
  58.     //I'm sure I misunderstood how wait & notify works, I researched in the internet but didn't really understand
  59.     //Is there any way to fix this so that my bot works as how I want?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement