I am trying to notify a slack channel whenever a new order appears in our site. But I am struggling to perform this CURL request in Laravel
curl -X POST --data-urlencode "payload={\"channel\": \"#new-order\", \"username\": \"product-notifier\", \"text\": \"New Orders: \n https://mission-control.airbringr.com/orders/item/411980\", \"icon_emoji\": \":smiley:\"}" https://hooks.slack.com/services/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
I’ve been trying to come up with a solution:
public function slack(Request $request){
$client = new \GuzzleHttp\Client();
try {
$client = new Client();
$response = $client->post('https://hooks.slack.com/services/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',[
'json' => [
'payload' =>
['channel'=>'#new-order','username'=> 'product-notifier', 'text'=>'New Orders: \n https://mission-control.airbringr.com/orders/item/411980', 'icon_emoji'=>':smiley:']
],
]
);
} catch (\Exception $e) {
return response()->json($e->getMessage(), 400);
}
return response()->json(["message" => "notified successfully"], 200);
}
But I am getting "Client error: `POST https:\/\/hooks.slack.com\/services\/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` resulted in a `400 Bad Request` response:\nmissing_text_or_fallback_or_attachments\n"
but the curl request works just fine in my terminal.
Any suggestions on how can I make this work?
>Solution :
Atleast you do not need to wrap you data in a payload key. Which you can see in the documentation here.
$response = $client->post('https://hooks.slack.com/services/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',[
'json' => [
'channel'=>'#new-order',
'username'=> 'product-notifier',
'text'=>'New Orders: \n https://mission-control.airbringr.com/orders/item/411980', 'icon_emoji'=>':smiley:'
]
],
);