Best answer

Issue with characters in custom webhook

  • 25 August 2021
  • 5 replies
  • 1646 views

Userlevel 1

 

Hi Community,

 

I am trying to post a messagecard to a teams incoming webhook. I am able to get this to work in general however occasionally, because I am using some data from a “Catch Hook” it has characters that cause the app to return a "Bad payload received by generic incoming webhook.". As mentioned, if I change the field to some manual input text it works fine. This leads me to believe its some like ‘character escaping’? If so, I am just trying to get some guidance on how I can escape characters inside a field from a previous step?

 

App: Webhooks by Zapier
Action: Custom Request
Method: Post
URL: https://subdomain.webhook.office.com/webhookb2/webhookguidhere
Data Pass-Thorugh: Undefined (Defaults to no)
Unflatten: Yes
Headers: Content-Type: application/json
Data:

{
    "@type": "MessageCard",
    "@context": "http://schema.org/extensions",
    "themeColor": "{{123456789__priority__color}}",
    "summary": "New {{123456789__template__name}} Request",
    "title": "[ID: {{123456789__id}}] {{123456789__template__name}}",
    "color": "{{123456789__priority__color}}",
    "sections": [{
        "activityTitle": "{{123456789__title}}",
        "activitySubtitle": "{{123456789__change_requester__name}} ({{123456789__change_requester__department__name}})",
        "activityImage": "https://sub.somedomain.com/images/icons/{{123456789__change_type__name}}.png",
        "activityText":"**{{123456789__reason_for_change__name}}:** {{123456789__short_description}}",
        "facts": [{
            "name": "Implementer",
            "value": "{{123456789__change_owner__name}}"
        },    {
            "name": "Department",
            "value": "{{123456789__group__name}}"
        },    {
            "name": "Start Time",
            "value": "{{123456789__scheduled_start_time__display_value}}"
        }, {
            "name": "End Time",
            "value": "{{123456789__scheduled_end_time__display_value}}"
        }],
    },
    {
      "activityTitle": "Roll Out Plan",
      "activitySubtitle": "@{{123456789__roll_out_plan__updated_by__name}} - {{123456789__roll_out_plan__updated_on__display_value}}",
      "activityText":"{{123456789__roll_out_plan__description}}",
    },
    {
      "activityTitle": "Backout Plan",
      "activitySubtitle": "@{{123456789__back_out_plan__updated_by__name}} - {{123456789__back_out_plan__updated_on__display_value}}",
      "activityText":"{{123456789__back_out_plan__description}}",
    }],
    "potentialAction": [{
        "@type": "OpenUri",
        "name": "View Change Online",
        "targets": [{
            "os": "default",
            "uri": "https://sub.somedomain.com/ChangeDetails.do?CHANGEID={{123456789__id}}"
        }]
    }]
}

 

 

What normally happens is my service desk sends some JSON to the Step1: Catch Hook as shown below in Request D.

 

The Step 2: Custom Webhook then Posts to the teams channel and makes a nice pretty message card.

 

…. that all works fine…. until one of the fields ie: short_description has a few characters in it. as shown in this next caught hook, Request F.

 

Then without change the above JSON data to teams in Step2, the following displays

 

 

icon

Best answer by daryl_HBL 27 August 2021, 04:29

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

Userlevel 7
Badge +14

Hi @daryl_HBL 

Issue looks to be with this field:

 

Check out the Formatter app: https://zapier.com/help/doc/how-use-formatter-functions

Try using replace to replace   with a [:space:].

Userlevel 1

Hi @daryl_HBL 

Issue looks to be with this field:

 

Check out the Formatter app: https://zapier.com/help/doc/how-use-formatter-functions

Try using replace to replace   with a [:space:].

 

Thanks Troy, that space was actually me just blanking that part of the screenshot because it had a real URL in it.

That image URL part works when the other fields like “1. Short Description” has no / or #’s in it.

Userlevel 7
Badge +14

@daryl_HBL

Try using Formatter > Text > Replace: https://zapier.com/help/doc/how-use-formatter-functions#using-replace

OR

You can use a Code step with the replace() function: https://www.w3schools.com/jsref/jsref_replace.asp

Code: https://zapier.com/apps/code/help

Userlevel 1

I actually solved this a slightly different way using “Code by Zapier”

Using Code by Zapier as a step after my hook, I then ran an action event “Run Python”

Using the input data fields I wanted to ‘sanitize’ before sending to the teams webhook, 

 

string1 = input_data['shortDesc']
string2 = input_data['rolloutDesc']
string3 = input_data['backoutDesc']
for ch in ['\\','`','*','{','}','"','+','!','$','\'']:
    if ch in string1:
        string1=string1.replace(ch,"\\"+ch)
    if ch in string2:
        string2=string2.replace(ch,"\\"+ch)
    if ch in string3:
        string3=string3.replace(ch,"\\"+ch) 
return {'shortDesc': string1, 'rolloutDesc': string2, 'backoutDesc': string3}

 

Then in the teams post webhook, I just referenced the above outputs of the santization step and all was well.

 

 

Userlevel 7
Badge +11

Glad to hear you were able to sort this using a Code by Zapier step. Thank you so much for sharing your solution here, @daryl_HBL! :)