Search in sources :

Example 1 with Template

use of org.finos.symphony.webhookbot.domain.Template in project spring-bot by finos.

the class ReceiveController method receiveWebhook.

@PostMapping(path = "/hook/{streamId}/{hookId}")
public ResponseEntity<Void> receiveWebhook(@PathVariable(name = "streamId") String streamId, @PathVariable(name = "hookId") String hookId, @RequestBody JsonNode body) throws JsonProcessingException {
    Room r = rooms.loadRoomById(streamId);
    ActiveWebHooks ho = history.getLastFromHistory(ActiveWebHooks.class, r).orElse(new ActiveWebHooks());
    WebHook hook = getHook(ho, hookId);
    if ((hook != null) && (hook.isActive())) {
        // ok, we've found the webhook for this call.
        Template template = hook.getTemplate();
        if (template == null) {
            template = createDefaultTemplate(body, hook.getDisplayName());
            hook.setTemplate(template);
        }
        EntityJson out = createEntityJson(body, ho, hook);
        MessageResponse mr = new MessageResponse(wf, r, out, "", "", template.getContents());
        handler.accept(mr);
        return new ResponseEntity<Void>(HttpStatus.OK);
    }
    throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
Also used : ActiveWebHooks(org.finos.symphony.webhookbot.domain.ActiveWebHooks) ResponseEntity(org.springframework.http.ResponseEntity) EntityJson(org.finos.symphony.toolkit.json.EntityJson) MessageResponse(org.finos.symphony.toolkit.workflow.response.MessageResponse) WebHook(org.finos.symphony.webhookbot.domain.WebHook) Room(org.finos.symphony.toolkit.workflow.content.Room) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Template(org.finos.symphony.webhookbot.domain.Template) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Template

use of org.finos.symphony.webhookbot.domain.Template in project spring-bot by finos.

the class ReceiveController method createDefaultTemplate.

private Template createDefaultTemplate(JsonNode body, String title) {
    List<String> fields = new ArrayList<String>();
    String prefix = "entity.payload.contents";
    addSomeFields(prefix, body, fields);
    String fieldsTemplate = fields.stream().map(n -> "\n<tr><td><span class=\"tempo-text-color--secondary\">" + n.substring(prefix.length()) + "</span></td>\n" + "<td><span class=\"tempo-bg-color--red tempo-text-color--white tempo-token\">${" + n + "}</span></td></tr>\n").reduce("", String::concat);
    Template out = new Template();
    out.setName(title + " Default Template");
    out.setContents("<table>" + fieldsTemplate + "</table>" + buttonForm());
    out.setShared(false);
    return out;
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Template(org.finos.symphony.webhookbot.domain.Template) WebHook(org.finos.symphony.webhookbot.domain.WebHook) SymphonyRooms(org.finos.symphony.toolkit.workflow.sources.symphony.room.SymphonyRooms) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Controller(org.springframework.stereotype.Controller) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestBody(org.springframework.web.bind.annotation.RequestBody) Map(java.util.Map) EntityJsonConverter(org.finos.symphony.toolkit.workflow.sources.symphony.handlers.EntityJsonConverter) JsonNode(com.fasterxml.jackson.databind.JsonNode) HeaderDetails(org.finos.symphony.toolkit.workflow.form.HeaderDetails) PostMapping(org.springframework.web.bind.annotation.PostMapping) Workflow(org.finos.symphony.toolkit.workflow.Workflow) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Iterator(java.util.Iterator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Room(org.finos.symphony.toolkit.workflow.content.Room) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HashTag(org.finos.symphony.toolkit.workflow.content.HashTag) TextNode(com.fasterxml.jackson.databind.node.TextNode) History(org.finos.symphony.toolkit.workflow.history.History) WebhookPayload(org.finos.symphony.webhookbot.domain.WebhookPayload) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MessageResponse(org.finos.symphony.toolkit.workflow.response.MessageResponse) ResponseHandler(org.finos.symphony.toolkit.workflow.sources.symphony.handlers.ResponseHandler) EntityJson(org.finos.symphony.toolkit.json.EntityJson) TagSupport(org.finos.symphony.toolkit.workflow.sources.symphony.TagSupport) ActiveWebHooks(org.finos.symphony.webhookbot.domain.ActiveWebHooks) Entry(java.util.Map.Entry) ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) Template(org.finos.symphony.webhookbot.domain.Template)

Example 3 with Template

use of org.finos.symphony.webhookbot.domain.Template in project spring-bot by finos.

the class UpdateTemplateConsumer method apply.

@Override
public List<Response> apply(ElementsAction t) {
    if (t.getAction().equals(WebHookOps.TEMPLATE_UPDATE)) {
        try {
            Template template = (Template) t.getFormData();
            EntityJson workflow = t.getData();
            ActiveWebHooks awh = (ActiveWebHooks) workflow.get(EntityJsonConverter.WORKFLOW_001);
            WebHook active = (WebHook) workflow.get(ReceiveController.INVOKED_WEBHOOK);
            WebHook wh = ReceiveController.getHook(awh, active.getHookId().getId());
            wh.setTemplate(template);
            WebhookPayload payload = (WebhookPayload) workflow.get(ReceiveController.PAYLOAD);
            EntityJson out = ReceiveController.createEntityJson(payload.getContents(), awh, wh);
            MessageResponse mr = new MessageResponse(t.getWorkflow(), t.getAddressable(), out, "", "", template.getContents());
            return Collections.singletonList(mr);
        } catch (JsonProcessingException e) {
            return Collections.singletonList(new ErrorResponse(t.getWorkflow(), t.getAddressable(), e.getMessage()));
        }
    } else {
        return Collections.emptyList();
    }
}
Also used : ActiveWebHooks(org.finos.symphony.webhookbot.domain.ActiveWebHooks) EntityJson(org.finos.symphony.toolkit.json.EntityJson) WebhookPayload(org.finos.symphony.webhookbot.domain.WebhookPayload) MessageResponse(org.finos.symphony.toolkit.workflow.response.MessageResponse) WebHook(org.finos.symphony.webhookbot.domain.WebHook) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Template(org.finos.symphony.webhookbot.domain.Template) ErrorResponse(org.finos.symphony.toolkit.workflow.response.ErrorResponse)

Aggregations

EntityJson (org.finos.symphony.toolkit.json.EntityJson)3 MessageResponse (org.finos.symphony.toolkit.workflow.response.MessageResponse)3 ActiveWebHooks (org.finos.symphony.webhookbot.domain.ActiveWebHooks)3 Template (org.finos.symphony.webhookbot.domain.Template)3 WebHook (org.finos.symphony.webhookbot.domain.WebHook)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Room (org.finos.symphony.toolkit.workflow.content.Room)2 WebhookPayload (org.finos.symphony.webhookbot.domain.WebhookPayload)2 ResponseEntity (org.springframework.http.ResponseEntity)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ResponseStatusException (org.springframework.web.server.ResponseStatusException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1