Structuring Marketo webhooks to post via Slack API

This is part 4 in a series about integrating Marketo with Slack.

Part 1: Basic Marketo & Slack integration using Incoming Webhooks
Part 2: Posting via Marketo using the Slack API vs Incoming Webhooks
Part 3: Configuring a Slack app for use with Marketo
Part 5: Sending data back to Marketo from Slack

This specific setup has taken many webhooks to achieve an optimal setup. Usually when one new Slack webhook is made, it gives thoughts to other use cases, requiring new webhooks, which scales poorly.

Now, how do we push over something like the graphic above from Marketo to Slack in a format that Slack will accept that is also scaleable to be used in multiple scenarios

Structuring JSON Payload

This takes into account that you have the prerequisites of (1) a Slack app created, (2) a bot user within the app invited to the channel you will post to, (3) Authorization header with Bearer token added to your Marketo webhook.

The basic elements in a POST

  • URL to your Slack workspace with /api/chat.postMessageappended to the end (basic use case will be posting to Slack
  • channel – Channel ID for the channel you are posting to. This can easily be found by accessing Slack channel via a browser and grabbing the last string in the URL.
  • as_user set to true – the bot user will be posting to the channel.
  • attachments – used for adding parameters in the post as an array
  • fallbackused for adding a fallback text which will usually show in the toast notification on desktop – I prefer to set this the same as the pretext/title.
  • pretext – Used as the title of the post (the text above everything else.
  • fields – This is another array that basically allows you to add both short and long text fields to a post. Short being 2-column and long being 1-column. In this case I have just added the lead token for a custom field called Slack_Contact Details which I am rewriting through flow steps before posting.
  • color is just defining the color of the edge of the attachments and is not really important.

The post in JSON format

Breaking down the elements

As you can see from the above example, this presents malformed JSON as {{lead.Slack_Contact Details}} is not denoted within quotes. The reason for this is that the fields are added through a series of flow steps which assign values based on fields that are existing and always:

  1. Start with a field in quotes (for example something I know will exist {"title": "Email","value": "{{lead.Email Address}}", "short": true},)
  2. Uses comma separators in between fields and
  3. Ends with values that will always be attached without a comma at the end to close the JSON array.

Using “My Tokens” for Channel and Title

In order to make this easier and more scalable (after having 20 different webhooks), I figured it’s easier to have the title of the webhook and the channel it is posted to defined on the program level in Marketo as this always for recycling the webhook in other situations.

Assigning existing field values via Marketo flow steps

Marketo knows best what data exists and when we have a template webhook we can decide exactly what goes into Slack without making a gigantic post that has empty/default value fields.

The field Slack_Contact Details (can be named whatever you want) runs through flow steps to:

  1. NULL the value
  2. Add a starting value
  3. Attach other fields (both short/long) with constraints that the fields are not empty
  4. Add an ending value for closing the JSON array.

All this is done by appending the new value to the existing value of the field using the token {{lead.Slack_Contact Details}} without spaces:

Once all the values have been assigned, Marketo will post this over to Slack in the final step.