-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor provider hierarchy (refs xatkit-bot-platform/xatkit-runtime#221
) ChatPlatform now contains 2 providers that extend the ones from the runtime component: - ChatIntentProvider that can be extended by providers based on third-party libraries (e.g. Slack) - JsonWebhookChatIntentProvider that can be extended by providers receiving messages as JSON payloads
- Loading branch information
Showing
3 changed files
with
101 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
runtime/src/main/java/com/xatkit/plugins/chat/platform/io/ChatProviderUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}. | ||
* <p> | ||
* 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<String, Object> 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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
runtime/src/main/java/com/xatkit/plugins/chat/platform/io/JsonWebhookChatIntentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
* <p> | ||
* 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 <T> the concrete {@link ChatPlatform} that contains this provider | ||
* @see ChatIntentProvider | ||
*/ | ||
public abstract class JsonWebhookChatIntentProvider<T extends ChatPlatform> extends JsonWebhookEventProvider<T> { | ||
|
||
/** | ||
* 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} | ||
* <p> | ||
* 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); | ||
} | ||
} |