Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //I'm making a chat bot for LINE (it's a social media) and in my source code lies a problem
- //There are a lot of functions, so I will just mention the associated ones.
- //TOP OF MY CLASS
- private String profileName = "";
- @Autowired
- private LineMessagingClient lineMessagingClient; //@Autowired and LineMessagingClient are from LINE libraries
- ... //Many functions
- //This function will be called automatically when a user sends a message to my bot
- private void handleTextContent(String replyToken, Event event, TextMessageContent content) {
- String text = content.getText();
- String userId = event.getSource().getUserId();
- //I want my bot to reply the profile name of the user when their message meets certain keywords
- //This is one of the keyword criterias
- if (text.equals("Hi!")) {
- updateProfileName(replyToken, userId); //So I get the profile name first
- String reply = profileName + ",hi!";
- this.reply(replyToken, new TextMessage(reply)); //And then I tell the bot to reply
- }
- }
- private void updateProfileName(String replyToken, String userId) throws Exception {
- profileName = "Unknown"; //Sometimes userId can be null if the user doesn't agree to LINE Terms, so firstly I set it to Unknown
- //I copied the * part from internet, so I don't really know how it works
- //I also don't really understand how synchronized works
- synchronized (lineMessagingClient) {
- try {
- if (userId != null) {
- //Start * part
- lineMessagingClient
- .getProfile(userId)
- .whenComplete((profile, throwable) -> {
- //What I know with this lambda is that it is slow in getting the username
- //So I use the wait & notify I found on the internet to make
- //this function finished after the username has been gained successfully
- if (throwable != null) {
- this.reply(replyToken, new TextMessage(throwable.getMessage()));
- return;
- }
- profileName = profile.getDisplayName();
- lineMessagingClient.notifyAll(); //The & part
- });
- //End * part
- lineMessagingClient.wait(); //The ^ part
- }
- } catch (Exception e) {
- System.out.println("EXCEPTION!");
- }
- }
- }
- //Now the problem is that the & part fails to stop the ^ part
- //In other words when I send "Hi!" to my bot there is no further response
- //Before I added wait & notify my bot responded, but it responded "Unknown, hi!" instead of my name and hi
- //I'm sure I misunderstood how wait & notify works, I researched in the internet but didn't really understand
- //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