Package: slackbotng
This is a fork ofscrapinghub/slackbotdue to the lack of the activity from the original author.Python package was renamed toslackbotngand it's being publishing to:https://pypi.org/project/slackbotng/A chat bot forSlackinspired byllimllib/limboandwill.FeaturesBased on slackReal Time Messaging APISimple plugins mechanismMessages can be handled concurrentlyAutomatically reconnect to slack when connection is lostFull-fledged functional testsInstallationpip install slackbotngUsageGenerate the slack api tokenFirst you need to get the slack api token for your bot. You have two options:If you use abot user integrationof slack, you can get the api token on the integration page.If you use a real slack user, you can generate an api token onslack web api page.Configure the botFirst create aslackbot_settings.pyand arun.pyin your own instance of slackbot.Configure the api tokenThen you need to configure theAPI_TOKENin a python moduleslackbot_settings.py, which must be located in a python import path. This will be automatically imported by the bot.slackbot_settings.py:API_TOKEN="<your-api-token>"Alternatively, you can use the environment variableSLACKBOT_API_TOKEN.Run the botfromslackbot.botimportBotdefmain():bot=Bot()bot.run()if__name__=="__main__":main()Configure the default answerAdd a DEFAULT_REPLY toslackbot_settings.py:DEFAULT_REPLY="Sorry but I didn't understand you"Configure the docs answerThemessageattribute passed toyour custom pluginshas an special functionmessage.docs_reply()that will parse all the plugins available and return the Docs in each of them.Send all tracebacks directly to a channel, private channel, or userSetERRORS_TOinslackbot_settings.pyto the desired recipient. It can be any channel, private channel, or user. Note that the bot must already be in the channel. If a user is specified, ensure that they have sent at least one DM to the bot first.ERRORS_TO='some_channel'# or...ERRORS_TO='username'Configure the pluginsAddyour plugin modulesto aPLUGINSlist inslackbot_settings.py:PLUGINS=['slackbot.plugins','mybot.plugins',]Now you can talk to your bot in your slack client!Attachment Supportfromslackbot.botimportrespond_toimportreimportjson@respond_to('github',re.IGNORECASE)defgithub(message):attachments=[{'fallback':'Fallback text','author_name':'Author','author_link':'http://www.github.com','text':'Some text','color':'#59afe1'}]message.send_webapi('',json.dumps(attachments))Create PluginsA chat bot is meaningless unless you can extend/customize it to fit your own use cases.To write a new plugin, simply create a function decorated byslackbot.bot.respond_to,slackbot.bot.listen_to,slackbot.bot.run_at_times:A function decorated withrespond_tois called when a message matching the pattern is sent to the bot (direct message or @botname in a channel/group chat)A function decorated withlisten_tois called when a message matching the pattern is sent on a channel/group chat (not directly sent to the bot)A function decorated withrun_at_timesis called periodically at a given amount of secondsfromslackbot.botimportrespond_to,listen_to,run_at_timesimportre@respond_to('hi',re.IGNORECASE)defhi(message):message.reply('I can understand hi or HI!')# react with thumb up emojimessage.react('+1')@respond_to('I love you')deflove(message):message.reply('I love you too!')@listen_to('Can someone help me?')defhelp(message):# Message is replied to the sender (prefixed with @user)message.reply('Yes, I can!')# Message is sent on the channelmessage.send('I can help everybody!')# Start a thread on the original messagemessage.reply("Here's a threaded reply",in_thread=True)@run_at_times(run_once_at=60)defrun_once_at_60s(client):client.rtm_send_message('channel_name_or_username','This runs once at 60s!')To extract params from the message, you can use regular expression:fromslackbot.botimportrespond_to@respond_to('Give me (.*)')defgiveme(message,something):message.reply('Here is{}'.format(something))If you would like to have a command like 'stats' and 'stats start_date end_date', you can create reg ex like so:fromslackbot.botimportrespond_toimportre@respond_to('stat$',re.IGNORECASE)@respond_to('stat (.*) (.*)',re.IGNORECASE)defstats(message,start_date=None,end_date=None):And add the plugins module toPLUGINSlist of slackbot settings, e.g. slackbot_settings.py:PLUGINS=['slackbot.plugins','mybot.plugins',]The@default_replydecoratorAdded in slackbot 0.4.1Besides specifyingDEFAULT_REPLYinslackbot_settings.py, you can also decorate a function with the@default_replydecorator to make it the default reply handler, which is more handy.@default_replydefmy_default_handler(message):message.reply('...')Here is another variant of the decorator:@default_reply(r'hello.*)')defmy_default_handler(message):message.reply('...')The above default handler would only handle the messages which must (1) match the specified pattern and (2) can't be handled by any other registered handler.List of third party pluginsYou can find a list of the available third party plugins onthis page.