Filipe Ferminiano
Filipe Ferminiano

Reputation: 8791

Call to a member function hears() on a non-object

I'm trying to use this package but I'm getting this error:

Call to a member function hears() on a non-object

This is my code:

$slackbot = new SlackBot();
$slackbot = SlackBot::initialize('xoxb');


// give the bot something to listen for.
$slackbot->hears('hello', function (SlackBot $bot, $message) {
   $bot->reply('Hello yourself.');
});

How can I fix this?


EDIT

This is the file I'm in:

routes.php

<?php

use SlackBot;

Route::post('/slack', function(\Illuminate\Http\Request $request)
{

    $payload = $request->all();

    if ( isset($payload['type']) && isset($payload['challenge']) )
    {
        if ($payload['type'] == 'url_verification')
        {
            return $payload['challenge'];
        }
    }

    $slackbot = new SlackBot();
    $slackbot = SlackBot::initialize('xoxb-xxx');


    // give the bot something to listen for.
    $slackbot->hears('hello', function (SlackBot $bot, $message) {
      $bot->reply('Hello yourself.');
    });

});

Upvotes: 0

Views: 66

Answers (2)

Christos Lytras
Christos Lytras

Reputation: 37318

I think you are trying to use the facade instead of the actual SlackBot class.

Try change:

use SlackBot;

with:

use Mpociot\SlackBot\SlackBot;

Or use the SlackBot::class to get the full class with namespace:

$slackBot = app(SlackBot::class);
$slackBot->initialize('xoxb');

Upvotes: 1

Joel Hinz
Joel Hinz

Reputation: 25404

Your second line is wrong - you should initialise on the created object, not overwrite it.

$slackbot = new SlackBot();
$slackbot->initialize('xoxb');

Edit: I haven't used SlackBot through a facade myself, but it looks like you should not initialise it since you use a facade. Instead, you should make sure your token is set in the right config file (services.slack.bot_token).

Then, instead of e.g.

$slackbot->hears(...)

you do

SlackBot::hears(...)

Upvotes: 1

Related Questions