hanoii / platformsh2slack

An adapter from platform.sh webhook to slack incoming webhook that can be hosted on a platform.sh app.
20 stars 5 forks source link

Add a way to customize Slack messages #2

Open clemherreman opened 7 years ago

clemherreman commented 7 years ago

Hi!

First of all, thank you for sharing this with the community. You rock!

I'd like to have a way to customize the messages sent to Slack, without having to edit platform2slack code. This could be just a callback, given when setting up my hook, much like validateToken():

// Optional settings
$settings = [
    'channel' => '#random',
    'region' => 'eu',
];

$platformsh2slack = new Hanoii\Platformsh2Slack\Platformsh2Slack(
    'https://hooks.slack.com/services/...',
    $settings
);

// 'environment.delete' come from the `switch ($platformsh->type)`
$platformsh2slack->customizeSlackText('environment.delete', function ($defaultText, $platformsh) {
    return sprintf('Oh noes, the env "%s" has been deleted', $platformsh->payload->environment->name);
});

// "all" is a special event
$platformsh2slack->customizeSlackText('all', function ($defaultText, $platformsh) {
    return sprintf('Hey @here: %s', $defaultText); // Prefix any message sent to slack.
});

My own usecase is that I want to customize the way routes are displayed, to make it more user-friendly (read: project manager friendly)

I'm willing to take some time and write the PR, but I wanted to know if you were interested in such a new feature.

hanoii commented 7 years ago

Hey, thanks, I am glad you like it. I appreciate the suggestion, I recently moved this into a proper composer library and the formatting of message was carried over from the original script. It's definitely a place to improve.

To be honest, I believe that callbacks are not the way to go. Not very OOP.

What you could and show do is extend the class and override the methods you need.

Right now that's processPlatformshPayload(), however, it kind of have quite a bit of logic. That function needs to be properly refactored into something that's easier to extend from upper classes and make better functions to extend.

If you are want to contribute some refactor I am happy to review.

Also, if if you want to suggest a more user friendly way of displaying routes, and if I like it more or it looks better that what I did, I am also happy to change it. I haven't given it much thought.