Search Intercom Users 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 Intercom users that have matching names and posts user 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.

../_images/create-a-pipeline-button.png

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.

../_images/slack-new-message.png

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.

../_images/slack-new-message-pipe.png

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.

../_images/slack-new-message-pipe-filter.png

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:”

../_images/slack-new-message-pipe-filter2.png

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.

../_images/regular-expression.png

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.

../_images/regular-expression-dnd.png

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 :

../_images/regular-expression2.png

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 Intercom and post back to Slack.

Searching Users in Intercom

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

../_images/intercom-search-users.png

In the “Search Users” select the Intercom account that you want to use. If needed connect a new one. Then click the Add Filter button (blue plus icon) and select Name.

../_images/intercom-search-users2.png

In the filter value textbox drag and drop the term field form the previous pipe.

../_images/intercom-search-users3.png

Having “Search Users” configured like this will search for user records in Intercom that have their Name property match our term search field.

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.

../_images/intercom-foreach.png

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”.

../_images/intercom-slack-post1.png

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 they support.

../_images/intercom-slack-post2.png

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

*name: {{c:name}}*
 *email:* {{c:email}}
 *created:* {{c:signed_up_at}}
 *last updated:* {{c:updated_at}}
 *social:* {% for sprofile in c.social_profiles %}
     *{{sprofile.name}}* - _{{sprofile.username}}_
 {% else %}
     _no social profiles_
 {% endfor %}
*-----------------------------------------*

As you can see we’ve used many fields from Intercom’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.

Conclusion

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 Intercom by using Slack to search for Users and query user 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!