2017-10-04 21:20:17 +02:00
|
|
|
|
# Writing interactive bots
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2018-01-26 13:37:54 +01:00
|
|
|
|
This guide is about writing and testing interactive bots. We assume
|
|
|
|
|
familiarity with our [guide for running bots](running-bots).
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
|
|
|
|
On this page you'll find:
|
2017-11-15 11:49:15 +01:00
|
|
|
|
|
2017-10-04 21:20:17 +02:00
|
|
|
|
* A step-by-step
|
2017-11-17 19:58:55 +01:00
|
|
|
|
[guide](#installing-a-development-version-of-the-zulip-bots-package)
|
2017-10-04 21:20:17 +02:00
|
|
|
|
on how to set up a development environment for writing bots with all
|
|
|
|
|
of our nice tooling to make it easy to write and test your work.
|
2017-10-04 18:01:31 +02:00
|
|
|
|
* A [guide](#writing-a-bot) on writing a bot.
|
|
|
|
|
* A [guide](#adding-a-bot-to-zulip) on adding a bot to Zulip.
|
2017-11-15 19:12:50 +01:00
|
|
|
|
* A [guide](#testing-a-bots-output) on testing a bot's output.
|
2018-10-20 22:13:51 +02:00
|
|
|
|
* [Documentation](#bot-api) of the bot API.
|
2017-05-21 23:19:07 +02:00
|
|
|
|
* Common [problems](#common-problems) when developing/running bots and their solutions.
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-11-17 14:12:37 +01:00
|
|
|
|
## Installing a development version of the Zulip bots package
|
2017-10-04 18:01:31 +02:00
|
|
|
|
|
2024-02-10 00:10:17 +01:00
|
|
|
|
{start_tabs}
|
2017-10-04 18:01:31 +02:00
|
|
|
|
|
2024-02-10 00:10:17 +01:00
|
|
|
|
1. Clone the [python-zulip-api](https://github.com/zulip/python-zulip-api)
|
|
|
|
|
repository:
|
2017-10-04 18:01:31 +02:00
|
|
|
|
|
2024-02-10 00:10:17 +01:00
|
|
|
|
```
|
|
|
|
|
git clone https://github.com/zulip/python-zulip-api.git
|
|
|
|
|
```
|
2017-10-04 18:01:31 +02:00
|
|
|
|
|
2024-02-10 00:10:17 +01:00
|
|
|
|
1. Navigate into your cloned repository:
|
2017-10-04 18:01:31 +02:00
|
|
|
|
|
2024-02-10 00:10:17 +01:00
|
|
|
|
```
|
|
|
|
|
cd python-zulip-api
|
|
|
|
|
```
|
2017-10-04 18:01:31 +02:00
|
|
|
|
|
2024-02-10 00:10:17 +01:00
|
|
|
|
1. Install all requirements in a Python virtualenv:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
python3 ./tools/provision
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
1. Run the command provided in the final output of the `provision` process to
|
|
|
|
|
enter the new virtualenv. The command will be of the form `source .../activate`.
|
|
|
|
|
|
2024-07-09 17:45:31 +02:00
|
|
|
|
1. You should now see the name of your virtualenv preceding your prompt (e.g.,
|
|
|
|
|
`(zulip-api-py3-venv)`).
|
2024-02-10 00:10:17 +01:00
|
|
|
|
|
|
|
|
|
!!! tip ""
|
|
|
|
|
|
|
|
|
|
`provision` installs the `zulip`, `zulip_bots`, and
|
|
|
|
|
`zulip_botserver` packages in developer mode. This enables you to
|
|
|
|
|
modify these packages and then run your modified code without
|
|
|
|
|
having to first re-install the packages or re-provision.
|
|
|
|
|
|
|
|
|
|
{end_tabs}
|
2017-10-04 18:01:31 +02:00
|
|
|
|
|
|
|
|
|
## Writing a bot
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
The tutorial below explains the structure of a bot `<my-bot>.py`,
|
|
|
|
|
which is the only file you need to create for a new bot. You
|
|
|
|
|
can use this as boilerplate code for developing your own bot.
|
|
|
|
|
|
|
|
|
|
Every bot is built upon this structure:
|
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-10-04 18:01:31 +02:00
|
|
|
|
class MyBotHandler(object):
|
|
|
|
|
'''
|
|
|
|
|
A docstring documenting this bot.
|
|
|
|
|
'''
|
|
|
|
|
|
2017-12-13 02:45:43 +01:00
|
|
|
|
def usage(self):
|
2017-10-04 18:01:31 +02:00
|
|
|
|
return '''Your description of the bot'''
|
|
|
|
|
|
2017-12-13 02:45:43 +01:00
|
|
|
|
def handle_message(self, message, bot_handler):
|
2017-10-04 18:01:31 +02:00
|
|
|
|
# add your code here
|
|
|
|
|
|
|
|
|
|
handler_class = MyBotHandler
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* The class name (in this case *MyBotHandler*) can be defined by you
|
|
|
|
|
and should match the name of your bot. To register your bot's class,
|
|
|
|
|
adjust the last line `handler_class = MyBotHandler` to match your
|
|
|
|
|
class name.
|
|
|
|
|
|
|
|
|
|
* Every bot needs to implement the functions
|
|
|
|
|
* `usage(self)`
|
|
|
|
|
* `handle_message(self, message, bot_handler)`
|
|
|
|
|
|
|
|
|
|
* These functions are documented in the [next section](#bot-api).
|
|
|
|
|
|
|
|
|
|
## Adding a bot to Zulip
|
2017-05-21 23:19:07 +02:00
|
|
|
|
|
2017-08-22 16:38:36 +02:00
|
|
|
|
Zulip's bot system resides in the [python-zulip-api](
|
|
|
|
|
https://github.com/zulip/python-zulip-api) repository.
|
2017-05-21 23:19:07 +02:00
|
|
|
|
|
2017-08-22 16:38:36 +02:00
|
|
|
|
The structure of the bots ecosystem looks like the following:
|
2017-05-21 23:19:07 +02:00
|
|
|
|
|
|
|
|
|
```
|
2017-08-22 16:38:36 +02:00
|
|
|
|
zulip_bots
|
|
|
|
|
└───zulip_bots
|
|
|
|
|
├───bots
|
|
|
|
|
│ ├───bot1
|
|
|
|
|
│ └───bot2
|
|
|
|
|
│ │
|
|
|
|
|
│ ├───bot2.py
|
2017-09-06 14:54:34 +02:00
|
|
|
|
│ ├───bot2.conf
|
|
|
|
|
│ ├───doc.md
|
2017-11-21 16:36:17 +01:00
|
|
|
|
│ ├───requirements.txt
|
2017-09-06 14:54:34 +02:00
|
|
|
|
│ ├───test_bot2.py
|
|
|
|
|
│ ├───assets
|
2017-08-22 16:38:36 +02:00
|
|
|
|
│ │ │
|
2017-09-06 14:54:34 +02:00
|
|
|
|
│ │ └───pic.png
|
|
|
|
|
│ ├───fixtures
|
|
|
|
|
│ │ │
|
|
|
|
|
│ │ └───test1.json
|
|
|
|
|
│ └───libraries
|
2017-08-22 16:38:36 +02:00
|
|
|
|
│ │
|
2017-09-06 14:54:34 +02:00
|
|
|
|
│ └───lib1.py
|
2017-08-22 16:38:36 +02:00
|
|
|
|
├─── lib.py
|
|
|
|
|
├─── test_lib.py
|
|
|
|
|
├─── run.py
|
|
|
|
|
└─── provision.py
|
2017-05-21 23:19:07 +02:00
|
|
|
|
```
|
2017-05-05 22:23:15 +02:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
Each subdirectory in `bots` contains a bot. When writing bots, try to use the structure outlined
|
2017-05-21 23:19:07 +02:00
|
|
|
|
above as an orientation.
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
## Testing a bot's output
|
2017-09-01 17:27:50 +02:00
|
|
|
|
|
|
|
|
|
If you just want to see how a bot reacts to a message, but don't want to set it up on a server,
|
2021-11-19 19:20:49 +01:00
|
|
|
|
we have a little tool to help you out: `zulip-bot-shell`
|
2017-09-01 17:27:50 +02:00
|
|
|
|
|
2017-11-18 00:55:42 +01:00
|
|
|
|
* [Install all requirements](#installing-a-development-version-of-the-zulip-bots-package).
|
2017-09-01 17:27:50 +02:00
|
|
|
|
|
2021-11-19 19:20:49 +01:00
|
|
|
|
* Run `zulip-bot-shell` to test one of the bots in
|
2021-08-31 23:32:37 +02:00
|
|
|
|
[`zulip_bots/bots`](https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots).
|
2018-01-18 21:49:30 +01:00
|
|
|
|
|
|
|
|
|
Example invocations are below:
|
|
|
|
|
|
|
|
|
|
```
|
2021-11-19 19:20:49 +01:00
|
|
|
|
> zulip-bot-shell converter
|
2018-01-18 21:49:30 +01:00
|
|
|
|
|
|
|
|
|
Enter your message: "12 meter yard"
|
|
|
|
|
Response: 12.0 meter = 13.12336 yard
|
|
|
|
|
|
2021-11-19 19:20:49 +01:00
|
|
|
|
> zulip-bot-shell -b ~/followup.conf followup
|
2018-01-18 21:49:30 +01:00
|
|
|
|
|
2021-05-10 07:02:14 +02:00
|
|
|
|
Enter your message: "Task completed"
|
2018-01-18 21:49:30 +01:00
|
|
|
|
Response: stream: followup topic: foo_sender@zulip.com
|
2021-05-10 07:02:14 +02:00
|
|
|
|
from foo_sender@zulip.com: Task completed
|
2018-01-18 21:49:30 +01:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note that the `-b` (aka `--bot-config-file`) argument is for an optional third party
|
2024-07-04 12:14:37 +02:00
|
|
|
|
config file (e.g., ~/giphy.conf), which only applies to certain types of bots.
|
2017-09-01 17:27:50 +02:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
## Bot API
|
2017-01-05 23:23:16 +01:00
|
|
|
|
|
2017-05-25 04:12:34 +02:00
|
|
|
|
This section documents functions available to the bot and the structure of the bot's config file.
|
2017-05-21 23:19:07 +02:00
|
|
|
|
|
|
|
|
|
With this API, you *can*
|
|
|
|
|
|
|
|
|
|
* intercept, view, and process messages sent by users on Zulip.
|
|
|
|
|
* send out new messages as replies to the processed messages.
|
|
|
|
|
|
|
|
|
|
With this API, you *cannot*
|
|
|
|
|
|
|
|
|
|
* modify an intercepted message (you have to send a new message).
|
|
|
|
|
* send messages on behalf of or impersonate other users.
|
2023-06-16 01:15:50 +02:00
|
|
|
|
* intercept direct messages (except for direct messages with the bot as an
|
2017-05-21 23:19:07 +02:00
|
|
|
|
explicit recipient).
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### usage
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
*usage(self)*
|
|
|
|
|
|
|
|
|
|
is called to retrieve information about the bot.
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Arguments
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
* self - the instance the method is called on.
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Return values
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
* A string describing the bot's functionality
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Example implementation
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-12-13 02:45:43 +01:00
|
|
|
|
def usage(self):
|
2016-12-18 16:11:46 +01:00
|
|
|
|
return '''
|
|
|
|
|
This plugin will allow users to flag messages
|
|
|
|
|
as being follow-up items. Users should preface
|
|
|
|
|
messages with "@followup".
|
2024-05-14 21:04:46 +02:00
|
|
|
|
Before running this, make sure to create a channel
|
2016-12-18 16:11:46 +01:00
|
|
|
|
called "followup" that your API user can send to.
|
|
|
|
|
'''
|
|
|
|
|
```
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### handle_message
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2017-06-11 15:03:39 +02:00
|
|
|
|
*handle_message(self, message, bot_handler)*
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-03-05 14:52:12 +01:00
|
|
|
|
handles user message.
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Arguments
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
* self - the instance the method is called on.
|
|
|
|
|
|
2016-12-20 04:30:25 +01:00
|
|
|
|
* message - a dictionary describing a Zulip message
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2024-07-04 12:14:37 +02:00
|
|
|
|
* bot_handler - used to interact with the server, e.g., to send a message
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Return values
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
None.
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Example implementation
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-12-13 02:45:43 +01:00
|
|
|
|
def handle_message(self, message, bot_handler):
|
2016-12-18 16:11:46 +01:00
|
|
|
|
original_content = message['content']
|
|
|
|
|
original_sender = message['sender_email']
|
|
|
|
|
new_content = original_content.replace('@followup',
|
|
|
|
|
'from %s:' % (original_sender,))
|
|
|
|
|
|
2017-06-11 15:03:39 +02:00
|
|
|
|
bot_handler.send_message(dict(
|
2016-12-18 16:11:46 +01:00
|
|
|
|
type='stream',
|
|
|
|
|
to='followup',
|
|
|
|
|
subject=message['sender_email'],
|
|
|
|
|
content=new_content,
|
|
|
|
|
))
|
2017-12-12 15:12:24 +01:00
|
|
|
|
```
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### bot_handler.send_message
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2017-06-11 15:03:39 +02:00
|
|
|
|
*bot_handler.send_message(message)*
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
|
|
|
|
will send a message as the bot user. Generally, this is less
|
|
|
|
|
convenient than *send_reply*, but it offers additional flexibility
|
|
|
|
|
about where the message is sent to.
|
|
|
|
|
|
2017-09-15 16:03:22 +02:00
|
|
|
|
#### Arguments
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
|
|
|
|
* message - a dictionary describing the message to be sent by the bot
|
|
|
|
|
|
2017-09-15 16:03:22 +02:00
|
|
|
|
#### Example implementation
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-06-11 15:03:39 +02:00
|
|
|
|
bot_handler.send_message(dict(
|
2017-05-25 04:12:34 +02:00
|
|
|
|
type='stream', # can be 'stream' or 'private'
|
2024-05-14 21:04:46 +02:00
|
|
|
|
to=channel_name, # either the channel name or user's email
|
2017-05-25 04:12:34 +02:00
|
|
|
|
subject=subject, # message subject
|
|
|
|
|
content=message, # content of the sent message
|
|
|
|
|
))
|
|
|
|
|
```
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### bot_handler.send_reply
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2017-06-11 15:03:39 +02:00
|
|
|
|
*bot_handler.send_reply(message, response)*
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
|
|
|
|
will reply to the triggering message to the same place the original
|
|
|
|
|
message was sent to, with the content of the reply being *response*.
|
|
|
|
|
|
2017-09-15 16:03:22 +02:00
|
|
|
|
#### Arguments
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
|
|
|
|
* message - Dictionary containing information on message to respond to
|
|
|
|
|
(provided by `handle_message`).
|
|
|
|
|
* response - Response message from the bot (string).
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### bot_handler.update_message
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2017-06-11 15:03:39 +02:00
|
|
|
|
*bot_handler.update_message(message)*
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
|
|
|
|
will edit the content of a previously sent message.
|
|
|
|
|
|
2017-09-15 16:03:22 +02:00
|
|
|
|
#### Arguments
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
|
|
|
|
* message - dictionary defining what message to edit and the new content
|
|
|
|
|
|
2017-09-15 16:03:22 +02:00
|
|
|
|
#### Example
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2017-08-22 16:38:36 +02:00
|
|
|
|
From `zulip_bots/bots/incrementor/incrementor.py`:
|
2017-05-25 04:12:34 +02:00
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-06-11 15:03:39 +02:00
|
|
|
|
bot_handler.update_message(dict(
|
2017-05-25 04:12:34 +02:00
|
|
|
|
message_id=self.message_id, # id of message to be updated
|
|
|
|
|
content=str(self.number), # string with which to update message with
|
|
|
|
|
))
|
|
|
|
|
```
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-10-24 15:57:44 +02:00
|
|
|
|
### bot_handler.storage
|
|
|
|
|
|
|
|
|
|
A common problem when writing an interactive bot is that you want to
|
2024-07-04 12:14:37 +02:00
|
|
|
|
be able to store a bit of persistent state for the bot (e.g., for an
|
2017-10-24 15:57:44 +02:00
|
|
|
|
RSVP bot, the RSVPs). For a sufficiently complex bot, you want need
|
|
|
|
|
your own database, but for simpler bots, we offer a convenient way for
|
|
|
|
|
bot code to persistently store data.
|
|
|
|
|
|
|
|
|
|
The interface for doing this is `bot_handler.storage`.
|
|
|
|
|
|
|
|
|
|
The data is stored in the Zulip Server's database. Each bot user has
|
|
|
|
|
an independent storage quota available to it.
|
|
|
|
|
|
|
|
|
|
#### Performance considerations
|
|
|
|
|
|
|
|
|
|
Since each access to `bot_handler.storage` will involve a round-trip
|
|
|
|
|
to the server, we recommend writing bots so that they do a single
|
|
|
|
|
`bot_handler.storage.get` at the start of `handle_message`, and a
|
|
|
|
|
single `bot_handler.storage.put` at the end to submit the state to the
|
|
|
|
|
server. We plan to offer a context manager that takes care of this
|
|
|
|
|
automatically.
|
|
|
|
|
|
|
|
|
|
#### bot_handler.storage.put
|
|
|
|
|
|
|
|
|
|
*bot_handler.storage.put(key, value)*
|
|
|
|
|
|
|
|
|
|
will store the value `value` in the entry `key`.
|
|
|
|
|
|
|
|
|
|
##### Arguments
|
|
|
|
|
|
|
|
|
|
* key - a UTF-8 string
|
|
|
|
|
* value - a UTF-8 string
|
|
|
|
|
|
|
|
|
|
##### Example
|
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-10-24 15:57:44 +02:00
|
|
|
|
bot_handler.storage.put("foo", "bar") # set entry "foo" to "bar"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### bot_handler.storage.get
|
|
|
|
|
|
|
|
|
|
*bot_handler.storage.get(key)*
|
|
|
|
|
|
|
|
|
|
will retrieve the value for the entry `key`.
|
|
|
|
|
|
|
|
|
|
###### Arguments
|
|
|
|
|
|
|
|
|
|
* key - a UTF-8 string
|
|
|
|
|
|
|
|
|
|
##### Example
|
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-10-24 15:57:44 +02:00
|
|
|
|
bot_handler.storage.put("foo", "bar")
|
|
|
|
|
print(bot_handler.storage.get("foo")) # print "bar"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### bot_handler.storage.contains
|
|
|
|
|
|
|
|
|
|
*bot_handler.storage.contains(key)*
|
|
|
|
|
|
|
|
|
|
will check if the entry `key` exists.
|
|
|
|
|
|
|
|
|
|
##### Arguments
|
|
|
|
|
|
|
|
|
|
* key - a UTF-8 string
|
|
|
|
|
|
|
|
|
|
##### Example
|
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2017-10-24 15:57:44 +02:00
|
|
|
|
bot_handler.storage.contains("foo") # False
|
|
|
|
|
bot_handler.storage.put("foo", "bar")
|
|
|
|
|
bot_handler.storage.contains("foo") # True
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### bot_handler.storage marshaling
|
|
|
|
|
|
|
|
|
|
By default, `bot_handler.storage` accepts any object for keys and
|
|
|
|
|
values, as long as it is JSON-able. Internally, the object then gets
|
|
|
|
|
converted to an UTF-8 string. You can specify custom data marshaling
|
|
|
|
|
by setting the functions `bot_handler.storage.marshal` and
|
|
|
|
|
`bot_handler.storage.demarshal`. These functions parse your data on
|
|
|
|
|
every call to `put` and `get`, respectively.
|
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
### Configuration file
|
|
|
|
|
|
2017-12-12 15:12:24 +01:00
|
|
|
|
```
|
2016-12-18 16:11:46 +01:00
|
|
|
|
[api]
|
|
|
|
|
key=<api-key>
|
|
|
|
|
email=<email>
|
|
|
|
|
site=<dev-url>
|
2017-12-12 15:12:24 +01:00
|
|
|
|
```
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-01-05 23:23:16 +01:00
|
|
|
|
* key - the API key you created for the bot; this is how Zulip knows
|
|
|
|
|
the request is from an authorized user.
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2024-07-04 12:14:37 +02:00
|
|
|
|
* email - the email address of the bot, e.g., `some-bot@zulip.com`
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-01-05 23:23:16 +01:00
|
|
|
|
* site - your development environment URL; if you are working on a
|
|
|
|
|
development environment hosted on your computer, use
|
|
|
|
|
`localhost:9991`
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
## Writing tests for bots
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
|
|
|
|
Bots, like most software that you want to work, should have unit tests. In this section,
|
|
|
|
|
we detail our framework for writing unit tests for bots. We require that bots in the main
|
2021-08-31 23:32:37 +02:00
|
|
|
|
[`python-zulip-api`](https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots)
|
2017-09-15 16:05:48 +02:00
|
|
|
|
repository include a reasonable set of unit tests, so that future developers can easily
|
|
|
|
|
refactor them.
|
|
|
|
|
|
|
|
|
|
*Unit tests for bots make heavy use of mocking. If you want to get comfortable with mocking,
|
|
|
|
|
mocking strategies, etc. you should check out our [mocking guide](
|
2017-11-16 18:54:01 +01:00
|
|
|
|
https://zulip.readthedocs.io/en/latest/testing/testing-with-django.html#testing-with-mocks).*
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### A simple example
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
|
|
|
|
Let's have a look at a simple test suite for the [`helloworld`](
|
2021-08-31 23:32:37 +02:00
|
|
|
|
https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots/helloworld)
|
2017-12-14 12:38:33 +01:00
|
|
|
|
bot.
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```python
|
2019-05-15 00:35:12 +02:00
|
|
|
|
from zulip_bots.test_lib import StubBotTestCase
|
|
|
|
|
|
|
|
|
|
class TestHelpBot(StubBotTestCase):
|
2020-05-09 00:10:17 +02:00
|
|
|
|
bot_name: str = "helloworld"
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2019-05-15 00:35:12 +02:00
|
|
|
|
def test_bot(self) -> None:
|
|
|
|
|
dialog = [
|
|
|
|
|
('', 'beep boop'),
|
|
|
|
|
('help', 'beep boop'),
|
|
|
|
|
('foo', 'beep boop'),
|
|
|
|
|
]
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2019-05-15 00:35:12 +02:00
|
|
|
|
self.verify_dialog(dialog)
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2019-05-15 00:35:12 +02:00
|
|
|
|
```
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2017-12-14 12:38:33 +01:00
|
|
|
|
The `helloworld` bot replies with "beep boop" to every message @-mentioning it. We
|
|
|
|
|
want our test to verify that the bot **actually** does that.
|
|
|
|
|
|
|
|
|
|
Note that our helper method `verify_dialog` simulates the conversation for us, and
|
|
|
|
|
we just need to set up a list of tuples with expected results.
|
|
|
|
|
|
|
|
|
|
The best way to learn about bot tests is to read all the existing tests in the
|
|
|
|
|
`bots` subdirectories.
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### Testing your test
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
|
|
|
|
Once you have written a test suite, you want to verify that everything works as expected.
|
|
|
|
|
|
|
|
|
|
* To test a bot in [Zulip's bot directory](
|
2021-08-31 23:32:37 +02:00
|
|
|
|
https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots):
|
2017-09-15 16:05:48 +02:00
|
|
|
|
`tools/test-bots <botname>`
|
|
|
|
|
|
|
|
|
|
* To run all bot tests: `tools/test-bots`
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
### Advanced testing
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
|
|
|
|
This section shows advanced testing techniques for more complicated bots that have
|
|
|
|
|
configuration files or interact with third-party APIs.
|
|
|
|
|
*The code for the bot testing library can be found [here](
|
2021-08-31 23:32:37 +02:00
|
|
|
|
https://github.com/zulip/python-zulip-api/blob/main/zulip_bots/zulip_bots/test_lib.py).*
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Testing bots with config files
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
|
|
|
|
Some bots, such as [Giphy](
|
2021-08-31 23:32:37 +02:00
|
|
|
|
https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots/giphy),
|
2017-12-14 12:38:33 +01:00
|
|
|
|
support or require user configuration options to control how the bot works.
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2017-12-14 12:38:33 +01:00
|
|
|
|
To test such a bot, you can use the following pattern:
|
|
|
|
|
|
|
|
|
|
with self.mock_config_info(dict(api_key=12345)):
|
|
|
|
|
# self.verify_reply(...)
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2017-12-14 12:38:33 +01:00
|
|
|
|
`mock_config_info()` replaces the actual step of reading configuration from the file
|
|
|
|
|
system and gives your test "dummy data" instead.
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Testing bots with internet access
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
|
|
|
|
Some bots, such as [Giphy](
|
2021-08-31 23:32:37 +02:00
|
|
|
|
https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots/giphy),
|
2021-05-14 00:16:30 +02:00
|
|
|
|
depend on a third-party service, such as the Giphy web app, in order to work. Because
|
2017-09-15 16:05:48 +02:00
|
|
|
|
we want our test suite to be reliable and not add load to these third-party APIs, tests
|
|
|
|
|
for these services need to have "test fixtures": sample HTTP request/response pairs to
|
|
|
|
|
be used by the tests. You can specify which one to use in your test code using the
|
|
|
|
|
following helper method:
|
|
|
|
|
|
|
|
|
|
with self.mock_http_conversation('test_fixture_name'):
|
|
|
|
|
# self.assert_bot_response(...)
|
|
|
|
|
|
|
|
|
|
`mock_http_conversation(fixture_name)` patches `requests.get` and returns the data specified
|
2019-07-11 12:45:26 +02:00
|
|
|
|
in the file `fixtures/{fixture_name}.json`. Use the following JSON code as a skeleton for new
|
2018-02-05 15:05:40 +01:00
|
|
|
|
fixtures:
|
2021-05-26 18:48:00 +02:00
|
|
|
|
```json
|
2018-02-05 15:05:40 +01:00
|
|
|
|
{
|
|
|
|
|
"request": {
|
|
|
|
|
"api_url": "http://api.example.com/",
|
|
|
|
|
"params": {
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"response": {
|
|
|
|
|
},
|
|
|
|
|
"response-headers": {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
For an example, check out the [giphy bot](
|
2021-08-31 23:32:37 +02:00
|
|
|
|
https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots/giphy).
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2019-08-08 20:04:16 +02:00
|
|
|
|
*Tip: You can use [requestbin](https://requestbin.com/) or a similar
|
|
|
|
|
tool to capture payloads from the service your bot is interacting
|
|
|
|
|
with.*
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2017-10-04 18:01:31 +02:00
|
|
|
|
#### Examples
|
2017-09-15 16:05:48 +02:00
|
|
|
|
|
2021-08-31 23:32:37 +02:00
|
|
|
|
Check out our [bots](https://github.com/zulip/python-zulip-api/tree/main/zulip_bots/zulip_bots/bots)
|
2017-09-15 16:05:48 +02:00
|
|
|
|
to see examples of bot tests.
|
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
## Common problems
|
2017-01-05 23:23:16 +01:00
|
|
|
|
|
2016-12-18 16:11:46 +01:00
|
|
|
|
* I modified my bot's code, yet the changes don't seem to have an effect.
|
2017-09-28 10:17:37 +02:00
|
|
|
|
* Ensure that you restarted the `zulip-run-bot` script.
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
|
|
|
|
* My bot won't start
|
2016-12-20 03:25:35 +01:00
|
|
|
|
* Ensure that your API config file is correct (download the config file from the server).
|
2018-07-06 23:26:34 +02:00
|
|
|
|
* Ensure that you bot script is located in `zulip_bots/bots/<my-bot>/`
|
2017-05-05 19:54:51 +02:00
|
|
|
|
* Are you using your own Zulip development server? Ensure that you run your bot outside
|
|
|
|
|
the Vagrant environment.
|
2017-05-23 00:28:18 +02:00
|
|
|
|
* Some bots require Python 3. Try switching to a Python 3 environment before running
|
2017-08-22 16:38:36 +02:00
|
|
|
|
your bot.
|
2016-12-18 16:11:46 +01:00
|
|
|
|
|
2017-05-05 22:26:57 +02:00
|
|
|
|
## Future direction
|
|
|
|
|
|
|
|
|
|
The long-term plan for this bot system is to allow the same
|
2017-08-22 16:38:36 +02:00
|
|
|
|
`ExternalBotHandler` code to eventually be usable in several contexts:
|
2017-05-05 22:26:57 +02:00
|
|
|
|
|
|
|
|
|
* Run directly using the Zulip `call_on_each_message` API, which is
|
|
|
|
|
how the implementation above works. This is great for quick
|
|
|
|
|
development with minimal setup.
|
|
|
|
|
* Run in a simple Python webserver server, processing messages
|
|
|
|
|
received from Zulip's outgoing webhooks integration.
|
|
|
|
|
* For bots merged into the mainline Zulip codebase, enabled via a
|
|
|
|
|
button in the Zulip web UI, with no code deployment effort required.
|
2024-02-10 00:10:17 +01:00
|
|
|
|
|
|
|
|
|
## Related articles
|
|
|
|
|
|
|
|
|
|
* [Non-webhook integrations](/api/non-webhook-integrations)
|
|
|
|
|
* [Running bots](/api/running-bots)
|
|
|
|
|
* [Deploying bots](/api/deploying-bots)
|
|
|
|
|
* [Configuring the Python bindings](/api/configuring-python-bindings)
|