2017-10-04 21:11:27 +02:00
|
|
|
# Interactive bots
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2017-10-04 21:11:27 +02:00
|
|
|
Zulip's API has a powerful framework for interactive bots that react
|
2018-01-26 13:37:54 +01:00
|
|
|
to messages in Zulip. This page documents how to run a bot
|
2017-10-04 21:11:27 +02:00
|
|
|
implemented using that framework, both on your laptop for quick
|
|
|
|
testing as well in a production server environment.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
|
|
|
On this page you'll find:
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
* A step-by-step [tutorial](#running-a-bot) on how to run a bot.
|
|
|
|
* A [guide](#zulip-botserver) on running a Zulip botserver.
|
2017-10-02 13:19:41 +02:00
|
|
|
* Common [problems](#common-problems) when developing/running bots and their solutions.
|
|
|
|
|
2017-11-17 14:12:37 +01:00
|
|
|
## Installing the Zulip bots package
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
## Running a bot
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2018-05-14 02:58:03 +02:00
|
|
|
This guide will show you how to run an **existing** Zulip bot
|
2017-10-04 18:12:34 +02:00
|
|
|
found in [`zulip_bots/bots`](
|
2018-05-14 02:58:03 +02:00
|
|
|
https://github.com/zulip/python-zulip-api/tree/master/zulip_bots/zulip_bots/bots).
|
2017-10-02 13:19:41 +02:00
|
|
|
|
|
|
|
You need:
|
|
|
|
|
|
|
|
* An account in an organization on a Zulip server
|
|
|
|
(e.g. [chat.zulip.org](https://chat.zulip.org) or
|
|
|
|
yourSubdomain.zulipchat.com, or your own development server).
|
|
|
|
Within that Zulip organization, users will be able to interact with
|
|
|
|
your bot.
|
|
|
|
* A computer where you're running the bot from.
|
|
|
|
|
2017-12-14 22:00:15 +01:00
|
|
|
**Note: Please be considerate when testing experimental bots on public servers such as chat.zulip.org.**
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2017-10-04 21:11:27 +02:00
|
|
|
1. Run `pip install zulip_bots` to install the package.
|
|
|
|
|
2017-11-15 11:49:15 +01:00
|
|
|
*Hint: Do you want to install the latest development version? Check
|
|
|
|
out [this](
|
2017-11-18 00:55:42 +01:00
|
|
|
writing-bots#installing-a-development-version-of-the-zulip-bots-package)
|
2017-11-15 11:49:15 +01:00
|
|
|
guide.*
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Register a new bot user on the Zulip server's web interface.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
|
|
|
* Log in to the Zulip server.
|
|
|
|
* Navigate to *Settings (<i class="fa fa-cog"></i>)* -> *Your bots* -> *Add a new bot*.
|
|
|
|
Select *Generic bot* for bot type, fill out the form and click on *Create bot*.
|
|
|
|
* A new bot user should appear in the *Active bots* panel.
|
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Download the bot's `zuliprc` configuration file to your computer.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2018-05-14 03:02:22 +02:00
|
|
|
* Go to *Settings* -> *Your bots*.
|
2017-10-02 13:19:41 +02:00
|
|
|
* In the *Active bots* panel, click on the little green download icon
|
2017-11-21 00:49:55 +01:00
|
|
|
to download its configuration file *zuliprc* (the structure of this file is
|
2017-11-15 11:49:15 +01:00
|
|
|
explained [here](writing-bots#configuration-file)).
|
2017-11-21 00:49:55 +01:00
|
|
|
* The file will be downloaded to some place like `~/Downloads/zuliprc` (depends
|
|
|
|
on your browser and OS).
|
|
|
|
* Copy the file to a destination of your choice, e.g. to `~/zuliprc-my-bot`.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Run the bot.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
|
|
|
* Run
|
|
|
|
```
|
2017-11-21 00:49:55 +01:00
|
|
|
zulip-run-bot <bot-name> --config-file ~/zuliprc-my-bot
|
2017-10-02 13:19:41 +02:00
|
|
|
```
|
2017-11-21 00:49:55 +01:00
|
|
|
(using the path to the `zuliprc` file from step 3).
|
2017-10-02 13:19:41 +02:00
|
|
|
* Check the output of the command. It should start with the text
|
|
|
|
the `usage` function returns, followed by logging output similar
|
|
|
|
to this:
|
|
|
|
|
2017-12-14 22:00:15 +01:00
|
|
|
INFO:root:starting message handling...
|
|
|
|
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2017-10-04 21:11:27 +02:00
|
|
|
* Congrats! Your bot is running.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2017-10-04 21:11:27 +02:00
|
|
|
You can now play around with the bot and get it configured the way you
|
|
|
|
like. Eventually, you'll probably want to run it in a production
|
|
|
|
environment where it'll stay up, by deploying it on a server using the
|
|
|
|
Zulip Botserver.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
### Installing the Zulip Botserver
|
|
|
|
|
|
|
|
Install the `zulip_botserver` PyPI package using `pip`:
|
|
|
|
```
|
|
|
|
pip install zulip_botserver
|
|
|
|
```
|
|
|
|
|
|
|
|
### Running bots using the Zulip Botserver
|
|
|
|
|
|
|
|
1. Register new bot users on the Zulip server's web interface.
|
|
|
|
|
|
|
|
* Log in to the Zulip server.
|
|
|
|
* Navigate to *Settings (<i class="fa fa-cog"></i>)* -> *Your bots* -> *Add a new bot*.
|
|
|
|
Select *Outgoing webhook* for bot type, fill out the form and click on *Create bot*.
|
|
|
|
* A new bot user should appear in the *Active bots* panel.
|
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Download the `flaskbotrc` from the `your-bots` settings page. It
|
2017-10-02 13:19:41 +02:00
|
|
|
contains the configuration details for all the active outgoing
|
2017-11-21 00:49:55 +01:00
|
|
|
webhook bots. It's structure is very similar to that of zuliprc.
|
2017-10-02 13:19:41 +02:00
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Run the Zulip Botserver by passing the `flaskbotrc` to it. The
|
2017-10-02 13:19:41 +02:00
|
|
|
command format is:
|
|
|
|
|
|
|
|
```
|
|
|
|
zulip-bot-server --config-file <path_to_flaskbotrc> --hostname <address> --port <port>
|
|
|
|
```
|
|
|
|
|
|
|
|
If omitted, `hostname` defaults to `127.0.0.1` and `port` to `5002`.
|
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
r. Now set up the outgoing webhook service which will interact with
|
2017-12-30 16:32:36 +01:00
|
|
|
the server: Create an **Outgoing webhook** bot with its Endpoint URL
|
2017-10-02 13:19:41 +02:00
|
|
|
of the form:
|
|
|
|
|
|
|
|
```
|
|
|
|
http://<hostname>:<port>/bots/<bot_name>
|
|
|
|
```
|
|
|
|
|
|
|
|
`bot_name` refers to the name in the email address you specified
|
|
|
|
for the bot. It can be obtained by removing `-bot@*.*` from the
|
|
|
|
bot email: For example, the bot name of a bot with an email
|
|
|
|
`followup-bot@zulip.com` is `followup`.
|
|
|
|
|
|
|
|
In the development environment, an outgoing webhook bot and
|
|
|
|
corresponding service already exist, with the email
|
|
|
|
`outgoing-webhook@zulip.com`. This can be used for interacting
|
|
|
|
with flask server bots.
|
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Congrats, everything is set up! Test your botserver like you would
|
2017-10-02 13:19:41 +02:00
|
|
|
test a normal bot.
|
|
|
|
|
2017-11-17 14:12:37 +01:00
|
|
|
### Running Zulip Botserver with supervisord
|
2017-10-02 13:19:41 +02:00
|
|
|
|
|
|
|
[supervisord](http://supervisord.org/) 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.
|
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Install *supervisord* via your package manager; e.g. on Debian/Ubuntu:
|
2017-10-02 13:19:41 +02:00
|
|
|
```
|
|
|
|
sudo apt-get install supervisor
|
|
|
|
```
|
|
|
|
|
|
|
|
1. Configure *supervisord*. *supervisord* stores its configuration in
|
|
|
|
`/etc/supervisor/conf.d`.
|
|
|
|
* Do **one** of the following:
|
2017-11-16 20:44:00 +01:00
|
|
|
* Download the [sample config file][supervisord-config-file]
|
2017-10-02 13:19:41 +02:00
|
|
|
and store it in `/etc/supervisor/conf.d/zulip-botserver.conf`.
|
|
|
|
* Copy the following section into your existing supervisord config file.
|
2017-12-14 22:00:15 +01:00
|
|
|
|
|
|
|
[program:zulip-bot-server]
|
|
|
|
command=zulip-bot-server --config-file=<path/to/your/flaskbotrc>
|
|
|
|
--hostname <address> --port <port>
|
|
|
|
startsecs=3
|
|
|
|
stdout_logfile=/var/log/zulip-botserver.log ; all output of your botserver will be logged here
|
|
|
|
redirect_stderr=true
|
|
|
|
|
2017-10-02 13:19:41 +02:00
|
|
|
* Edit the `<>` sections according to your preferences.
|
|
|
|
|
2017-11-16 20:44:00 +01:00
|
|
|
[supervisord-config-file]: https://raw.githubusercontent.com/zulip/python-zulip-api/master/zulip_botserver/zulip-botserver-supervisord.conf
|
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Update *supervisord* to read the configuration file:
|
2017-10-02 13:19:41 +02:00
|
|
|
```
|
|
|
|
supervisorctl reread
|
|
|
|
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).
|
|
|
|
|
2018-05-14 02:43:32 +02:00
|
|
|
1. Test if your setup is successful:
|
2017-10-02 13:19:41 +02:00
|
|
|
```
|
|
|
|
supervisorctl status
|
|
|
|
```
|
|
|
|
The output should include a line similar to this:
|
|
|
|
> zulip-bot-server RUNNING pid 28154, uptime 0:00:27
|
|
|
|
|
|
|
|
The standard output of the bot server will be logged to the path in
|
|
|
|
your *supervisord* configuration.
|
|
|
|
|
|
|
|
## Common problems
|
|
|
|
|
|
|
|
* My bot won't start
|
|
|
|
* Ensure that your API config file is correct (download the config file from the server).
|
2017-12-12 15:12:24 +01:00
|
|
|
* Ensure that your bot script is located in zulip_bots/bots/<my-bot>/
|
2017-10-02 13:19:41 +02:00
|
|
|
* Are you using your own Zulip development server? Ensure that you run your bot outside
|
|
|
|
the Vagrant environment.
|
|
|
|
* Some bots require Python 3. Try switching to a Python 3 environment before running
|
|
|
|
your bot.
|