Search code examples
php-curlmattermost

Mattermost incoming webhooks error, "Unable to parse incoming data"


Judging by the amount of information out there this does not seem like a common way to execute mattermost webhooks. We have a mattermost installation, we have an older web application used to lookup inventory, we would like to be able to via webhook send a message to a team containing some information relating to a part search request.

I almost thought I could get away with making an ajax post request to the webhook URL with the "payload" set as a param, but this returns 400 "Unable To Parse Incoming Data", I thought for whatever reason the ajax request was the issue so I created a PHP script to do the curl request, this is what I have;

<?php

$payload = json_encode($_REQUEST['payload']);

$ch = curl_init('http://dev2:8065/hooks/6isjcohwyibsf8kp5g9p6bcgoa');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, "$payload");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

echo $result;

Any suggestion at this point would be worthwhile. This either can't be done for whatever reason or it's so trivial I'm over thinking it.


payload sample,

The payload being sent:

'payload={"text": "| Component  | Tests Run   | Tests Failed |
|:-----------|:------------|:-----------------------------------------------|
| Server     | 948         | :white_check_mark: 0|
| Web Client | 123         | :warning: [2 (see details)(http://linktologs) |
| iOS Client | 78          | :warning: [3 (see details)](http://linktologs) |"}'

This entire string is being sent in the request.


Works with this,

If I break up the payload inside the php like so:

$body = '| Component  | Tests Run   | Tests Failed |\n|:-----------|:------------|:---------------------------------------------|\n| Server     | 948         | :white_check_mark: 0                         |\n| Web Client | 123         | :warning: [2 (see details)](http://linktologs) |\n| iOS Client | 78          | :warning: [3 (see details)](http://linktologs) |\n';

$payload = 'payload={"text": "';
$payload .= $body;
$payload .= '"}';

It works as expected.


Solution

  • The Solution -

    The actual string for the text request needs to have /n delimiters.


    Once I was able to get one request to work I was then able to refactor all the way to using the jQ's ajax request to send a payload to the webhook. I am still not 100% on the why or the hows.

    This is the working JS:

    var body = 'payload={"text":"| Component  | Tests Run   | Tests Failed |\n' +
    '|:-----------|:------------|:---------------------------------------------|\n'+
    '| Server     | 948         | :white_check_mark: 0                         |\n' +
    '| Web Client | 123         | :warning: [2 (see details)](http://linktologs) |\n' +
    '| iOS Client | 78          | :warning: [3 (see details)](http://linktologs) |\n"}';
    
    $.ajax({
      type: 'POST',
      url: "http://dev2:8065/hooks/6isjcohwyibsf8kp5g9p6bcgoa",
      data: body
    });
    

    super simple, and what I was originally trying to acheive.