Buy
Buy

Installing Bundles with "Average" Docs

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Let's do something fun! Google for SlackBundle - you'll find one called nexylan/slack-bundle. This is a fun library that gives us a service that can send messages to a Slack channel. To install it, find the composer require line, copy that, move over to your terminal and paste:

composer require nexylan/slack-bundle php-http/guzzle6-adapter:1.1.1

Interesting: this installs the bundle and some other library called guzzle6-adapter. Wait for it to install and... it fails!

Don't panic. There are two important things happening. First, this installed two bundles! Cool! You can see both of them inside bundles.php:

... lines 1 - 2
return [
... lines 4 - 11
Http\HttplugBundle\HttplugBundle::class => ['all' => true],
Nexy\SlackBundle\NexySlackBundle::class => ['all' => true],
];

For the SlackBundle, it says "auto-generated recipe". That means that the bundle doesn't actually have a recipe... but Symfony Flex, at least added it to bundles.php for us. By the way, this is not necessarily a bad thing: sometimes a bundle doesn't really need a custom recipe!

The second bundle did have a recipe. Before I started recording, I committed my changes so far. To see what that recipe did, let's run:

git status

Interesting: it added a new configuration file called httplug.yaml:

httplug:
plugins:
redirect:
preserve_header: true
discovery:
client: 'auto'
clients:
app:
http_methods_client: true
plugins:
- 'httplug.plugin.content_length'
- 'httplug.plugin.redirect'

We don't know what this does, but it probably configures some sensible defaults. Let's ignore it unless the docs tells us otherwise.

When composer require Fails

The second important thing I want to talk about is... well... this big error!

The child node "endpoint" at path "nexy_slack" must be configured.

Oof... this is not a great error message. It means that this bundle requires some configuration, which we don't have yet. And since it didn't add a configuration file via a recipe, we'll need to create it ourselves. But before we do that, the most important thing to understand is this: when you see an error like this after running composer require, Composer did finish successfully and the library was installed.

Configuring the Slack Endpoint

Ok, let's go read the docs so we can figure out how to configure this bundle. Ah, so one of the reasons that installing this bundle isn't smoother is that its documentation is out-of-date! Hopefully it will be updated soon, but actually, this is a great example of how to navigate less-than-up-to-date docs.

How do I know it's out-of-date? This AppKernel thing is a Symfony 3 concept. We don't need to worry about enabling bundles: this is done for us automatically.

If you scroll down... ah, here is the configuration. And it says that this is an example of default values... which probably means that we don't need to copy all of this. Yep, we just need to fill in the parts that are required, so, endpoint.

Let's copy part of the configuration file. But... it doesn't tell us where to put this! That's ok! We already know: it can live in any file in config/packages. Let's create a new one called nexy_slack.yaml. Paste the config, but the only key we need is endpoint:

nexy_slack:
... lines 2 - 3

If you're coding along, here's how this will work. First, you'll need access to a Slack workspace where you're an admin. If you don't have one, you can create one: it's free and easy.

Once you've got it, go to your domain /apps/manage, then search the App Directory for "Incoming Webhooks". Click "Add Configuration" to setup a new webhook: I've already done this.

Thanks to this, you now have a new Webhook URL, which anyone can use to send messages to your Slack. There's no authentication - the URL is meant to be a secret. Um... yea, I know you can read mine - I'm super bad at secrets. I'll invalidate it after I record.

Copy the URL and paste it next to endpoint:

nexy_slack:
endpoint: 'https://hooks.slack.com/services/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'

Now, move over and clear your cache:

php bin/console cache:clear

Again, starting in Symfony 4.0.5, you will not need to clear your cache when adding a new config file.

What is $this->get()?

Sweet! The bundle is configured, so... how do we use it? Go back to the docs. Below, yea! Usage! And this is where things get really interesting. The code says $this->get('nexy_slack.client). What the heck is that?

Actually, this is something from Symfony 3... which we do not recommend doing in Symfony 4 and may or may not work, depending on the situation. Basically, $this->get() is, or was, a shortcut to fetch a service by its id. Instead of doing this, we are - of course - going to fetch the service via autowiring.

You guys know the drill: find your terminal and run:

php bin/console debug:autowiring

And search for "Slack". Wait... nothing!

Yep... this bundle technically works with Symfony 4... but it hasn't been fully updated. And so, it doesn't expose any services for autowiring! Right now, there is no way to autowire that nexy_slack.client service.

We need to learn a little bit more about public versus private services. And then take control of things with an autowiring alias!

Leave a comment!

  • 2018-11-29 Vladimir Sadicov

    Hey Patrick!

    It's awesome that you have found a working solution for this issue. We have checked packages and found that updated `php-http/guzzle6-adapter` caused it. Probably better solution will be to use

    composer require nexylan/slack-bundle php-http/guzzle6-adapter:1.1.1

    it will be closer to course code.

    By the way we have already updated course page with this new command.

    Cheers!.

  • 2018-11-27 Ahmed Ayman

    probably a different version of composer. or a newer release of php-http that is conflicting with the nexylan bundle. idk!

  • 2018-11-27 Ahmed Ayman

    and got it working by adding :
    "nexylan/slack-bundle": "*",
    "php-http/guzzle6-adapter":"*",
    in the require part of the composer.json file

  • 2018-11-27 Patrick Cool

    I got it 'working' with
    composer require nexylan/slack-bundle:1.1.2 php-http/guzzle6-adapter
    'working' means getting the same error as in the tutorial

    Unfortunately Http\HttplugBundle\HttplugBundle::class => ['all' => true], is not added to config/bundles.php and the file confgi/packages/httplug.yaml was not created either.
    I do find however httplug in vendor/php-http/ and the composer require mentioned that it was installed

    omposer require nexylan/slack-bundle:1.1.2 php-http/guzzle6-adapter
    Using version ^2.0 for php-http/guzzle6-adapter
    ./composer.json has been updated
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Restricting packages listed in "symfony/symfony" to "4.1.*"

    Prefetching 6 packages 🎵 💨
    - Downloading (100%)

    Package operations: 10 installs, 0 updates, 0 removals
    - Installing guzzlehttp/promises (v1.3.1): Loading from cache
    - Installing psr/http-message (1.0.1): Loading from cache
    - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
    - Installing guzzlehttp/guzzle (6.3.3): Loading from cache
    - Installing nexylan/slack (1.7.0): Loading from cache
    - Installing nexylan/slack-bundle (v1.1.2): Loading from cache
    - Installing psr/http-client (1.0.0): Loading from cache
    - Installing php-http/promise (v1.0.0): Loading from cache
    - Installing php-http/httplug (v2.0.0): Loading from cache
    - Installing php-http/guzzle6-adapter (v2.0.0): Loading from cache
    Writing lock file
    Generating autoload files
    Symfony operations: 1 recipe (1419766d96901a70366acb1ed9be6db3)
    - Configuring nexylan/slack-bundle (>=v1.1.2): From auto-generated recipe
    Executing script cache:clear [KO]
    [KO]
    Script cache:clear returned with error code 1
    !!
    !! In ArrayNode.php line 228:
    !!
    !! The child node "endpoint" at path "nexy_slack" must be configured.
    !!
    !!
    !!
    Script @auto-scripts was called via post-update-cmd

  • 2018-11-26 Patrick

    I got it 'working' with
    composer require nexylan/slack-bundle:1.1.2 php-http/guzzle6-adapter

    'working' means getting the same error as in the tutorial

  • 2018-11-26 Patrick

    thanks for your reply. Don't understand why in the tutorial this error did not happen

  • 2018-11-26 Diego Aguiar

    Hmm, nexylan/slack-bundle requires php-http/httplug v1.1 but php-http/guzzle6-adapter requires php-http/httplug v2.0 so you can't have both versions installed, you need to find a release where both bundle versions can work together, you may want to contact the repository maintainer.

    Cheers!

  • 2018-11-26 Patrick

    using Symfony 4.1.7 I get the following error after
    composer require nexylan/slack-bundle php-http/guzzle6-adapter

    Using version ^2.0 for nexylan/slack-bundle
    Using version ^2.0 for php-http/guzzle6-adapter
    ./composer.json has been updated
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Restricting packages listed in "symfony/symfony" to "4.1.*"
    Your requirements could not be resolved to an installable set of packages.

    Problem 1
    - Installation request for nexylan/slack-bundle ^2.0 -> satisfiable by nexylan/slack-bundle[v2.0.0].
    - nexylan/slack v2.0.0 requires php-http/client-common ^1.7 -> satisfiable by php-http/client-common[1.7.0, 1.8.0, 1.8.1].
    - php-http/client-common 1.7.0 requires php-http/httplug ^1.1 -> satisfiable by php-http/httplug[v1.1.0].
    - php-http/client-common 1.8.0 requires php-http/httplug ^1.1 -> satisfiable by php-http/httplug[v1.1.0].
    - php-http/client-common 1.8.1 requires php-http/httplug ^1.1 -> satisfiable by php-http/httplug[v1.1.0].
    - Can only install one of: php-http/httplug[v2.0.0, v1.1.0].
    - php-http/guzzle6-adapter v2.0.0 requires php-http/httplug ^2.0 -> satisfiable by php-http/httplug[v2.0.0].
    - nexylan/slack-bundle v2.0.0 requires nexylan/slack ^2.0 -> satisfiable by nexylan/slack[v2.0.0].
    - Installation request for php-http/guzzle6-adapter ^2.0 -> satisfiable by php-http/guzzle6-adapter[v2.0.0].

    Installation failed, reverting ./composer.json to its original content.

  • 2018-06-14 toporovvv

    If you need second dev channel to work on other instance of your application (like prod slack channel - for prod environment, dev slack channel - for developers environment), than you need to specify endpoint as a parameter like
    endpoint: '%slackEndpoint%'
    and then specify this parameters in services.yaml and services_dev.yaml as it was done in the past tutorial of this course (https://knpuniversity.com/s....

  • 2018-06-07 Babagana Zannah

    Thank you so so very much Sir. Thank you.

  • 2018-06-07 weaverryan

    Hi Babagana Zannah!

    Hmm. First, I may give a silly answer, because I'm trying to remember how this bundle works :). And, actually, the way the the incoming webhooks work is confusing. So, when you setup the webhook in Slack, you configure which channel you want to send to. But, when you actually *send* the message in PHP, there is also a to() method where you can set the channel: https://github.com/nexylan/...

    So, I would try using this ONE endpoint and the to() method to try to send to the #dev channel. From my reading, it sounds like this may *not* work - but I'm not 100% sure... because it seems odd to have this to() method, if it never works. If it doesn't work and you DO need 2 endpoints then, yes, the bundle does not support this. It's not a HUGE deal, you will just need to configure a second "Nexy\Slack\Client" service manually on your own (i.e. in services.yml).

    Cheers!

  • 2018-06-07 Babagana Zannah

    How to use multiple endpoints for Symfony NexySlackBundle

    I am using the nexylan/slack Bundle for my symfony 3.4 application. I configured the slack Incoming WebHook for #general channel and it's working as expected. The bundle configuration looks something like:


    nexy_slack:

    # If you want to use an another httplug client service.
    http:
    client: httplug.client

    # The Slack API Incoming WebHooks URL.
    endpoint: https://hooks.slack.com/services/ABCD/987ABC
    channel: null
    username: null
    icon: null
    link_names: false
    unfurl_links: false
    unfurl_media: true

    allow_markdown: true
    markdown_in_attachments: []

    Now I have another channel called #dev and I've added the Incoming WebHook and received the endpoint. I also want to send messages to the dev channel.

    My question is, how can I configure the dev channel endpoint too in order to use it. Is there any way I can do this?