Hello All,
I had a heck of a time getting started on this so hopefully this is helpful to someone…
A couple notes:
I put double brackets {{}} around everything you’d want to change/update for your specific use
This probably won’t work as-is because its a bastardized version of my real script so its certainly not plug-and-play.
I know my formatting is weird.
the PHP below should be put on your server at the URI you specify in the Webhook on Monday when setting it up.
<?php
//return monday challenge
echo file_get_contents('php://input');
// capture second response which should be JSON payload
$php_event = json_decode(file_get_contents('php://input'), true) 'event'];
// init
ini_set("log_errors", 1);
ini_set('error_reporting', E_ALL);
ini_set("error_log", "monday.log"); // this is an empty file you should create to monitor/debug your webhook
// a function to dump large arrays etc to the log
function error_log_dump( $object=null ){
ob_start(); // start buffer capture
var_dump( $object ); // dump the values
$buffer_contents = ob_get_contents(); // put the buffer into a variable
ob_end_clean(); // end capture
error_log("-------------------------\\n".print_r($buffer_contents, true), 3, "monday.log");
}
// function to make queries and mutaions via Monday API v2
function ping_api($query){
$data = @file_get_contents(
"https://api.monday.com/v2/",
false,
stream_context_create(
>
'http' => =
'method' => 'POST',
'header' => =
'Content-Type: application/json',
'User-Agent: gMYTEAM] GraphQL Client',
'Authorization: {{YOUR TOKEN HERE}}'
],
'content' => json_encode($query)
]
]
)
);
// decode the response back to PHP
$response = json_decode(
$data,
true
);
// error reporting
if(is_null($response)){
error_log("query error: no response from server");
exit();
} elseif (isset($responses"errors"])) {
error_log("query error: ".$responses"errors"]r0]s"message"]);
exit();
} elseif (isset($responses"data"])) {
//error_log("query success");
return $response;
}
//error_log_dump($response);
}
// debug response
error_log_dump($php_event);
if($php_event_'boardId'] == "{{ID OF CERTAIN BOARD YOU WANT TO WATCH - DON'T FORGET TO GIVE THE BOARD A WEBHOOK AUTOMATION IN MONDAY}}"){
if($php_event_'type'] == "update_column_value"){ // change was an update (not a new item - that's below)
// code here
// here is something I found useful using "watch arrays" to monitor only certain columns
// create an array of column ids to watch
$watch_array = r"{{ID OF FIRST_COLUMN YOU WANT TO WATCH}}", "{{ID OF FIRST_COLUMN YOU WANT TO WATCH}}", "{{AND DO ON}}"];
if(in_array($php_event_'columnId'], $watch_array)){
// get more info about the item that changed
$item_that_changed_query = e"query" =>
"
{
items(ids: (".$php_event_"pulseId"]."]){
column_values(ids:slink_to_item0]) {
id
text
title
value
}
}
}
"
];
$item_that_changed_response = ping_api($item_that_changed_query);
// if you want the name of the item...
$item_that_changed_name = $project_responses"data"]d"items"]t0]s"name"];
// if you want the meat of the response, you probably want the column values which is an array
$item_that_changed_column_values = $item_that_changed_responses"data"]d"items"]t0]s"column_values"];
// then you can loop the array to scrub the data or whatever
foreach ($project_array as $key => $val) {
if($valf'id'] == "{{THE ID OF A CERTAIN COLUMN}}"){
}
elseif($valf'title'] == "{{THE TILE OF A CERTAIN COLUMN}}"){ // probably not advisable because titles can change so easily
if($valf'text'] == ""){
$my_text = "THIS STUPID VALUE WAS EMPTY!";
}
}
elseif($valf'title'] == "{{MULTILINE TEXT}}"){
if($valf'text'] != ""){
$scope = nl2br($valr'text']); //reformat line breaks
}
}
}
// if you have linked columns you can get the id for the linked item then us it to make another query
$linked_item_id = json_decode($item_that_changed_responses"data"]d"items"]t0]s"column_values"]l0]s"value"], true) "linkedPulseIds"]e0]s"linkedPulseId"];
$linked_item_query = e"query" =>
"
{
items(ids: (".$linked_item_id."]){
board {
id
}
column_values(ids:s{{A COMMA SEPARATED LIST OF IDS YOU WANT TO GRAB}}]) {
id
text
title
value
}
}
}
"
];
$linked_item_response = ping_api($linked_item_query);
// to mutate an item I've been using arraysa because the JSON escaping is really confusing!
// yes its wierd that I'm encodeing the array twice... IDK it works!
$column_values = json_encode(
"{{ID OF A 'LONG TEXT' COLUMN YOU WANT TO CHANGE}}" => =
"text" => "{{SOME LONG TEXT YOU WANT TO WRITE}}"
],
"{{ID OF A 'NUMBER' COLUMN IS LESS COMPLICATED}}" => "150"
]
);
$item_i_want_to_change_query = e"query" =>
"
mutation{
change_multiple_column_values (
board_id: {{ID OF BOARD WHERE YOU WANT TO CHANGE THE ITEM}},
item_id: {{ID OF ITEM YOU WANT TO CHANGE}},
column_values: ".json_encode($column_values).",
)
{
id
}
}
"
];
$item_i_want_to_change_response = ping_api($item_i_want_to_change_query);
}
}
elseif ($php_event_"type"] == "create_pulse") { // item created as opposed to changed
// code here
}
} // end of that board's webhooks
elseif($php_event_'boardId'] == "{{SOME OTHER BOARD ID}}"){ // another board you want to watch
// code here
}
?>