Trello - How do i get comment and username from "New Comment Added to Card (Instant)"?

How can i edit the new-comment-added-to-card.mjs to include the comment text and the username of the user who made the comment?

I want to use it for sending a message to Discord, and i don’t see either in the test event data. I don’t understand why this isn’t something that’s already included in the data…

new-comment-added-to-card.mjs

import common from "../common/common-webhook.mjs";

export default {
  ...common,
  key: "trello-new-comment-added-to-card",
  name: "New Comment Added to Card (Instant)",
  description: "Emit new event for each new comment added to a card.",
  version: "0.0.10",
  type: "source",
  dedupe: "unique",
  props: {
    ...common.props,
    board: {
      propDefinition: [
        common.props.trello,
        "board",
      ],
    },
    cards: {
      propDefinition: [
        common.props.trello,
        "cards",
        (c) => ({
          board: c.board,
        }),
      ],
    },
  },
  hooks: {
    ...common.hooks,
    async deploy() {
      const {
        sampleEvents, sortField,
      } = await this.getSampleEvents();
      sampleEvents.sort((a, b) => (Date.parse(a[sortField]) > Date.parse(b[sortField]))
        ? 1
        : -1);
      for (const action of sampleEvents.slice(-25)) {
        this._setComment(action?.data?.text);
        this.emitEvent(action);
      }
    },
  },
  methods: {
    ...common.methods,
    async getSampleEvents() {
      const cards = this.cards && this.cards.length > 0
        ? this.cards
        : (await this.trello.getCards(this.board)).map((card) => card.id);
      const actions = [];
      for (const card of cards) {
        const activities = await this.trello.getCardActivity(card, "commentCard");
        actions.push(...activities);
      }
      return {
        sampleEvents: actions,
        sortField: "date",
      };
    },
    _getComment() {
      return this.db.get("comment");
    },
    _setComment(comment) {
      this.db.set("comment", comment);
    },
    isCorrectEventType(event) {
      const eventType = event.body?.action?.type;
      return eventType === "commentCard";
    },
    async getResult(event) {
      const cardId = event.body?.action?.data?.card?.id;
      const comment = event.body?.action?.data?.text;
      /** Record comment to use in generateMeta() */
      this._setComment(comment);
      return this.trello.getCard(cardId);
    },
    isRelevant({ result: card }) {
      return (
        (!this.board || this.board === card.idBoard) &&
        (!this.cards || this.cards.length === 0 || this.cards.includes(card.id))
      );
    },
    generateMeta({
      id, dateLastActivity,
    }) {
      const comment = this._getComment();
      return {
        id: `${id}${dateLastActivity}`,
        summary: comment,
        ts: Date.parse(dateLastActivity),
      };
    },
  },
};

Thanks

Hello @jpsweeney94, thank you so much for raising this issue! I’ve created a Github issue here to improve this and added it into Pipedream’s prioritized backlog. Pipedream component dev will take a look at it soon!

1 Like

Thanks a lot @vunguyenhung !

While we’re on the subject… The Trello source “New Card (Instant)” data does not include the list it was added to.

Also… is there any easy to add a timeout on these event triggers? Or can you guys make a version of those events/sources with a couple minute delay? Trello has its own automation as far as moving cards, changing labels etc… so the data can sometimes be incorrect or missing info since it sends it instantly.

@jpsweeney94,

Trello doesn’t include this in their event by default so Pipedream doesn’t have it. I think you need to add another Get List action with the idList in the event to get the list name. Here’s my example

On top of my mind there’s no easy way to do this, maybe @pierce from Pipedream team could have some hints

1 Like

Thanks a lot for that example! I missed that. Appreciate the active responses both here and the GitHub Issues!