Filipe Ferminiano Filipe Ferminiano - 25 days ago 7
PHP Question

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.');
});

});

Answer

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(...)