Skip to content

Commit

Permalink
V2.1.0 (#2)
Browse files Browse the repository at this point in the history
* More handy subclasses
Introduce more handy subclasses `Section`, `Divider`, `MarkdownText`, `PlainText`, `Image`, `Button` to be used instead of the generic classes `Block`, `Accessory`, `Text`

* Update README with a Hello, World! example

* Update the readme file

* Fix accessory type for buttons and new lines at the end of some classes.
  • Loading branch information
amiriskander committed Feb 13, 2022
1 parent ec8d7c5 commit 3405a97
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 2 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,41 @@ A simple PHP package that can be used to build and send Slack messages

- PHP >= 7.1
- PHP cURL Extension
- PHP JSON Extension

### Installation
```
composer require amiriskander/slack-notifier
```

### Usage

There is no massive effort you need to create a simple text message like `Hello, World!`

```
use AmirIskander\SlackNotifier;
public function TestHelloWorld()
{
$message = new Message()
->addBlock(new Section()->setText(new PlainText('Hello, World!')));
->send('https://hooks.slack.com/services/....')
;
}
```

The package supports block types:
- Divider: A horizontal gray divider.
- Section: A container that can contain multiple components.

And a block of type section can have components of types:
- Text: Can be either `Plain Text` or `Markdown`
- Accessory: Can be either `Button` or `Image`


For more complex examples that needs adding blocks with images, links and buttons, it is not a big problem.
Check the below example:

```
use AmirIskander\SlackNotifier;
Expand Down Expand Up @@ -62,5 +90,53 @@ public function TestSendSlackMessage()
}
```

The same example using classes for Button, Image, Section, Divider
```
use AmirIskander\SlackNotifier;
public function TestSendSlackMessage()
{
// Init Message object
$message = new Message();
// Adding a section
$block = new Section();
$block->setText(new MarkdownText('John Doe submitted a feedback!'));
$message->addBlock($block);
// Adding a divider
$message->addBlock(new Divider());
$block = new Section();
$block->setText(new MarkdownText(':new: *You guys are awesome. Keep up the good work!*'));
$message->addBlock($block);
$block = new Section();
$block->setText(new MarkdownText('*Source* :information_source: Homepage Feedback Form'));
$message->addBlock($block);
$block = new Section();
$block->setText(new MarkdownText('*In the last 30 days*'));
$block->addField(new MarkdownText('Submitted 6 form submissions'));
$block->addField(new MarkdownText('Rated 12 products'));
$accessory = new Image('https://i.ibb.co/19W2sdD/stat.png', 'Stats');
$block->setAccessory($accessory);
$message->addBlock($block);
$message->addBlock(new Divider());
$block = new Section();
$block->setText(new MarkdownText('More information about this user'));
$block->setAccessory(new Button(
'https://yourwebsite/user/123/profile/',
':bust_in_silhouette: View User'
));
$message->addBlock($block);
// Replace parameter below with webhook of channel you would like to post to
$message->send('https://hooks.slack.com/services/....');
}
```

### Screenshot
![Sample message screenshot](screenshot.png "Sample message screenshot")
16 changes: 16 additions & 0 deletions src/Button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace AmirIskander\SlackNotifier;

class Button extends Accessory
{
public function __construct(string $url, string $text, ?string $alt = null)
{
parent::__construct('button');
$this
->setUrl($url)
->setText(new PlainText($text))
->setAltText($alt)
;
}
}
11 changes: 11 additions & 0 deletions src/Divider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AmirIskander\SlackNotifier;

class Divider extends Block
{
public function __construct()
{
parent::__construct('divider');
}
}
15 changes: 15 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AmirIskander\SlackNotifier;

class Image extends Accessory
{
public function __construct(string $url, ?string $alt = null)
{
parent::__construct('image');
$this
->setImageUrl($url)
->setAltText($alt)
;
}
}
11 changes: 11 additions & 0 deletions src/MarkdownText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AmirIskander\SlackNotifier;

class MarkdownText extends Text
{
public function __construct(string $text)
{
parent::__construct('mrkdwn', $text);
}
}
3 changes: 1 addition & 2 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ public function send(string $webhookUrl)

// Create CURL HTTP POST request
$ch = curl_init($webhookUrl);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($body)
]);
Expand Down
11 changes: 11 additions & 0 deletions src/PlainText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AmirIskander\SlackNotifier;

class PlainText extends Text
{
public function __construct(string $text)
{
parent::__construct('plain_text', $text);
}
}
11 changes: 11 additions & 0 deletions src/Section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AmirIskander\SlackNotifier;

class Section extends Block
{
public function __construct()
{
parent::__construct('section');
}
}

0 comments on commit 3405a97

Please sign in to comment.