Message formatting, send buttons via Slack API with PHP - php

I'm using webhooks to send messages via Slack API
I need to send a button with a link to a report
I did it with Python successfully
But with PHP i'm having trouble with the arrays of actions in the attachments
code 1
$data = array(
"text" => $message
);
$actions =
[
'type' => "button",
'text' => "Report1",
'url' => "https://url.report1"
];
$data += [
"attachments" =>
[
"fallback" => "More details...", //I only get the message and this text in the slack
'actions' => [$actions] // or array($actions)
]
];
$payload = json_encode($data);
print_r($data) output:
Array
(
[text] => teste
[attachments] => Array
(
[fallback] => More details...
[actions] => Array
(
[0] => Array
(
[type] => button
[text] => Report1
[url] => https://url.report1
)
)
)
)
The paylod is shown, but the button doesn't
code 2
$data = array(
"text" => $message
);
$actions =
[
'type' => "button",
'text' => "Report1",
'url' => "https://url.report1"
];
$data += [
"attachments" =>
[
"fallback" => "More details...",
'actions' => $actions
]
];
$payload = json_encode($data);
print_r($data) output:
Array
(
[text] => teste
[attachments] => Array
(
[fallback] => More details...
[actions] => Array
(
[type] => button
[text] => Report1
[url] => https://url.report1
)
)
)
Neither payload or button are showed
Here's the documentation example, it's very easy to send this with python
{
"text": "<#W1A2BC3DD> approved your travel request. Book any airline you like by continuing below.",
"attachments": [
{
"fallback": "Book your flights at https://flights.example.com/book/r123456",
"actions": [
{
"type": "button",
"text": "Book flights 🛫",
"url": "https://flights.example.com/book/r123456"
}
]
}
]
}
How I can create this kind of structure in PHP?

Here is your "code 1" with the correction. Your attachments property needs to be an array of attachment arrays. You only had a simple array.
$message = "Hello Stackoverflow";
$data = array(
"text" => $message
);
$actions =
[
'type' => "button",
'text' => "Report1",
'url' => "https://url.report1"
];
$data += [
"attachments" =>
[
[
"fallback" => "More details...", //I only get the message and this text in the slack
'actions' => [$actions] // or array($actions)
]
]
];
$payload = json_encode($data);
print_r($payload);
The resulting payload works in the message builder.

Related

Need help to update values in ACF via WP API

I'm trying to update an item particularly with the "rows" feature in WordPress ACF plugin via PHP using Wordpress API.
The value I'm looking to change is "Monday"
Here's the json from the endpoint:
{
"id": 765,
"acf": {
"contact": "2150",
"sched": [
{
"field_611218a978d03": "Monday",
"field_6112197d78d04": "00:00:00",
"field_6112199f78d05": "17:00:00"
}
],
},
}
I can change the value of "2150" to "2151" with the following:
'body' => array(
'acf' => array (
'contact' => "2151",
),
)
But I can't change the value of "Monday", I've already tried the following:
'body' => array(
'acf' => array (
'sched' => array ( array ( "field_611218a978d03" => "Tuesday")
)
)
As well as:
'body' => array(
'acf' => array (
'sched' => array (
[0] => array( "field_611218a978d03" => "Tuesday")
)
)
)
I've been searching and there seems to be no answer pertaining to this specific problem. I've been stumped for days now. Any help would sincerely be appreciated!
Edit:
To answer CBroe this is the code I'm working with:
$api_response = wp_remote_post( 'domain.com/wp-json/wp/v2/person/765', array(
'headers' => array('Authorization' => 'Basic ' . base64_encode( 'user:pass' )),
'body' => array(
'acf' => array (
'contact' => '2150',
'sched' => array ( array ( "field_611218a978d03" => "Tuesday")
),
)
));
$body = json_decode( $api_response['body'],true);

How to push the array values based on which skill belongs to particular question

I have two array 1.$allSkills and 2. $questionList.
$allSkills = [
[ "skillID" => "d45","contentNodes" => [ "leafNodes" => [ "en" => ["1","2","3"] ]]],
[ "skillID" => "d46","contentNodes" => [ "leafNodes" => [ "en" => ["4","5","6"] ]]],
[ "skillID" => "d47","contentNodes" => [ "leafNodes" => [ "en" => ["7","8","9"] ]]]
];
$questionList = ["1","3","5","6","8"];
Now i want to find $questionList values which skillID belongs to. i have tried but i couldn't get my expected out ,please any one help me out on this.
My Code
<?php
$allSkills = [
[ "skillID" => "d45","contentNodes" => [ "leafNodes" => [ "en" => ["1","2","3"] ]]],
[ "skillID" => "d46","contentNodes" => [ "leafNodes" => [ "en" => ["4","5","6"] ]]],
[ "skillID" => "d47","contentNodes" => [ "leafNodes" => [ "en" => ["7","8","9"] ]]]
];
$questionList = ["1","3","5","6","8"];
foreach($allSkills as $skilID => $skilVal){
$leafNodes = $skilVal['contentNodes']['leafNodes']['en'];
$result = !empty(array_intersect($leafNodes, $questionList));
if($result){
$finalResult[$skilVal['skillID']] = $leafNodes;
}
}
echo "<pre>";
print_r($finalResult);
?>
My Expected output
Array
(
[d45] => Array
(
[0] => 1
[1] => 3
)
[d46] => Array
(
[0] => 5
[1] => 6
)
[d47] => Array
(
[0] => 8
)
)
You should be putting the intersection into the result, not the entire $leafNodes array.
foreach($allSkills as $skilID => $skilVal){
$leafNodes = $skilVal['contentNodes']['leafNodes']['en'];
$skills = array_intersect($leafNodes, $questionList);
if($skills){
$finalResult[$skilVal['skillID']] = $skills;
}
}
if($result){
$finalResult[$skilVal['skillID']] = array_values(array_intersect($leafNodes, $questionList));}

How to create json array php with childs

i am struggling with encoding json data
when posting to confluenec it needs to be
{"id":"282072112","type":"page","title":"new page","space":{"key":"BLA"},"body":{"storage":{"value":"<p>This is the updated text for the new page</p>","representation":"storage"}},"version":{"number":2}}'
so in php i created
$data = array('id'=>$siteid, 'type'=>'page', 'title'=>'title of the page');
$data_json = json_encode($data);
print_r ($data_json);
The endresult should look like
{
"id": "282072112",
"type": "page",
"title": "new page",
"space": {
"key": "BLA"
},
"body": {
"storage": {
"value": "<p>This is the updated text for the new page</p>",
"representation": "storage"
}
},
"version": {
"number": 2
}
}
but how can i add the childs etc?
Thanks
You can nest data in arrays similarly to what you would to in JavaScript:
$data = [
'id' => $siteid,
'type' => 'page',
'title' => 'new page',
'space' => [
'key' => 'BLA',
],
'body' => [
'storage' => [
'value' => '<p>...</p>',
'representation' => 'storage'
],
],
'version' => [
'number' => 2,
],
];
// as JSON in one line:
echo json_encode($data);
// or pretty printed:
echo json_encode($data, JSON_PRETTY_PRINT);
$data = [
"id" => $siteid,
"type" => "page",
"space" => ["key" => "bla"]
//...
]
You can nest arrays.
See also: Shorthand for arrays: is there a literal syntax like {} or []? and https://www.php.net/manual/en/language.types.array.php
Try it
$data_child = array( 'value'=> 'blablabla' );
$data = array('id'=>$siteid, 'type'=>'page', 'title'=>'title of the page', 'child' => $data_child );
$data_json = json_encode($data);
You can create nested array this way and the send it after json_encode()
<?php
$preparing_array = array
(
"id" => 282072112,
"type" => "page",
"title" => "new page",
"space" => array
(
"key" => "BLA"
),
"body" => array
(
"storage" => array
(
"value" => "<p>This is the updated text for the new page</p>",
"representation" => "storage"
)
),
"version" => array
(
"number" => 2
)
);
echo json_encode($preparing_array, JSON_PRETTY_PRINT);
?>
DEMO: https://3v4l.org/16960

Dynamically push arrays into multidimensional array

I'm trying to dynamically build an array for the Facebook Messenger SDK and populate it with data from a database.
I can't seem to get a properly structured array no matter what I try.
What I need:
$messages = [
"attachment" => [
"type" => "template",
"payload" => [
"template_type" => "generic",
"elements" => [[
"title" => $row['title'],
"item_url" => $row['item_url'],
"image_url" => $row['image_url'],
"subtitle" => $row['subtitle'],
"buttons" => [[
"type" => "web_url",
"url" => "www.google.com",
"title" => "View Website"],
["type" => "postback",
"title" => "Start Chatting",
"payload" => "start"]]
]
]]
]];
I need to create the data inside buttons based on what I have in the database, tried with array_merge and inserting the array as a string:
// Created arrays
if (!empty($row['button1_type'])) {
$buttons[] = array("type" => $row['button1_type'],"url" => $row['button1_url'],
"title" => $row['button1_title'],
"payload" => $row['button1_payload']);
}
if (!empty($row['button2_type'])) {
$buttons[] = array("type" => $row['button2_type'],"url" => $row['button2_url'],
"title" => $row['button2_title'],
"payload" => $row['button2_payload']);
}
// In the case when I had array_merge - $buttons were actually named as $buttons1 and $buttons2
$buttons = array_merge($buttons1, $buttons2);
// Tried to add it as a string
if (!empty($row['button2_type'])) {
$buttons = "\"type\" => ".$row['button2_type'].",\"url\" => .".$row['button2_url'].",
\"title\" => ".$row['button2_title'].",
\"payload\" => ".$row['button2_payload'];
}
$messages = [
"attachment" => [
"type" => "template",
"payload" => [
"template_type" => "generic",
"elements" => [[
"title" => $row['title'],
"item_url" => $row['item_url'],
"image_url" => $row['image_url'],
"subtitle" => $row['subtitle'],
"buttons" => [
$buttons
]
]
]]
]];
Screenshot showing the differences between correct and incorrect:
So basically $buttons are created inside an extra array, how can I get rid of it? Maybe I should change the whole approach?
With
"buttons" => [$buttons]
//new array ^ ^
a new array is being created with square brackets so to avoid it. use
"buttons" => $buttons

building a php array that will make specific JSON

I need to generate the following JSON:
{
"title": "my form",
"fields": [
{
"type": "short_text",
"question": "What is your name?"
},
{
"type": "multiple_choice",
"question": "How often do you want to receive emails?",
"choices": [
{
"label": "Daily"
},
{
"label": "Weekly"
},
{
"label": "Monthly"
},]
},]
I'm trying to do it with this php code:
$data = array(
"title" => "my form",
"fields" => array (
array (
"type" => "short_text",
"question" => "What is your name?"
),
array (
array (
"type" => "multiple_choice",
"question" => "How often do you want to receive emails?",
"choices" => (
array ("label" => "Daily")
),
(array ("label" => "Weekly")),
(array ("label" => "Monthly"))
)
)
)
);
$output = json_encode($data);
...but it's not working.
I'd appreciate any help you guys can offer!
<?php
$data = array (
'title' => 'my form',
'fields' =>
array (
array (
'type' => 'short_text',
'question' => 'What is your name?',
),
array (
'type' => 'multiple_choice',
'question' => 'How often do you want to receive emails?',
'choices' =>
array (
array (
'label' => 'Daily',
),
array (
'label' => 'Weekly',
),
array (
'label' => 'Monthly',
),
),
),
),
);
$output = json_encode($data);
?>

Categories