Writing interactive bots
Interactive bots react to messages through the compatible REST API. This guide
uses the separate upstream zulip_bots framework to develop and test a bot.
The product Python SDK is pero; see client libraries.
Installing pero does not install the bot framework or rename its packages
and commands. The framework uses its own upstream client dependencies, so keep
its development environment separate from your pero application environment.
Confirm compatibility and support for your deployment with the operator.
- Installing a development version of the bot framework
- Bot template
- Testing a bot’s output
- Troubleshooting
Installing a development version of zulip_bots
Section titled “Installing a development version of zulip_bots”This will provide convenient tooling for developing and testing your bot.
-
Clone the python-zulip-api repository:
git clone https://github.com/zulip/python-zulip-api.git -
Navigate into your cloned repository:
cd python-zulip-api -
Install all requirements in a Python virtualenv:
python3 ./tools/provision -
Run the command provided in the final output of the
provisionprocess to enter the new virtualenv. The command will be of the formsource .../activate. -
You should now see the name of your virtualenv preceding your prompt (e.g.,
(zulip-api-py3-venv)).
Bot template
Section titled “Bot template”The only file required for a new bot is <my-bot>.py. It has the following
structure:
class MyBotHandler(object): ''' A docstring documenting this bot. '''
def usage(self): return '''Your description of the bot'''
def handle_message(self, message, bot_handler): # add your code here pass
handler_class = MyBotHandlerThe class name (in this case MyBotHandler) can be defined by you
and must be a valid Python class name. To register your bot’s class,
adjust the last line handler_class = MyBotHandler to match your
class name.
Every bot must implement the usage(self) and handle_message(self, message, bot_handler) functions. They are described in detail in the
interactive bots API documentation.
All the files associated with a bot (e.g., tests, documentation, etc.) should be
placed in the same directory as <my-bot>.py.
Testing a bot’s output
Section titled “Testing a bot’s output”You can see how a bot reacts to a message without setting it up on a server,
using the zulip-bot-shell tool.
- Install all requirements.
- Run
zulip-bot-shellto test one of the bots inzulip_bots/bots.
Example invocations:
> zulip-bot-shell converter
Enter your message: "12 meter yard"Response: 12.0 meter = 13.12336 yard
> zulip-bot-shell -b ~/followup.conf followup
Enter your message: "Task completed"Response: stream: followup topic: foo_sender@zulip.com from foo_sender@zulip.com: Task completedNote that the -b (aka --bot-config-file) argument is for an optional third party
config file (e.g., ~/giphy.conf), which only applies to certain types of bots.
Troubleshooting
Section titled “Troubleshooting”I modified my bot’s code, yet the changes don’t seem to have an effect
Section titled “I modified my bot’s code, yet the changes don’t seem to have an effect”Ensure that you restarted the zulip-run-bot script.
My bot won’t start
Section titled “My bot won’t start”- Ensure that your API config file is correct (download the config file from the server).
- Ensure that your bot script path is correct. Built-in bots are located in
zulip_bots/bots/<my-bot>/; custom bots may be launched from their own path. - Are you using your own Zulip development server? Ensure that you run your bot outside the Vagrant environment.