Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 81f0819

Browse files
author
Jeff Ammons
committed
Clean up for packaging and bug fix
1 parent e6311dd commit 81f0819

File tree

10 files changed

+81
-63
lines changed

10 files changed

+81
-63
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ env
1111
tests/.cache
1212
.coverage
1313
.cache
14+
15+
# Packaging related
16+
dist/
17+
rtmbot.egg-info/
18+
MANIFEST

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include LICENSE
2+
include CHANGELOG.md
3+
include README.md

README.md

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,29 @@ Installation
2424
-----------
2525

2626
1. Create your project
27+
2728
mkdir myproject
2829
cd myproject
2930

3031
2. Install rtmbot (ideally into a virtualenv https://virtualenv.readthedocs.io/en/latest/)
3132

3233
pip install rtmbot
3334

34-
3. Create conf file and configure rtmbot (https://api.slack.com/bot-users)
35-
36-
vi rtmbot.conf
35+
3. Create and rtmbot.conf file and configure rtmbot (https://api.slack.com/bot-users)
3736

38-
DEBUG: True # make this False in production
39-
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
40-
ACTIVE_PLUGINS:
41-
- plugins.repeat.RepeatPlugin
42-
- plugins.test.TestPlugin
37+
# Add the following to rtmbot.conf
38+
DEBUG: True # make this False in production
39+
SLACK_TOKEN: "xoxb-11111111111-222222222222222"
40+
ACTIVE_PLUGINS:
41+
- plugins.repeat.RepeatPlugin
4342

4443
```DEBUG``` will adjust logging verbosity and cause the runner to exit on exceptions, generally making dubugging more pleasant.
4544

4645
```SLACK_TOKEN``` is needed to authenticate with your Slack team. More info at https://api.slack.com/web#authentication
4746

4847
```ACTIVE_PLUGINS``` RTMBot will attempt to import any Plugin specified in `ACTIVE_PLUGINS` (relative to your python path) and instantiate them as plugins. These specified classes should inherit from the core Plugin class.
4948

50-
For example, if your python path includes '/path/to/myproject' and you include `plugins.repeat.RepeatPlugin` in ACTIVE_PLUGINS, it will find the RepeatPlugin class within /path/to/myproject/plugins/repeat.py and instantiate it then attach it to your running RTMBot.
49+
For example, if your python path includes '/path/to/myproject' and you include `plugins.repeat.RepeatPlugin` in ACTIVE_PLUGINS, it will find the RepeatPlugin class within /path/to/myproject/plugins/repeat.py and instantiate it, then attach it to your running RTMBot.
5150

5251
A Word on Structure
5352
-------
@@ -64,13 +63,16 @@ RtmBot
6463

6564
Add Plugins
6665
-------
66+
Plugins can live within any python module, but we recommend just putting them in ./plugins. (Don't forget to add an __init__.py file to your directory to make it a module -- use `touch __init__.py` within your plugin directory to create one)
6767

6868
To add a plugin, create a file within your plugin directory (./plugins is a good place for it).
6969

70+
mkdir plugins
71+
touch plugins/__init__.py
7072
cd plugins
7173
vi myplugin.py
7274

73-
Add your plugin content into this file. Here's an example that will just print all of the requests it receives. See below for more information on available methods.
75+
Add your plugin content into this file. Here's an example that will just print all of the requests it receives to the console. See below for more information on available methods.
7476

7577
from future import print_function
7678
from rtmbot.core import Plugin
@@ -82,27 +84,44 @@ Add your plugin content into this file. Here's an example that will just print a
8284

8385
You can install as many plugins as you like, and each will handle every event received by the bot indepentently.
8486

85-
To install the example 'repeat' plugin
87+
To create an example 'repeat' plugin:
88+
89+
Open `plugins/repeat.py`
90+
91+
Add the following:
92+
93+
from __future__ import print_function
94+
from __future__ import unicode_literals
95+
96+
from rtmbot.core import Plugin
97+
98+
99+
class RepeatPlugin(Plugin):
86100

87-
mkdir plugins/
88-
cp docs/example-plugins/repeat.py plugins/repeat.py
101+
def process_message(self, data):
102+
if data['channel'].startswith("D"):
103+
self.outputs.append(
104+
[data['channel'], 'from repeat1 "{}" in channel {}'.format(
105+
data['text'], data['channel']
106+
)]
107+
)
89108

90-
The repeat plugin will now be loaded by the bot on startup.
109+
The repeat plugin will now be loaded by the bot on startup. Run `rtmbot` from console to start your RtmBot.
91110

92-
./rtmbot.py
111+
rtmbot
93112

94113
Create Plugins
95114
--------
96115

97116
####Incoming data
98-
All events from the RTM websocket are sent to the registered plugins. To act on an event, create a function definition called process_(api_method) that accepts a single arg. For example, to handle incoming messages:
117+
All events from the RTM websocket are sent to the registered plugins. To act on an event, create a function definition, inside your Plugin class, called process_(api_method) that accepts a single arg for data. For example, to handle incoming messages:
99118

100119
def process_message(self, data):
101120
print data
102121

103122
This will print the incoming message json (dict) to the screen where the bot is running.
104123

105-
Plugins having a method defined as ```catch_all(data)``` will receive ALL events from the websocket. This is useful for learning the names of events and debugging.
124+
Plugins having a method defined as ```catch_all(self, data)``` will receive ALL events from the websocket. This is useful for learning the names of events and debugging.
106125

107126
For a list of all possible API Methods, look here: https://api.slack.com/rtm
108127

@@ -130,37 +149,23 @@ Plugins also have access to the connected SlackClient instance for more complex
130149
####Timed jobs
131150
Plugins can also run methods on a schedule. This allows a plugin to poll for updates or perform housekeeping during its lifetime. This is done by appending a two item array to the crontable array. The first item is the interval in seconds and the second item is the method to run. For example, this will print "hello world" every 10 seconds.
132151

152+
Note that the Job uses the associated Plugin's output array to output back to the RTM stream.
153+
133154
from core import Plugin, Job
134155

135156

136157
class myJob(Job):
137158

138159
def say_hello(self):
139-
self.outputs.append(["C12345667", "hello world"])
160+
self.plugin.outputs.append(["C12345667", "hello world"])
140161

141162

142163
class myPlugin(Plugin):
143164

144165
def register_jobs(self):
145-
job = myJob(10, 'say_hello', self.debug)
166+
job = myJob(10, 'say_hello', self.debug, self)
146167
self.jobs.append(job)
147168

148169

149170
####Plugin misc
150-
The data within a plugin persists for the life of the rtmbot process. If you need persistent data, you should use something like sqlite or the python pickle libraries.
151-
152-
####Direct API Calls
153-
You can directly call the Slack web API in your plugins by including the following import:
154-
155-
from client import slack_client
156-
157-
You can also rename the client on import so it can be easily referenced like shown below:
158-
159-
from client import slack_client as sc
160-
161-
Direct API calls can be called in your plugins in the following form:
162-
163-
sc.api_call("API.method", "parameters")
164-
165-
####Todo:
166-
Some rtm data should be handled upstream, such as channel and user creation. These should create the proper objects on-the-fly.
171+
The data within a plugin persists for the life of the rtmbot process. If you need persistent data, you should use something like sqlite or the python pickle libraries.

client.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

rtmbot/bin/__init__.py

Whitespace-only changes.

rtmbot.py renamed to rtmbot/bin/run_rtmbot.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import sys
44
import os
55
import yaml
6-
import client
6+
7+
from rtmbot import RtmBot
78

89
sys.path.append(os.getcwd())
910

@@ -18,11 +19,16 @@ def parse_args():
1819
)
1920
return parser.parse_args()
2021

21-
# load args with config path
22-
args = parse_args()
23-
config = yaml.load(open(args.config or 'rtmbot.conf', 'r'))
24-
bot = client.init(config)
25-
try:
26-
bot.start()
27-
except KeyboardInterrupt:
28-
sys.exit(0)
22+
23+
def main(args=None):
24+
# load args with config path
25+
args = parse_args()
26+
config = yaml.load(open(args.config or 'rtmbot.conf', 'r'))
27+
bot = RtmBot(config)
28+
try:
29+
bot.start()
30+
except KeyboardInterrupt:
31+
sys.exit(0)
32+
33+
if __name__ == "__main__":
34+
main()

rtmbot/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from slackclient import SlackClient
99

10-
from utils.module_loading import import_string
10+
from rtmbot.utils.module_loading import import_string
1111

1212
sys.dont_write_bytecode = True
1313

@@ -33,7 +33,7 @@ def __init__(self, config):
3333
# TODO: Raise an exception if no SLACK_TOKEN is specified
3434

3535
# get list of directories to search for loading plugins
36-
self.active_plugins = config.get('ACTIVE_PLUGINS', None)
36+
self.active_plugins = config.get('ACTIVE_PLUGINS', [])
3737

3838
# set base directory for logs and plugin search
3939
working_directory = os.path.abspath(os.path.dirname(sys.argv[0]))
@@ -124,6 +124,10 @@ def load_plugins(self):
124124
load any classes with Plugin in the name from any files within those dirs.
125125
'''
126126
self._dbg("Loading plugins")
127+
if not self.active_plugins:
128+
self._dbg("No plugins specified in conf file")
129+
return # nothing to load
130+
127131
for plugin_path in self.active_plugins:
128132
self._dbg("Importing {}".format(plugin_path))
129133

rtmbot/utils/__init__.py

Whitespace-only changes.

setup.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#!/usr/bin/env python
2-
3-
from distutils.core import setup
2+
from setuptools import setup, find_packages
43

54
setup(
65
name='rtmbot',
7-
version='0.2.0',
6+
version='0.2.11',
87
description='A Slack bot written in python that connects via the RTM API.',
9-
author='Ryan Huber',
10-
author_email='[email protected]',
8+
author='Ryan Huber and Jeff Ammons',
9+
author_email='[email protected]',
1110
url='https://github.com/slackhq/python-rtmbot',
12-
packages=['rtmbot'],
11+
packages=find_packages(),
12+
entry_points={'console_scripts': ['rtmbot=rtmbot.bin.run_rtmbot:main']},
13+
install_requires=[
14+
'python-daemon',
15+
'pyyaml',
16+
'slackclient'
17+
]
1318
)

0 commit comments

Comments
 (0)