Search in sources :

Example 1 with Message

use of org.finos.symphony.toolkit.workflow.content.Message 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;
    }
}
Also used : User(org.finos.symphony.toolkit.workflow.content.User) Word(org.finos.symphony.toolkit.workflow.content.Word) Message(org.finos.symphony.toolkit.workflow.content.Message) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction)

Example 2 with Message

use of org.finos.symphony.toolkit.workflow.content.Message 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;
        }
    };
}
Also used : Action(org.finos.symphony.toolkit.workflow.actions.Action) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) Message(org.finos.symphony.toolkit.workflow.content.Message) HashMap(java.util.HashMap) ChatVariable(org.finos.symphony.toolkit.workflow.annotations.ChatVariable) HelpPage(org.finos.symphony.toolkit.workflow.help.HelpPage) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) ChatRequest(org.finos.symphony.toolkit.workflow.annotations.ChatRequest) HashMap(java.util.HashMap) Map(java.util.Map) WorkMode(org.finos.symphony.toolkit.workflow.annotations.WorkMode)

Example 3 with Message

use of org.finos.symphony.toolkit.workflow.content.Message 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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) User(org.finos.symphony.toolkit.workflow.content.User) Message(org.finos.symphony.toolkit.workflow.content.Message) CoreDocument(edu.stanford.nlp.pipeline.CoreDocument) ParseException(java.text.ParseException) CoreEntityMention(edu.stanford.nlp.pipeline.CoreEntityMention) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) Addressable(org.finos.symphony.toolkit.workflow.content.Addressable) TimeAnnotations(edu.stanford.nlp.time.TimeAnnotations) Timex(edu.stanford.nlp.time.Timex) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse)

Example 4 with Message

use of org.finos.symphony.toolkit.workflow.content.Message in project spring-bot by finos.

the class TestContents method testContents.

@Test
public void testContents() {
    // tag def
    doAssertsOnContent(new CashTag("id123"), new CashTag("id123"));
    doAssertsOnContent(new HashTag("id123"), new HashTag("id123"));
    // room def
    doAssertsOnObject(new SymphonyRoom("abc", "abc123"), new SymphonyRoom("abc", "abc123"));
    doAssertsOnObject(new SymphonyRoom(null, "abc123"), new SymphonyRoom(null, "abc123"));
    doAssertsOnObject(new SymphonyRoom("abc", "abc123"), new SymphonyRoom("abc", "abc123"));
    doAssertsOnObject(new SymphonyRoom("abc", null), new SymphonyRoom("abc", null));
    // user def
    doAssertsOnObject(new SymphonyUser(123l, "rob", "rob@example.com"), new SymphonyUser(123l, "rob", "rob@example.com"));
    doAssertsOnObject(new SymphonyUser("rob", "rob@example.com"), new SymphonyUser("rob", "rob@example.com"));
    doAssertsOnObject(new SymphonyUser(null, "rob@example.com"), new SymphonyUser(null, "rob@example.com"));
    doAssertsOnObject(new SymphonyUser(123l, "rob", null), new SymphonyUser(123l, "rob", null));
    // id
    UUID some = UUID.randomUUID();
    doAssertsOnContent(HashTag.createID(some), HashTag.createID(some));
    // wordx
    Word w1 = Word.of("hello");
    Word w2 = Word.of("bye");
    doAssertsOnContent(w1, Word.of("hello"));
    // paragraph
    Paragraph p1 = Paragraph.of(Arrays.asList(w1, w2));
    Paragraph p2 = Paragraph.of(Arrays.asList(w1, w2));
    doAssertsOnContent(p1, p2);
    doAssertsOnContent(p1.getNth(Word.class, 0).get(), p2.getNth(Word.class, 0).get());
    // message
    Message m1 = Message.of(Arrays.asList(p1, p2));
    Message m2 = Message.of(Arrays.asList(p1, p2));
    doAssertsOnContent(m1, m2);
}
Also used : Word(org.finos.symphony.toolkit.workflow.content.Word) Message(org.finos.symphony.toolkit.workflow.content.Message) HashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag) CashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.CashTag) UUID(java.util.UUID) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) SymphonyRoom(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom) Paragraph(org.finos.symphony.toolkit.workflow.content.Paragraph) Test(org.junit.jupiter.api.Test)

Example 5 with Message

use of org.finos.symphony.toolkit.workflow.content.Message in project spring-bot by finos.

the class TestHandlerMapping method execute.

private void execute(String s) throws Exception {
    EntityJson jsonObjects = new EntityJson();
    jsonObjects.put("1", new SymphonyUser(123l, "gaurav", "gaurav@example.com"));
    jsonObjects.put("2", new Taxonomy(Arrays.asList(new HashTag("SomeTopic"))));
    Message m = smp.parse("<messageML>/" + s + "</messageML>", jsonObjects);
    Chat r = new SymphonyRoom("The Room Where It Happened", "abc123");
    User author = new SymphonyUser(ROB_EXAMPLE_ID, ROB_NAME, ROB_EXAMPLE_EMAIL);
    Action a = new SimpleMessageAction(r, author, m, jsonObjects);
    Action.CURRENT_ACTION.set(a);
    mc.accept(a);
}
Also used : Action(org.finos.symphony.toolkit.workflow.actions.Action) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) EntityJson(org.finos.symphony.toolkit.json.EntityJson) User(org.finos.symphony.toolkit.workflow.content.User) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) Message(org.finos.symphony.toolkit.workflow.content.Message) Taxonomy(org.symphonyoss.Taxonomy) HashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag) Chat(org.finos.symphony.toolkit.workflow.content.Chat) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) SymphonyRoom(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom)

Aggregations

Message (org.finos.symphony.toolkit.workflow.content.Message)11 SimpleMessageAction (org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction)6 EntityJson (org.finos.symphony.toolkit.json.EntityJson)4 User (org.finos.symphony.toolkit.workflow.content.User)4 Word (org.finos.symphony.toolkit.workflow.content.Word)4 SymphonyUser (org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser)4 Test (org.junit.jupiter.api.Test)4 Action (org.finos.symphony.toolkit.workflow.actions.Action)3 FormAction (org.finos.symphony.toolkit.workflow.actions.FormAction)3 Paragraph (org.finos.symphony.toolkit.workflow.content.Paragraph)3 HashTag (org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag)3 Addressable (org.finos.symphony.toolkit.workflow.content.Addressable)2 CashTag (org.finos.symphony.toolkit.workflow.sources.symphony.content.CashTag)2 SymphonyRoom (org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom)2 Taxonomy (org.symphonyoss.Taxonomy)2 V4MessageSent (com.symphony.api.model.V4MessageSent)1 CoreDocument (edu.stanford.nlp.pipeline.CoreDocument)1 CoreEntityMention (edu.stanford.nlp.pipeline.CoreEntityMention)1 TimeAnnotations (edu.stanford.nlp.time.TimeAnnotations)1 Timex (edu.stanford.nlp.time.Timex)1