diff --git a/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatIntentProvider.java b/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatIntentProvider.java index c0f8f68..ba7ac90 100644 --- a/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatIntentProvider.java +++ b/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatIntentProvider.java @@ -1,26 +1,24 @@ package com.xatkit.plugins.chat.platform.io; -import com.xatkit.core.platform.io.RuntimeIntentProvider; +import com.xatkit.core.platform.io.RuntimeEventProvider; import com.xatkit.core.session.XatkitSession; import com.xatkit.intent.EventInstance; import com.xatkit.plugins.chat.ChatUtils; import com.xatkit.plugins.chat.platform.ChatPlatform; import org.apache.commons.configuration2.Configuration; -import java.util.Map; - -import static fr.inria.atlanmod.commons.Preconditions.checkState; -import static java.util.Objects.nonNull; - /** - * An abstract chat {@link RuntimeIntentProvider}. + * An abstract chat intent provider. *

- * This class is extended by provider of platforms extending the {@link ChatPlatform}, and ensures that all the - * chat-related context variables have been set before triggering actions associated to the computed event. + * This class is designed to be extended by concrete providers relying on third-party libraries to receive chat + * events (e.g. a dedicated library connecting to Slack). To receive chat events from Json webhooks check + * {@link JsonWebhookChatIntentProvider}. * * @param the concrete {@link ChatPlatform} that contains this provider + * + * @see JsonWebhookChatIntentProvider */ -public abstract class ChatIntentProvider extends RuntimeIntentProvider { +public abstract class ChatIntentProvider extends RuntimeEventProvider { /** * Constructs a new {@link ChatIntentProvider} with the provided {@code runtimePlatform} and {@code configuration}. @@ -41,19 +39,10 @@ public ChatIntentProvider(T runtimePlatform, Configuration configuration) { */ @Override public void sendEventInstance(EventInstance eventInstance, XatkitSession session) { - Map contextVariables = - session.getRuntimeContexts().getContextVariables(ChatUtils.CHAT_CONTEXT_KEY); - checkState(nonNull(contextVariables), "Intent provider %s did not define the context %s", - this.getClass().getSimpleName(), ChatUtils.CHAT_CONTEXT_KEY); - checkState(nonNull(contextVariables.get(ChatUtils.CHAT_CHANNEL_CONTEXT_KEY)), "Intent provider %s did not " + - "define the context variable %s.%s", this.getClass().getSimpleName(), - ChatUtils.CHAT_CONTEXT_KEY, - ChatUtils.CHAT_CHANNEL_CONTEXT_KEY); - checkState(nonNull(contextVariables.get(ChatUtils.CHAT_USERNAME_CONTEXT_KEY)), "Intent provider %s did not " + - "define the context variable %s.%s", this.getClass().getSimpleName(), - ChatUtils.CHAT_USERNAME_CONTEXT_KEY); - checkState(nonNull(contextVariables.get(ChatUtils.CHAT_RAW_MESSAGE_CONTEXT_KEY)), "Intent provider %s did not" + - " define the context variable %s.%s", this.getClass().getSimpleName()); + /* + * TODO should we set this override as final? + */ + ChatProviderUtils.checkSession(session); super.sendEventInstance(eventInstance, session); } } diff --git a/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatProviderUtils.java b/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatProviderUtils.java new file mode 100644 index 0000000..6960a6d --- /dev/null +++ b/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatProviderUtils.java @@ -0,0 +1,41 @@ +package com.xatkit.plugins.chat.platform.io; + +import com.xatkit.core.session.XatkitSession; +import com.xatkit.plugins.chat.ChatUtils; + +import java.util.Map; + +import static fr.inria.atlanmod.commons.Preconditions.checkState; +import static java.util.Objects.nonNull; + +/** + * Provides utility methods to check session contents. + */ +public class ChatProviderUtils { + + /** + * Checks that the context variables defined in {@link ChatUtils} have been set in the given {@code session}. + *

+ * This method is called when sending the event to the {@link com.xatkit.core.XatkitCore} component, and ensures + * that each concrete implementation of the {@link com.xatkit.plugins.chat.platform.ChatPlatform} sets the same + * context variables. + * + * @param session the {@link XatkitSession} to check + * @throws IllegalStateException if a context or context variable has not been set in the session + */ + protected static void checkSession(XatkitSession session) { + Map contextVariables = + session.getRuntimeContexts().getContextVariables(ChatUtils.CHAT_CONTEXT_KEY); + checkState(nonNull(contextVariables), "The context %s has not been defined by the intent provider", + ChatUtils.CHAT_CONTEXT_KEY); + checkState(nonNull(contextVariables.get(ChatUtils.CHAT_CHANNEL_CONTEXT_KEY)), "The context variable %s" + + ".%s has not been defined by the intent provider", ChatUtils.CHAT_CONTEXT_KEY, + ChatUtils.CHAT_CHANNEL_CONTEXT_KEY); + checkState(nonNull(contextVariables.get(ChatUtils.CHAT_USERNAME_CONTEXT_KEY)), "The context variable %s" + + ".%s has not been defined by the intent provider", + ChatUtils.CHAT_CONTEXT_KEY, ChatUtils.CHAT_USERNAME_CONTEXT_KEY); + checkState(nonNull(contextVariables.get(ChatUtils.CHAT_RAW_MESSAGE_CONTEXT_KEY)), "The context variable %s" + + ".%s has not been defined by the intent provider", ChatUtils.CHAT_CONTEXT_KEY, + ChatUtils.CHAT_RAW_MESSAGE_CONTEXT_KEY); + } +} diff --git a/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/JsonWebhookChatIntentProvider.java b/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/JsonWebhookChatIntentProvider.java new file mode 100644 index 0000000..f63d26b --- /dev/null +++ b/runtime/src/main/java/com/xatkit/plugins/chat/platform/io/JsonWebhookChatIntentProvider.java @@ -0,0 +1,48 @@ +package com.xatkit.plugins.chat.platform.io; + +import com.xatkit.core.platform.io.JsonWebhookEventProvider; +import com.xatkit.core.session.XatkitSession; +import com.xatkit.intent.EventInstance; +import com.xatkit.plugins.chat.ChatUtils; +import com.xatkit.plugins.chat.platform.ChatPlatform; +import org.apache.commons.configuration2.Configuration; + +/** + * An abstract chat intent provider that extracts intents from received Json payloads. + *

+ * This class is designed to be extended by concrete providers extracting user intents from received Json payloads. + * The class provides utility methods to navigate the Json payload and the received HTTP headers. To receive chat + * from third-party libraries check and {@link ChatIntentProvider}. + * + * @param the concrete {@link ChatPlatform} that contains this provider + * @see ChatIntentProvider + */ +public abstract class JsonWebhookChatIntentProvider extends JsonWebhookEventProvider { + + /** + * Constructs a new {@link JsonWebhookChatIntentProvider} with the provided {@code runtimePlatform} and {@code + * configuration}. + * + * @param runtimePlatform the {@link ChatPlatform} containing this provider + * @param configuration the {@link Configuration} used to initialize this provider + */ + public JsonWebhookChatIntentProvider(T runtimePlatform, Configuration configuration) { + super(runtimePlatform, configuration); + } + + /** + * {@inheritDoc} + *

+ * This method checks that the context variables defined in {@link ChatUtils} have been initialized by the + * concrete provider. This ensures that all the providers extending {@link ChatIntentProvider} set the same set + * of variables and can be used transparently in execution models. + */ + @Override + public void sendEventInstance(EventInstance eventInstance, XatkitSession session) { + /* + * TODO should we set this override as final? + */ + ChatProviderUtils.checkSession(session); + super.sendEventInstance(eventInstance, session); + } +}