scrapinghub / slackbot

A chat bot for Slack (https://slack.com).
MIT License
1.26k stars 395 forks source link

Can't initialize module #188

Closed the-machine-preacher closed 5 years ago

the-machine-preacher commented 5 years ago

Hi,

I'm new to Slack bots (familiar with Discord.py). Followed your example, installed the package using pip install slackbot, went to the path of the installation, created a slackbot_settings.py (with specified tags you mentioned) and run.py in the main directory and mybot.py (with example commands you showed on your page), but I can't initialize the bot. I tried to run the bot using python run.py but get the following error:

Traceback (most recent call last): File "run.py", line 23, in main() File "run.py", line 20, in main bot.run() File "/Users/xxx/anaconda3/lib/python3.5/site-packages/slackbot/bot.py", line 32, in run self._plugins.init_plugins() File "/Users/xxx/anaconda3/lib/python3.5/site-packages/slackbot/manager.py", line 31, in init_plugins self._load_plugins(plugin) File "/Users/xxx/anaconda3/lib/python3.5/site-packages/slackbot/manager.py", line 47, in _load_plugins path_name = importlib_find(plugin) File "/Users/xxx/anaconda3/lib/python3.5/importlib/util.py", line 89, in find_spec return _find_spec(fullname, parent.path) AttributeError: module 'mybot' has no attribute 'path'

mybot.py has the following lines:

from slackbot.bot import respond_to
from slackbot.bot import listen_to
import re

@respond_to('hi', re.IGNORECASE)
def hi(message):
    message.reply('I can understand hi or HI!')
    # react with thumb up emoji
    message.react('+1')

@respond_to('I love you')
def love(message):
    message.reply('I love you too!')

@listen_to('Can someone help me?')
def help(message):
    # Message is replied to the sender (prefixed with @user)
    message.reply('Yes, I can!')

    # Message is sent on the channel
    message.send('I can help everybody!')

    # Start a thread on the original message
    message.reply("Here's a threaded reply", in_thread=True)

Where am I going wrong?

ErikKalkoken commented 5 years ago

The bot apparently can not find your mybot plugin. Assuming mybot.py is in the same folder than run.py change the entry in PLUGINS to 'mybot'. Then it should work.

Full example of slackbot_settings.py:

DEFAULT_REPLY = "Sorry but I didn't understand you"

PLUGINS = [    
    'mybot' 
]
the-machine-preacher commented 5 years ago

Worked! Thanks!

ErikKalkoken commented 5 years ago

Happy to help. Please also mark this as solution to your related question on StackOverflow: https://stackoverflow.com/questions/56797185/how-to-initialize-python-slackbot-module