Commit graph

59 commits

Author SHA1 Message Date
PIG208 285a946a1f bot_server: Add support for running botserver from bots registry. 2021-07-29 11:08:33 -07:00
PIG208 4fd29baf2b bot_server: Reuse import_module_from_source to load bot modules from paths.
This removes the need to have `load_module_from_file`.
2021-07-22 12:18:34 +08:00
PIG208 a87303beb1 zulip_botserver: Fix path finding for external bots.
The previous implementation to locate the `bot_dir` is unfortunately
wrong as it doesn't work with the external custom bots.
2021-07-22 11:14:52 +08:00
PIG208 9ce7c52a10 pyupgrade: Reformat with --py36-plus.
This includes mainly fixes of string literals using f-strings or
.format(...), as well as unpacking of list comprehensions.
2021-06-02 18:45:57 -07:00
PIG208 6f3f9bf7e4 black: Reformat without skipping string normalization. 2021-06-02 18:45:57 -07:00
PIG208 fba21bb00d black: Reformat skipping string normalization. 2021-06-02 18:45:57 -07:00
PIG208 5580c68ae5 isort: Reformat using isort. 2021-06-02 18:45:57 -07:00
LoopThrough-i-j dda9e0a638 botserver: Fix mypy error.
Value of type "Optional[Any]" is not indexable error
was originated in PR #682. This is due to request in Flask 2.0.
2021-05-12 15:17:52 -07:00
PIG208 5b32b32914 botserver: Return a valid JSON that is acceptable to outgoing webhooks.
In zulip/zulip@b998138d3a, we introduce
a check for responses from outgoing webhooks that require them to be
a dictionary. This commit fixes the return value of the botserver view
function to accommodate with the change from the serverside.
2021-05-11 09:08:04 -07:00
Anders Kaseorg 19f5b4f6a6 mypy: Fix exec_module type: ignore comments.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 09:58:18 -08:00
LoopThrough-i-j bcf183d2b1 zulip-botserver: Allow passing config via JSON formatted environment variable.
Fixes #485.

Co-authored-by: Alex Vandiver <alexmv@zulip.com>
2021-01-07 00:31:26 -08:00
Anders Kaseorg 6f40bcf745 cleanup: Fix whitespace around parameter equals.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-18 20:31:14 -07:00
Anders Kaseorg 54b2578204 cleanup: Remove unused variables.
Generated by autoflake.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-18 20:31:14 -07:00
Anders Kaseorg 2f6623bbc4 cleanup: Remove unused imports.
Generated by autoflake.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-18 20:31:14 -07:00
Anders Kaseorg e30b3b094b Modernize legacy Python 2 syntax with pyupgrade.
Generated by `pyupgrade --py3-plus --keep-percent-format` followed by
manual indentation fixes.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-18 15:04:36 -07:00
neiljp (Neil Pilgrim) 520a004b85 botserver: Raise exceptions rather than returning them. 2020-03-24 21:25:45 -07:00
neiljp (Neil Pilgrim) fca2404b46 mypy: Add type ignore in botserver/server.py. 2020-03-24 21:25:45 -07:00
Rohitt Vashishtha 74d902d14f botserver: Allow importing custom bot modules.
We can now specify path to a bot's python file as the ini section
header in the botserver's config file. For example:

[~/Documents/helloworld.py]
email=a@b.com
key=XXXX
site=https://b.com
token=XXXX
2019-09-03 13:27:21 -07:00
Tim Abbott fae8e4c0b0 botserver: Add a python3 !# line. 2019-03-18 13:10:06 -07:00
novokrest 50173c105b botserver: Read bot's config from config section with bot's name.
This commit changes the default behavior for a hypothetical bot
named 'bot_one' to the following with the config file:

1. Find and load section named 'bot_one', else...
2. Find and load the first section, else...
3. Exit with error message.

Also, we now gracefully show missing options in a config section
instead of printing the whole stacktrace of the exception.
2018-07-12 20:49:10 +05:30
Robert Hönig 242bcdbadc botserver: Add specific error message for unedited zuliprcs.
Previously, when a user tried to run the Botserver with a
zuliprc but forgot to set the bot name, they were told to
edit the botserverrc file. However, the recommended approach
is to specify the botname with the -b option. This commit
adds an error message specific for this case. It recognizes
zuliprc files by their section header `api`.
2018-06-04 10:14:17 -07:00
Robert Hönig 3ddc8f9b5d botserver: Make bot-name-not-found error more user-friendly.
Previously, when a bot name wasn't found, the Botserver threw
an ImportError with an error message. This results in an
intimidating traceback which is in most cases not helpful to
the user. This commit replaces the ImportError with sys.exit.
2018-06-04 10:14:17 -07:00
Robert Hönig c85b42be9f botserver: Exit with helpful output if botserverrc is invalid.
If the botserverrc file contains empty section headers, it
is very likely that the user forgot to edit the file. This
reminds them to do so.
2018-06-04 10:14:17 -07:00
Robert Hönig f4e0808a87 botserver: Validate token before accepting message.
Previously, the botserver would accept any message sent
to it. This was a security hazard, since an attacker could
impersonate arbitrary users with arbitrary messages. We only
want the Zulip instance where a bot is registered to be able
to send out messages for that bot. To do this, this commits
adds a check for the security token associated with each
outgoing webhook bot. For each bot, its token is stored in
the botserverrc file. The server sends the token along with
each message.
2018-05-30 09:37:33 -04:00
Robert Hönig 64a1306e39 Consistently use Botserver instead of botserver or bot server. 2018-05-29 10:58:37 +02:00
Robert Hönig 762118bbea Rename flaskbotrc to botserverrc. 2018-05-29 10:19:50 +02:00
Robert Hönig 3859bef05f botserver: Make handle_bot logic more concise. 2018-05-29 10:05:32 +02:00
Robert Hönig e6ef34a964 botserver: Strip messages like we do in zulip-run-bot.
Previously, messages weren't stripped at all. This
caused most bots to break and send replies similar to
"I didn't understand your command". Nobody noticed,
because the tests were only validating that replies
were sent, but not the content in them. Thus, this
commit also adds tests to avoid further regressions.
2018-05-29 10:05:19 +02:00
Robert Hönig f1bcf3b9a4 botserver: Remove redundant message check.
Previously, the botserver `handle_bot` routine did two checks
on an incoming message:
* First, it checked if the bot email matches
an email in the flaskbotrc.
* Second, it checked if the bot name that corresponds to an email
has a lib module loaded. However, this must be the case, because
all lib modules for all emails are loaded on initialization. Thus,
this commit removes the second check.
2018-05-29 09:52:14 +02:00
Robert Hönig 6a8cb2965c zulip_botserver: Handle all requests from the root / endpoint.
Previously, the Botserver determined which bot to run by dispatching on
a unique URL endpoint /bots/<botname> for each bot.
Now, instead, the Botserver determines which bot to run by the section
header of the bot in the flaskbotrc.
2018-05-28 15:09:39 +02:00
dkvasov a06fb98f21 zulip_bots and zulip_botserver: Extract common function. 2018-05-17 17:35:09 +03:00
dkvasov 86ab26d2ba zulip_botserver: Get rid of last global variable. 2018-05-16 10:58:09 -07:00
dkvasov a5d6286d85 zulip_botserver: Better message for single bot mode. 2018-05-16 10:58:09 -07:00
dkvasov b06ebdecef zulip_botserver: Add option to test third party configs 2018-05-16 10:50:43 -07:00
dkvasov a8665aaac8 zulip_botserver: Add option to set third party configs for bots. 2018-05-16 10:50:43 -07:00
dkvasov 6542f8c867 zulip_botserver: Add parameter to set custom bot name (for single-bot mode). 2018-05-16 17:41:22 +03:00
dkvasov aa193c1298 zulip_botserver: Extract input parameters. 2018-05-16 17:40:40 +03:00
Guitar1st f90913d54c zulip_botserver: Get rid of some global variables. 2018-05-15 12:32:57 -07:00
Tim Abbott 7b6da265ea zulip_botserver: Reorder imports. 2018-05-15 12:32:45 -07:00
Tim Abbott 70dd05e5a6 zulip_botserver: Enforce that port argument is an int. 2018-05-15 12:32:45 -07:00
Tim Abbott 5ef224f2a2 zulip_botserver: Eliminate the bots_config global variable.
It's easy to just pass it around to the one function that needs it.
2018-05-15 12:32:44 -07:00
Tim Abbott 1e5c0fec1f zulip_botserver: Fix long line for ImportError. 2018-05-15 12:23:40 -07:00
Tim Abbott 9eda19d6cc zulip_botserver: Get rid of old six.moves for configparser. 2018-05-15 12:23:10 -07:00
Guitar1st 2ba6f75fb3 zulip_botserver: Change style of type annotations to python3 2018-05-14 20:00:44 +00:00
Guitar1st 641665c338 zulip_botserver: Remove unused imports and do some other cleanup. 2018-05-14 20:00:44 +00:00
Tim Abbott b1ab308c26 botserver: Fix mypy error. 2018-05-14 10:07:02 -07:00
Tim Abbott 7124f9dc14 bot_server: Call the bot server initialization code. 2018-05-14 08:50:52 -07:00
Steve Howell 536ba1843a Add a command line option to run.py for bot config files.
Before this change, we were looking for config files in
default locations in source control, which is not a good
place to look for them.  Now `run.py` and friends have a
command line argument where users can specify the config
files.

Note that the change to server.py is only a partial fix
to make it so that bots that don't use third party config
files won't crash.  That program needs an overhaul, anyway.
2017-11-28 10:52:13 -08:00
Steve Howell 8a15452525 bots: Require bots_details in ExternalBotHandler.
This parameter was defaulting to a dictionary, which is
a classic Python pitfall.
2017-11-28 10:52:13 -08:00
derAnfaenger bf72a8994a zulip_botserver: Raise exception on wrong flaskbotrc file. 2017-11-10 18:36:20 -08:00