Gérer les sessions dans un package personnalisé

Vous pouvez extraire une session à l'aide d'un nom de session à partir de SessionsMap. Dans le même package, vous pouvez utiliser des sessions pour transmettre des informations entre les actions, mais pas à d'autres packages.

Configurer les attributs suivants

  • Appliquez une annotation au champ de la classe et ajoutez un Set Public correspondant.
  • Récupérez SessionsMap en utilisant l'attribut Sessions.
  • Définissez le type de variable comme Map<String, Object>.
  • Définissez attributeType : TEXT ou SESSION. Si type = TEXT, il s'agit d'une session normale ; si type = SESSION, il s'agit d'une sharedSession.

Démarrer une nouvelle 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;
    }
}

Terminer une 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;
    }
}

Ajouter une session

Dans l'exemple ci-dessous, des objets de chaîne sont utilisés (bien que dans les actions réelles, vous puissiez utiliser des objets complexes).

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

Définir une variable de session

L'exemple ci-dessous montre comment développer une action (par exemple, Set session variable). Dans l'action, les détails de la session sont récupérés et stockés comme une variable de session afin que les informations puissent être utilisées dans un package spécifique.

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