Query Trello Cards Using a Command in Slack

In this tutorial we are going to build a pipeline that listens on new messages in Slack that start with “search:”. It then searches Trello Cards that have matching names and posts selected information back to the Slack channel.

How to do it

Let’s start by creating a new, blank pipeline by clicking the Create a Pipeline button on the dashboard.


The first pipe that needs to be added to the pipeline is a trigger, that will fire whenever a new message is posted to Slack. We need to locate Slack in the channels list on the right, then drag and drop the “New Message” trigger.


After dropping the pipe in the first slot of the pipeline, select your account (or connect a new one if needed), then select the Slack channel you are interested in monitoring.


Here we want ot act on Slack messages that start with “search:” so we need to add a filter to the pipe. Filters make sure that only events that match will fire the trigger and execution of the pipeline will continue. Filters are supported on pipes that have the Add filter button near the end (blue plus icon)

Adding a Filter

Click the Add filter button (blue plus icon) and select Text as the field to filter on.


We are only interested in messages that start with “search:”, so in the filter condition drop-down select starts with and in the filter value enter “search:”. This will make sure that the trigger will fire only on messages that start with “search:”


Now, since the New Message pipe outputs the entire message that was sent to Slack, we need to split it into two parts - “search:” which we’ll call “command” and the string that was searched for, which we call “term”.

Splitting Text With a Regular Expression

In order to do that we will use a Regular Expression. Regular Expressions (also called “regexes”) are extremely flexible and powerful and allow you to do all kinds of magic with text. In our example we are going to use one to split the message we have from Slack into two parts – the “command” used (“search” in this example) and the search term.

Locate the “Apply Regular Expression” pipe under Built-in channels -> Text and add it as the next step in the pipeline.


Apply Regular Expression has only two parameters – Text, the text to apply the regular expression on, and Regular Expression, the regex to apply. Since we are interested in splitting the Slack message from the fields list drag and drop Text.


In the Regular Expression textbox enter: (?P<command>.*)\:\s(?P<term>.*)

This will make sure that in the pipe output we will have two named fields - command and term. We consider all that’s before the colon in the Slack message to be “command” and any characters after to be “term”.

Note: For a reference of the supported syntax please refer to this document. You can also use Pythex to test your regular expression.

After you are done the “Apply Regular Expression” pipe should look like this :


At this point we now have the message that was posted in Slack split in command and term which are available in the output of “Apply Regular Expression”. Note that because of the filter that we added in the “New Message” trigger the command will always be “search”.

So now all that’s left is to search Trello and post back to Slack.

Searching Cards in Trello

Select the All Channels tab on the channels list and locate Trello. Note that you can also use the text-box above to search by name for speedier access. Then add the Search Cards pipe as the next step in the pipeline :


In the Search Cards pipe select the Trello account that you want to use (If needed connect a new one) and the Board to search in. Then click the “Add Filter” button (blue “plus” icon) and select Name.


From the filter condition drop-down select contains then in the filter value textbox drag and drop the “term” field form the previous pipe.


Having Search Cards configured like this will search for Cards in the Board you selected in Trello that contain our term search field in their Name property.

The last thing we need to do is to post back to Slack the results we’ve found.

Posting to Slack

Adding a Search pipe automatically adds a for-each iteration block after it. Using this we can act on each individual result found.


So now all we need to do is add a “Post Message” action pipe from Slack. In the channels list locate Slack. Drag the “Post Message” pipe and put it in the slot after “DO”.


You can now select a specific channel in your Slack that you want the results posted to. Alternatively drag and drop ID from the list of fields to the right after expanding the first list of fields (the one from the “A” pipe).

From the fields list drag and drop the fields you want posted to Slack in response to our query. Note that since we are posting to Slack we can make use of the markdown-like formatting Slack supports.


For our example in the Text textbox we are going to enter this:

*name: {{c:name}}*
 *description:* {{c:description}}
 *due:* {{c:due_date}}
 *list:* {{c:list.name}}
 *labels:* {% for label in c.labels %}
     *{{label.name}}* - _{{label.color}}_
 {% else %}
     no labels
 {% endfor %}

As you can see we’ve added many fields from Trello’s result and used Slack formatting. We also made use of advanced field operations, but that’s for another tutorial and another time.

One optional step we can take is to add one more “Post Message” pipe to the very end of the pipeline that simply says “End List”, so even for the cases when the search returns no results you will get a note in Slack and know that it was an empty list.


This is all there is to it really. In this brief tutorial we’ve covered all that’s needed to build quite a sophisticated pipeline that automates your workflow for Trello by using Slack to search for Cards and query data. We’ve used a regular expression to correctly split “commands” in Slack and also filters to only act on commands we are interested in.

Should you need any assistance, as usual just drop us a line at support@cloudpipes.com or reach us in the in-app chat.

Happy integrating with Cloudpipes!