[JAVA] Conversation status (context) management (session management?) With LINE BOT

Thing you want to do

I want to create a LINE BOT that has something like conversation status (context) on the server side (like a web application session?) And the response changes accordingly.

The image looks like this. image.png

Natural language analysis, Deep Learning, RNN, chat BOT, etc. are not advanced stories.

Realization method

I decided to create a map with the user ID as the key and the class that collects the values to be saved as the value.

In addition, the implementation of the part that responds to the message is sample-spring-boot-kitchensink I referred to (replyText method). The language is Java.

In fact, if there is a more royal road method (≒ library, framework), please let us know.

Class to save status

Status.java


public class Status {

	private String context = "0";//status
	private String place = "";//place

	public String getContext() {
		return context;
	}
	public void setContext(String context) {
		this.context = context;
	}

	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}

}

map

PseudoSession.java


public class PseudoSession {

	private static Map<String,Status> statusMap = new HashMap<>();

	public static Status getStatus(String userId) {
		return PseudoSession.statusMap.get(userId);
	}

	public static void putStatus(String userId, Status status) {
		PseudoSession.statusMap.put(userId, status);
	}

	public static String readContext(String userId) {
		return PseudoSession.statusMap.get(userId).getContext();
	}

	public static String readPlace(String userId) {
		return PseudoSession.statusMap.get(userId).getPlace();
	}

	public static void updateContext(String userId, String context) {
		Status newStatus = PseudoSession.statusMap.get(userId);
		newStatus.setContext(context);
		PseudoSession.statusMap.put(userId, newStatus);
	}

	public static void updatePlace(String userId, String place) {
		Status newStatus = PseudoSession.statusMap.get(userId);
		newStatus.setPlace(place);
		PseudoSession.statusMap.put(userId, newStatus);
	}

}

Main processing

Controller.java


	private void handleTextContent(String replyToken, Event event, TextMessageContent content)
			throws Exception {
		final String text = content.getText();
		final String userId = event.getSource().getUserId();

        //For first-time users, enter a value in map
		if (Objects.isNull(PseudoSession.getStatus(userId))) {
			PseudoSession.putStatus(userId, new Status());
		}

		if(PseudoSession.readContext(userId).equals("0")) {

			if (text.equals("Tell me the weather")) {
				this.replyText(replyToken,"Where's the weather?");
				PseudoSession.updateContext(userId,"1");
			} else {
				this.replyText(replyToken,"Enter "Tell me the weather"!");
			}

		}else if (PseudoSession.readContext(userId).equals("1")) {

			if (!("Toyama".equals(text))&&!("Ishikawa".equals(text))) {
				this.replyText(replyToken,"I only know Toyama or Ishikawa");
			} else {
				this.replyText(replyToken,"When is the weather?");
				PseudoSession.updatePlace(userId,text);
				PseudoSession.updateContext(userId,"2");
			}

		}else if (PseudoSession.readContext(userId).equals("2")) {

			if (!("today".equals(text))&&!("tomorrow".equals(text))) {
				this.replyText(
						replyToken,"I only know today or tomorrow");
			} else {
				String answer = text + "of"
						+ PseudoSession.readPlace(userId) + "of"
						+ "The weather is sunny.";
				this.replyText(replyToken,answer);
				PseudoSession.updatePlace(userId,"");
				PseudoSession.updateContext(userId,"0");
			}
		}
	}

result

image.png

Recommended Posts

Conversation status (context) management (session management?) With LINE BOT
[Rails] Push transmission with LINE Bot
line bot
I made a LINE bot with Rails + heroku
Easy to make LINE BOT with Java Servlet
Deploy Line bot with rails5 + Docker + Heroku Note
Let's make a LINE Bot with Ruby + Sinatra --Part 2
Let's make a LINE Bot with Ruby + Sinatra --Part 1