use of org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction in project spring-bot by finos.
the class InRoomAddressingChecker method filter.
public Action filter(Action a) {
if (a.getAddressable() instanceof User) {
// direct message to bot
return a;
}
if (a instanceof SimpleMessageAction) {
SimpleMessageAction sma = (SimpleMessageAction) a;
Optional<User> firstUserMention = sma.getMessage().getNth(User.class, 0);
if ((firstUserMention.isPresent()) && (theBot.matches(firstUserMention.get()))) {
// bot is mentioned, so return the action, stripping out the bot mention
Message changedMessage = (Message) ((SimpleMessageAction) a).getMessage().removeAtStart(firstUserMention.get());
return new SimpleMessageAction(a.getAddressable(), a.getUser(), changedMessage, sma.getData());
}
Optional<Word> firstWord = sma.getMessage().getNth(Word.class, 0);
if (allowSlash && firstWord.isPresent() && firstWord.get().getText().startsWith("/")) {
// we don't actually remove the slash - words will match anyway.
return a;
}
return null;
} else {
// forms, everything else - let them through
return a;
}
}
use of org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction in project spring-bot by finos.
the class TimeFinderIT method applyTest.
@SuppressWarnings("unchecked")
@Test
public void applyTest() {
try {
SimpleMessageAction action = getAction();
lenient().when(history.getLastFromHistory(Mockito.any(Class.class), Mockito.any(Addressable.class))).thenReturn(reminderList());
timefinder.initializingStanfordProperties();
timefinder.accept(action);
ArgumentCaptor<Response> args = ArgumentCaptor.forClass(Response.class);
Mockito.verify(responseHandlers).accept(args.capture());
Assertions.assertEquals(args.getAllValues().size(), 1);
WorkResponse fr = (WorkResponse) args.getValue();
Reminder r = (Reminder) fr.getFormObject();
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
Assertions.assertEquals(r.getLocalTime(), LocalDateTime.of(year, month + 1, day, 21, 20, 0));
} catch (OutOfMemoryError e) {
// see this on our own machines
return;
}
}
use of org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction in project spring-bot by finos.
the class TimeFinderIT method getAction.
private SimpleMessageAction getAction() {
SimpleMessageAction simpleMessageAction = new SimpleMessageAction(getAddressable(), getUser(), getMessage(), null);
Action.CURRENT_ACTION.set(simpleMessageAction);
return simpleMessageAction;
}
use of org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction in project spring-bot by finos.
the class ChatRequestChatHandlerMapping method createMappingRegistration.
@Override
protected MappingRegistration<ChatRequest> createMappingRegistration(ChatRequest mapping, ChatHandlerMethod handlerMethod) {
List<WildcardContent> wildcards = createWildcardContent(mapping, handlerMethod);
List<MessageMatcher> matchers = createMessageMatchers(mapping, wildcards);
return new MappingRegistration<ChatRequest>(mapping, handlerMethod) {
@Override
public ChatHandlerExecutor getExecutor(Action a) {
if (a instanceof SimpleMessageAction) {
if (!canBePerformedHere((SimpleMessageAction) a)) {
return null;
}
return matchesSimpleMessageAction((SimpleMessageAction) a);
}
if (a instanceof FormAction) {
if (Objects.nonNull(((FormAction) a).getData().get("form")) && ((FormAction) a).getData().get("form").getClass() != HelpPage.class) {
return null;
}
if (!canBePerformedHere((FormAction) a)) {
return null;
}
return matchesFormAction((FormAction) a);
}
return null;
}
private boolean canBePerformedHere(Action a) {
ChatRequest cb = getMapping();
return canBePerformed(a.getAddressable(), a.getUser(), cb);
}
private ChatHandlerExecutor matchesSimpleMessageAction(SimpleMessageAction a) {
return pathMatches(a.getMessage(), a);
}
private ChatHandlerExecutor matchesFormAction(FormAction a) {
return pathMatches(Message.of(Word.build(a.getAction())), a);
}
private ChatHandlerExecutor pathMatches(Message words, Action a) {
MappingRegistration<?> me = this;
ChatHandlerExecutor bestMatch = null;
for (MessageMatcher messageMatcher : matchers) {
Map<ChatVariable, Object> map = new HashMap<>();
if (messageMatcher.consume(words, map)) {
if ((bestMatch == null) || (bestMatch.getReplacements().size() < map.size())) {
bestMatch = new AbstractHandlerExecutor(wrf, rh, converters) {
@Override
public Map<ChatVariable, Object> getReplacements() {
return map;
}
@Override
public Action action() {
return a;
}
@Override
public ChatMapping<?> getOriginatingMapping() {
return me;
}
};
}
}
}
return bestMatch;
}
@Override
public boolean isButtonFor(Object o, WorkMode m) {
return false;
}
};
}
use of org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction in project spring-bot by finos.
the class TimeFinder method accept.
@Override
public void accept(Action t) {
try {
if (t instanceof SimpleMessageAction) {
Message m = ((SimpleMessageAction) t).getMessage();
User currentUser = t.getUser();
Addressable a = t.getAddressable();
String messageInString = m.getText();
CoreDocument document = new CoreDocument(messageInString);
stanfordCoreNLP.annotate(document);
for (CoreEntityMention cem : document.entityMentions()) {
System.out.println("temporal expression: " + cem.text());
System.out.println("temporal value: " + cem.coreMap().get(TimeAnnotations.TimexAnnotation.class));
Timex timex = cem.coreMap().get(TimeAnnotations.TimexAnnotation.class);
LocalDateTime ldt = toLocalTime(timex);
if (ldt != null) {
Optional<ReminderList> rl = h.getLastFromHistory(ReminderList.class, a);
int remindBefore;
if (rl.isPresent()) {
remindBefore = rl.get().getRemindBefore();
} else {
remindBefore = reminderProperties.getDefaultRemindBefore();
}
ldt = ldt.minus(remindBefore, ChronoUnit.MINUTES);
Reminder reminder = new Reminder();
reminder.setDescription(messageInString);
reminder.setLocalTime(ldt);
reminder.setAuthor(currentUser);
WorkResponse wr = new WorkResponse(a, reminder, WorkMode.EDIT);
rh.accept(wr);
}
}
}
} catch (Exception e) {
errorHandler.handleError(e);
}
}
Aggregations