Search in sources :

Example 1 with V4SymphonyElementsAction

use of com.symphony.api.model.V4SymphonyElementsAction in project spring-bot by finos.

the class ElementsHandler method accept.

@SuppressWarnings("unchecked")
@Override
public void accept(V4Event t) {
    try {
        V4SymphonyElementsAction action = t.getPayload().getSymphonyElementsAction();
        if (action != null) {
            Map<String, Object> formValues = (Map<String, Object>) action.getFormValues();
            String verb = (String) formValues.get("action");
            String formId = action.getFormId();
            Object currentForm;
            if (hasFormData(formValues)) {
                currentForm = formConverter.convert(formValues, formId);
            } else {
                currentForm = null;
            }
            EntityJson data = retrieveData(action.getFormMessageId());
            Addressable rr = ruBuilder.loadRoomById(action.getStream().getStreamId());
            User u = ruBuilder.loadUserById(t.getInitiator().getUser().getUserId());
            // if we're not in a room, address the user directly.
            rr = rr == null ? u : rr;
            Errors e = ErrorHelp.createErrorHolder();
            if (validated(currentForm, e)) {
                FormAction ea = new FormAction(rr, u, currentForm, verb, data);
                try {
                    Action.CURRENT_ACTION.set(ea);
                    for (ActionConsumer c : elementsConsumers) {
                        try {
                            c.accept(ea);
                        } catch (Exception ee) {
                            LOG.error("Failed to handle consumer " + c, ee);
                        }
                    }
                } finally {
                    Action.CURRENT_ACTION.set(Action.NULL_ACTION);
                }
            } else {
                WorkResponse fr = new WorkResponse(rr, currentForm, WorkMode.EDIT, ButtonList.of(new Button(verb, Button.Type.ACTION, "Retry")), convertErrorsToMap(e));
                rh.accept(fr);
            }
        }
    } catch (Exception e) {
        LOG.error("Couldn't handle event " + t, e);
    }
}
Also used : EntityJson(org.finos.symphony.toolkit.json.EntityJson) User(org.finos.symphony.toolkit.workflow.content.User) ActionConsumer(org.finos.symphony.toolkit.workflow.actions.consumers.ActionConsumer) Errors(org.springframework.validation.Errors) Button(org.finos.symphony.toolkit.workflow.form.Button) V4SymphonyElementsAction(com.symphony.api.model.V4SymphonyElementsAction) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) Addressable(org.finos.symphony.toolkit.workflow.content.Addressable) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse) Map(java.util.Map) ErrorMap(org.finos.symphony.toolkit.workflow.form.ErrorMap)

Example 2 with V4SymphonyElementsAction

use of com.symphony.api.model.V4SymphonyElementsAction in project spring-bot by finos.

the class BotIT method testPressButton.

/**
 * When the user presses a button, we return a response.
 */
@Test
public void testPressButton() {
    V4Event in = new V4Event().initiator(new V4Initiator().user(new V4User().email("rob@example.com").displayName("Rob Example").userId(2438923l))).payload(new V4Payload().symphonyElementsAction(new V4SymphonyElementsAction().formId("koreai-choice").formValues(Collections.singletonMap("action", "some button")).stream(new V4Stream().streamId("ABC123"))));
    getPublicBot().sendToConsumer(in);
    littleSleep();
    wireMockRule.verify(1, WireMock.postRequestedFor(urlPathMatching("/agent/v4/stream/ABC123/message/create")));
}
Also used : V4SymphonyElementsAction(com.symphony.api.model.V4SymphonyElementsAction) V4Initiator(com.symphony.api.model.V4Initiator) V4Event(com.symphony.api.model.V4Event) V4User(com.symphony.api.model.V4User) V4Stream(com.symphony.api.model.V4Stream) V4Payload(com.symphony.api.model.V4Payload) Test(org.junit.jupiter.api.Test)

Example 3 with V4SymphonyElementsAction

use of com.symphony.api.model.V4SymphonyElementsAction in project spring-bot by finos.

the class KoreAIEventHandler method accept.

@Override
public void accept(V4Event t) {
    try {
        V4MessageSent ms = t.getPayload().getMessageSent();
        V4User u = t.getInitiator().getUser();
        if (ms != null) {
            V4Stream stream = ms.getMessage().getStream();
            String text = extractText(ms);
            EntityJson ej = parseEntityJson(ms.getMessage().getData());
            if (!u.getEmail().equals(botIdentity.getEmail()) && (isAddressed(stream, ej, text))) {
                try {
                    Address a = buildAddress(u, stream);
                    text = normalizeText(text);
                    requester.send(a, text);
                } catch (Exception e) {
                    LOG.error("Couldn't handle message {}", ms);
                }
            }
        }
        // handle form submits
        V4SymphonyElementsAction elements = t.getPayload().getSymphonyElementsAction();
        if (elements != null) {
            String formId = elements.getFormId();
            if (formId.equals("koreai-choice")) {
                @SuppressWarnings("unchecked") String button = ((Map<String, String>) elements.getFormValues()).get("action");
                try {
                    Address a = buildAddress(u, elements.getStream());
                    requester.send(a, button);
                } catch (Exception e) {
                    LOG.error("Couldn't handle form submission {}", ms);
                }
            }
        }
    } catch (Exception e) {
        LOG.error("Couldn't handle stream event " + t, e);
    }
}
Also used : EntityJson(org.finos.symphony.toolkit.json.EntityJson) Address(org.finos.symphony.toolkit.koreai.Address) V4SymphonyElementsAction(com.symphony.api.model.V4SymphonyElementsAction) V4MessageSent(com.symphony.api.model.V4MessageSent) V4User(com.symphony.api.model.V4User) V4Stream(com.symphony.api.model.V4Stream) Map(java.util.Map) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

V4SymphonyElementsAction (com.symphony.api.model.V4SymphonyElementsAction)3 V4Stream (com.symphony.api.model.V4Stream)2 V4User (com.symphony.api.model.V4User)2 Map (java.util.Map)2 EntityJson (org.finos.symphony.toolkit.json.EntityJson)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 V4Event (com.symphony.api.model.V4Event)1 V4Initiator (com.symphony.api.model.V4Initiator)1 V4MessageSent (com.symphony.api.model.V4MessageSent)1 V4Payload (com.symphony.api.model.V4Payload)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Address (org.finos.symphony.toolkit.koreai.Address)1 FormAction (org.finos.symphony.toolkit.workflow.actions.FormAction)1 ActionConsumer (org.finos.symphony.toolkit.workflow.actions.consumers.ActionConsumer)1 Addressable (org.finos.symphony.toolkit.workflow.content.Addressable)1 User (org.finos.symphony.toolkit.workflow.content.User)1 Button (org.finos.symphony.toolkit.workflow.form.Button)1 ErrorMap (org.finos.symphony.toolkit.workflow.form.ErrorMap)1 WorkResponse (org.finos.symphony.toolkit.workflow.response.WorkResponse)1