Ana
Ana

Reputation: 73

Slack, sending and receiving messages

I am new in slack and i would like to do a bot user in my channel. The problem is: every time that i want to send a message to slack i have to refresh my page (the page that receives the information from slack). I configured my Incoming Webhook with success.

This is what i did to configured my incoming webhook with success in php:

header('Content-Type: application/json');
    $entityBody = file_get_contents('php://input');
    echo $entityBody;

My question is why do i have to refresh my page in order to receive or send messages to slack?

Upvotes: 1

Views: 2442

Answers (2)

Uri Goren
Uri Goren

Reputation: 13700

Here is a simple example of sending outgoing messages to slack with curl

<?php
define('SLACK_WEBHOOK', 'https://hooks.slack.com/services/xxx/yyy/zzz');
function slack($txt) {
  $msg = array('text' => $txt);
  $c = curl_init(SLACK_WEBHOOK);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($c, CURLOPT_POST, true);
  curl_setopt($c, CURLOPT_POSTFIELDS, array('payload' => json_encode($msg)));
  curl_exec($c);
  curl_close($c);
}
?>

Snippet taken from here

Upvotes: 2

Erik Kalkoken
Erik Kalkoken

Reputation: 32854

The reason your have the refresh your page is that it contains the PHP code ( I assume) and refreshing it triggers its execution. That is however not how you would usually do it. Depending on your use case you would usually have another web service trigger your script, e.g. a Slack event.

For implementing a bot user one approach is to use the Events API and subscribe to the message event. Each time a message is send your script will automatically be called by Slack and can then process the incoming message.

For sending a message back will just require to simple reply to the Slack request with an echo. You would not need an incoming webhook for that.

Upvotes: 2

Related Questions