Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Latest commit

 

History

History
66 lines (45 loc) · 2.85 KB

usage.md

File metadata and controls

66 lines (45 loc) · 2.85 KB

Usage

In this chapter I want to give you an overall idea of how to send payloads to the Slack API, accessing the methods that it has made available.

To do this, I'm going to be using an example scenario where I want to send a message to Slack, which is probably the scenario that drove you to this package in the first place (am I right 😄?). So let's get slacking!

NOTE: For detailed examples of each and every method that this library supports, check out the method reference.

1) Setting up the client

First, we need to construct the client that will be communicating with the Slack API. To do this you will be using the API token that you should have generated by now.

If you haven't created your own API token yet, check out the Getting Started doc to do so now.

Once obtained, you should provide it as the constructor argument.

$client = new ApiClient('your-api-token-here');

NOTE: Alternatively, you can provide a token on each call you make to the send()-method (see further below); it all depends on how you use this in your application.

2) Sending a payload (here: chat.postMessage)

Most likely you will just want to send some messages to one of your Slack channels, so here's how you do it:

$payload = new ChatPostMessagePayload();
$payload->setChannel('#general');
$payload->setMessage('Hello world!');

See how every method that the Slack API provides has been converted into a special payload-class. This is because not every method requires the same data, and some methods even send data back. For sending a message, this class is aptly called the ChatPostMessagePayload class.

Actually, all payloads follow their API-method's name, camel-cased to follow PSR standards, in this case converted from chat.postMessage.

All that's left to do now is to send the payload we created. This is very simple as well:

/** @var ChatPostMessagePayloadResponse $response */
$response = $client->send($payload);

// the following is very much up to you, this is just a very simple example
if ($response->isOk()) {
    echo sprintf('Successfully posted message on %s', $response->getChannelId());
} else {
    echo sprintf('Failed to post message to Slack: %s', $response->getErrorExplanation());
}

That's it! If you provided a valid token and used the instructions above, there should now be a message Hello World! posted in the #general of your team's Slack instance. If this did not happen, check out the instructions for handling errors below.

NOTE: Although the setters and getters are different of each payload, they are all handled in a similar way as illustrated above.

Next chapter

In the next chapter, I show you an example of using the events made available by this package: Events