Deploying bots in production
Usually, work on a bot starts on a laptop. At some point, you’ll want to deploy your bot in a production environment, so that it’ll stay up regardless of what’s happening with your laptop. There are several options for doing so:
- The simplest is running
zulip-run-botinside ascreensession on a server. This works, but if your server reboots, you’ll need to manually restart it, so we don’t recommend it. - Using
supervisordor a similar tool for managing a production process withzulip-run-bot. This consumes a bit of resources (since you need a persistent process running), but otherwise works great. - Using the
zulip-botserver, which is a simple Flask server for running a bot in production, and connecting that to Zulip’s outgoing webhooks feature.
zulip-botserver
Section titled “zulip-botserver”The zulip-botserver is for people who want to:
- Run bots in production.
- Run multiple bots at once.
The zulip-botserver is a Python (Flask) server that implements Zulip’s
outgoing webhooks API. You can of course write your own servers using
the outgoing webhooks API, but the Botserver is designed to make it
easy for a novice Python programmer to write a new bot and deploy it
in production.
How Botserver works
Section titled “How Botserver works”zulip-botserver starts a web server that listens to incoming messages
from your main Zulip server. The sequence of events in a successful
Botserver interaction are:
-
Your bot user is mentioned or receives a direct message:
@**My Bot User** hello world -
The Zulip server sends a POST request to your Botserver endpoint URL:
{"message":{"content":"@**My Bot User** hello world"},"bot_email":"myuserbot-bot@example.com","trigger":"mention","token":"XXXX"}This URL is configured in the Web settings for the bot. The payload above is a shortened illustration, not a complete event.
-
The Botserver searches for a bot to handle the message, and executes your bot’s
handle_messagecode.
Your bot’s code should work just like it does with zulip-run-bot.
The commands below belong to the external zulip_botserver package. Pass the downloaded perorc file explicitly with --config-file; keep the configuration section names unchanged.
Installing the zulip-botserver
Section titled “Installing the zulip-botserver”Create and activate a Python virtualenv, then install the zulip_botserver package:
python3 -m venv .venvsource .venv/bin/activatepython -m pip install zulip_botserverCreate an outgoing webhook bot in your Zulip organization
Section titled “Create an outgoing webhook bot in your Zulip organization”- Navigate to the Bots tab of the Personal settings menu.
- Click Add a new bot.
- Set Bot type to Outgoing webhook and use the native webhook format.
- Set Endpoint URL to
https://botserver.example.com/, replacing the hostname with your reverse proxy address. - Click Add. Your new bot appears in the Bots table.
The Botserver listens on HTTP at 127.0.0.1:5002 by default.
Configure an HTTPS reverse proxy on the same host that forwards the public
endpoint to this local address. The public endpoint must be reachable from
your Zulip server; it is distinct from the Botserver’s local listening port.
Running a bot using the zulip-botserver
Section titled “Running a bot using the zulip-botserver”-
Create your bot in your Zulip organization.
-
Download the
perorcfile for the bot created above. -
Run the Botserver, where
helloworldis the name of the bot you want to run:zulip-botserver --config-file <path_to_perorc> --bot-name=helloworldYou can specify the port number and various other options; run
zulip-botserver --helpto see how to do this.
After configuring the HTTPS endpoint and starting the process, test your Botserver by sending a direct message or a channel message that begins with a mention of the bot.
Running multiple bots using the zulip-botserver
Section titled “Running multiple bots using the zulip-botserver”The zulip-botserver also supports running multiple bots from a single
Botserver process.
-
Create your outgoing webhook bots in your Zulip organization.
-
Download the
botserverrcfile from the Bots tab of the Personal settings menu, by clicking the download () icon above the Your Bots table. -
Open the
botserverrc. It should contain one or more sections that look like this:[helloworld]email=foo-bot@hostnamekey=dOHHlyqgpt5g0tVuVl6NHxDLlc9eFRX4site=https://hostnametoken=aQVQmSd6j6IHphJ9m1jhgHdbnhl5ZcsYEach section contains the configuration for an outgoing webhook bot.
-
For each bot, enter the name of the bot you want to run in the square brackets
[](e.g., the above example applies to thehelloworldbot). To run an external bot, enter the path to the bot’s python file instead, e.g.,[/absolute/path/my_bot_script.py]. -
Run the
zulip-botserverby passing thebotserverrcto it.zulip-botserver --config-file <path-to-botserverrc> --hostname <address> --port <port>If omitted,
hostnamedefaults to127.0.0.1andportto5002.
Running zulip-botserver with supervisord
Section titled “Running zulip-botserver with supervisord”supervisord is a popular tool for running services in production. It helps ensure the service starts on boot, manages log files, restarts the service if it crashes, etc. This section documents how to run the Zulip Botserver using supervisord.
Running the zulip-botserver with supervisord works almost like
running it manually.
-
Install supervisord via your package manager; e.g., on Debian/Ubuntu:
sudo apt-get install supervisor -
Configure supervisord. supervisord stores its configuration in
/etc/supervisor/conf.d.-
Do one of the following:
-
Download the sample config file and store it in
/etc/supervisor/conf.d/zulip-botserver.conf. -
Copy the following section into your existing supervisord config file.
[program:zulip-botserver]command=<absolute-path-to-venv>/bin/zulip-botserver --config-file=<absolute-path-to-botserverrc> --hostname 127.0.0.1 --port 5002user=<service-user>autostart=trueautorestart=truestartsecs=3stdout_logfile=/var/log/zulip-botserver.logredirect_stderr=true
-
-
Replace the
<>placeholders with existing absolute paths and the account that will run the bot. That account must be able to read the configuration. Keep the completecommandon one line; Supervisor does not activate a shell virtualenv.
-
-
Update supervisord to read the configuration file:
sudo supervisorctl rereadsudo supervisorctl update(or you can use
/etc/init.d/supervisord restart, but this is less disruptive if you’re using supervisord for other services as well). -
Test if your setup is successful:
sudo supervisorctl statusThe output should include a line similar to this:
zulip-botserver RUNNING pid 28154, uptime 0:00:27
The standard output of the Botserver will be logged to the path in your supervisord configuration.
If you are hosting the Botserver yourself (as opposed to using a
hosting service that provides SSL), we recommend securing your
Botserver with SSL using an nginx or Apache reverse proxy and
Certbot.
Troubleshooting
Section titled “Troubleshooting”-
Make sure the API key you’re using is for an outgoing webhook bot and you’ve correctly configured the URL for your Botserver.
-
Your Botserver needs to be accessible from your Zulip server over HTTP(S). Make sure any firewall allows the connection. We recommend using zulip-run-bot instead for development/testing on a laptop or other non-server system. From the Zulip server, test the public HTTPS endpoint with
curl -i https://botserver.example.com/. Since this route accepts POST, a GET request should return HTTP 405, for example:HTTP/1.1 405 METHOD NOT ALLOWEDAllow: POST, OPTIONSHTTP 405 only confirms endpoint reachability; then verify a real bot reply.