问题
i have configured Slack outgoing-webhook, but i'm not sure how to handle HTTP POST request which was send by Slack to my specified URL. Workflow is like this. when someone sends a message to specified channel, then an HTTP POST request will be sent by API to one of the specified URL (on which i can do something with that POST). Currently i'm not able to handle that request in my PHP code. i used below code, as it works for POSTs made by forms.
<?php
if(isset($_POST['text'])){ echo $_POST['text'];}
?>
but it does'nt work. As i'm receiving POST from API, i need to handle this request to get data from it.
Any help will be highly appreciated.
回答1:
Assuming that your script is reachable with a public URL and correctly configured in Slack as outgoing webhook (remember you need either a trigger word or configure a public channel) it will receive the Slack request. The Slack parameters can be accessed as you did through the superglobal $_POST
, however your echo command will not work.
If you want to send a message back to Slack your need to use the Slack format for messages. Here is an example:
$message = ["text" => "Hello Slack"];
echo json_encode($message);
But be careful. If you have configured the webhook to listen for a channel sending back a message to Slack will again trigger the webhook and create a loop. So only do that if you have configured the webhook to listen to trigger words.
Or if you just want to see want has been received, I would recommend to write it into a log, so that you can review it later. Here is a very simple and easy to use Logger for PHP. Its nothing fancy, but works nicely for small projects and tests. Or use Monolog, a popular Logger for PHP.
来源:https://stackoverflow.com/questions/42419340/how-to-handle-outgoing-webhook-slack-using-php