Handle sessions in a custom package

You can extract a session using a session name from the SessionsMap. In the same package, you can use sessions to pass information between actions, but not to other packages.

Configure the following attributes

  • Apply an annotation to the class field and add a corresponding public setter.
  • Retrieve the SessionsMap using the Sessions attribute.
  • Set the variable type as Map<String, Object>.
  • Set the attributeType: TEXT or SESSION. If type = TEXT, it is a regular session; if type = SESSION, it is a sharedSession.

Start a new session

@BotCommand
@CommandPkg(label = "Start session", name = "startSession", description = "Start new session", 
icon = "pkg.svg", node_label = "start session {{sessionName}}|") public class Start {
 
    @Sessions
    private Map<String, Object> sessions;
     
    @Execute
    public void start(@Idx(index = "1", type = TEXT) @Pkg(label = "Session name", 
    default_value_type = STRING, default_value = "Default") @NotEmpty String sessionName) {
 
        // Check for existing session
        if (sessions.containsKey(sessionName))
            throw new BotCommandException(MESSAGES.getString("xml.SessionNameInUse", sessionName));
 
        // Do some operation
 
        // Create new session
        sessions.put(sessionName, new Session(operation));
    }
    public void setSessions(Map<String, Object> sessions) {
        this.sessions = sessions;
    }
}

End a session

@BotCommand
@CommandPkg(label = "End session", name = "endSession", description = "End session", icon = 
"pkg.svg", node_label = "End session {{sessionName}}|")
public class EndSession {
    @Sessions
    private Map<String, Object> sessions;
    @Execute
    public void end(
            @Idx(index = "1", type = TEXT) @Pkg(label = "Session name", default_value_type = STRING, 
            default_value = "Default") @NotEmpty String sessionName) {
                 
        sessions.remove(sessionName);   
    }  
    public void setSessions(Map<String, Object> sessions) {
        this.sessions = sessions;
    }
}

Add a session

In the below example, string objects are used (although in real actions, you can use complex objects).

@BotCommand
@CommandPkg(label = "[[SessionDemo.label]]", description = "[[SessionDemo.description]]", icon = "sample.svg", name = "sessionDemo")
public class SessionDemo {

	// Sessions are provided as a Map. Actions can add or remove entries in this
	// Map.
	// The choice to reuse/overwrite/delete/add any Object in this Map belongs to
	// the actions, and the framework makes no assumption regarding it.
	@Sessions
	private Map<String, Object> sessionMap;

	@Execute
	public void execute(@Idx(index = "1", type = TEXT)
	@Pkg(label = "[[SessionDemo.name.label]]")
	@NotEmpty
	String name) {
		if (!sessionMap.containsKey(name))
			sessionMap.put(name, "Some Value");
	}

	// Ensure that a public setter exists.
	public void setSessionMap(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
}

Set a session variable

The below example shows how to develop an action (for example, Set session variable). In the action, the session details are retrieved and stored as a session variable so that the information can be used within a specific package.

@BotCommand
@CommandPkg(label = "[[SessionDemo2.label]]", description = "[[SessionDemo2.description]]", icon = "sample.svg", name = "SessionDemo2",
return_type = DataType.STRING)
public class SessionDemo2 {

	// Sessions are provided as a Map. Actions can add or remove entries in this
	// Map.
	// The choice to reuse/overwrite/delete/add any Object in this Map belongs to
	// the actions, and the framework makes no assumption regarding it.
	@Sessions
	private Map<String, Object> sessionMap;

	@Execute
	public Value<?> execute(@Idx(index = "1", type = TEXT)
	@Pkg(label = "[[SessionDemo2.name.label]]")
	@NotEmpty
	String name) {
		if (sessionMap.containsKey(name)) {
			return new StringValue(sessionMap.get(name));
		}	
		return new StringValue("session does not exist");
	}
	// Ensure that a public setter exists.
	public void setSessionMap(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
}