From d517b35713296f63d369191ea6c982544ff975de Mon Sep 17 00:00:00 2001 From: Joseph Birkner Date: Wed, 15 May 2019 15:03:57 +0200 Subject: [PATCH] Test fixes, yaml cleanup, how-to run telegram bot in README. --- README.md | 7 +++- config/generic.yml | 9 ----- config/roboy.yml | 6 +-- config/telegram_test.yml | 40 +++++++++++++++++++ modules/ravestate_telegramio/telegram_bot.py | 6 +-- .../ravestate_telegramio/test_telegram_bot.py | 5 ++- 6 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 config/telegram_test.yml diff --git a/README.md b/README.md index 12960d9..10f5d89 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,12 @@ python3 -m ravestate -f config/generic.yml -f config/keys.yml After the conversation, check the Neo4j interface under `localhost:7474`. It should now contain some nodes! -__Reminder: Whenever you use ravestate from the command line, activate the virtual environment first!__ +__Reminder: Whenever you use ravestate from the command line, source the virtual environment first!__ + +#### Running your Telegram bot + +To test your telegram bot with a custom bot token in your `keys.yml`, +just run `telegram_test.yml` instead of `generic.yml`. This will load the `ravestate_telegramio` module. #### Setting up PyCharm diff --git a/config/generic.yml b/config/generic.yml index 3ac1b82..afea931 100644 --- a/config/generic.yml +++ b/config/generic.yml @@ -19,10 +19,6 @@ config: tickrate: 10 import: - ravestate_conio - - ravestate_idle - - ravestate_ontology - - ravestate_nlp - - ravestate_interloc - ravestate_fillers - ravestate_wildtalk - ravestate_hibye @@ -37,8 +33,3 @@ module: genqa config: drqa_server_address: http://35.246.158.89:5000 roboy_answer_sanity: 1000 - ---- -module: roboyqa -config: - roboy_node_id: 356 diff --git a/config/roboy.yml b/config/roboy.yml index 1296c8b..a9a7fe8 100644 --- a/config/roboy.yml +++ b/config/roboy.yml @@ -18,20 +18,16 @@ module: core config: tickrate: 10 import: - - ravestate_nlp - - ravestate_interloc - ravestate_conio - ravestate_roboyio - ravestate_roboyqa - ravestate_genqa - ravestate_persqa - - ravestate_akinator - - ravestate_idle - - ravestate_fillers - ravestate_wildtalk - ravestate_hibye - ravestate_stalker - ravestate_fillers + #- ravestate_akinator --- module: genqa diff --git a/config/telegram_test.yml b/config/telegram_test.yml new file mode 100644 index 0000000..b8d2e42 --- /dev/null +++ b/config/telegram_test.yml @@ -0,0 +1,40 @@ +# In addition to this file, load a config file `keys.yml`, where +# `keys.yml` should contain the following content: +# +# module: telegramio +# config: +# telegram-token: # Your telegram token here +# --- +# module: ontology +# config: +# neo4j_address: bolt://localhost:7687 # Your neo4j server uri here +# neo4j_username: neo4j # Your neo4j user here +# neo4j_pw: # Your neo4j pw here +# +# Then, start `python3 -m ravestate` with `-f config/telegram_test.yml -f config/keys.yml` + +--- +module: core +config: + tickrate: 10 + import: + - ravestate_telegramio + - ravestate_conio + - ravestate_fillers + - ravestate_wildtalk + - ravestate_hibye + - ravestate_genqa + - ravestate_roboyqa + - ravestate_persqa + #- ravestate_akinator + +--- +module: genqa +config: + drqa_server_address: http://35.246.158.89:5000 + roboy_answer_sanity: 1000 + +--- +module: telegramio +config: + all_in_one_context: True diff --git a/modules/ravestate_telegramio/telegram_bot.py b/modules/ravestate_telegramio/telegram_bot.py index 911053f..aad49aa 100644 --- a/modules/ravestate_telegramio/telegram_bot.py +++ b/modules/ravestate_telegramio/telegram_bot.py @@ -287,9 +287,9 @@ def telegram_output(ctx: rs.ContextWrapper): If all telegram chats should be in the same context, sends the content of rawio:out to every currently active chat. Otherwise it only sends output using the Pipe if it is a child process """ - text = ctx["rawio:out:changed"] + text = ctx[rawio.prop_out.changed()] if not text or not isinstance(text, str): - return + return rs.Resign() text = text.lower().strip() if ctx.conf(key=ALL_IN_ONE_CONTEXT_CONFIG_KEY): # TODO don't instantiate the updater every time @@ -297,13 +297,11 @@ def telegram_output(ctx: rs.ContextWrapper): if not token: logger.error('telegram-token is not set. Shutting down telegramio') return rs.Delete() - updater: Updater = Updater(token) for chat_id in active_chats.keys(): updater.bot.send_message(chat_id=chat_id, text=text) else: child_conn = ctx.conf(key=CHILD_CONN_CONFIG_KEY) - if child_conn: # Child Process -> write to Pipe child_conn.send(text) diff --git a/test/modules/ravestate_telegramio/test_telegram_bot.py b/test/modules/ravestate_telegramio/test_telegram_bot.py index e095e9c..f0bee62 100644 --- a/test/modules/ravestate_telegramio/test_telegram_bot.py +++ b/test/modules/ravestate_telegramio/test_telegram_bot.py @@ -4,6 +4,7 @@ from pytest_mock import mocker from testfixtures import LogCapture +import ravestate_rawio as rawio from ravestate_telegramio.telegram_bot import * @@ -31,6 +32,7 @@ def test_telegram_run(mocker, context_wrapper_fixture: Context): def test_telegram_output(mocker, context_wrapper_fixture: ContextWrapper): + context_wrapper_fixture.spike_payloads = {rawio.prop_out.changed(): "FAKE TEXT"} with LogCapture(attributes=strip_prefix) as log_capture: # Master Process should return Delete and error for telegram output when all_in_one_context is True and no Token with mocker.patch.object(context_wrapper_fixture, "conf", @@ -40,14 +42,13 @@ def test_telegram_output(mocker, context_wrapper_fixture: ContextWrapper): log_capture.check_present('telegram-token is not set. Shutting down telegramio') # Child process should send message via pipe - context_wrapper_fixture.spike_payloads = {"rawio:out:changed": "FAKE TEXT"} child_conn = mocker.patch('multiprocessing.connection.Connection') with mocker.patch.object(child_conn, "send"): with mocker.patch.object(context_wrapper_fixture, "conf", side_effect=generate_fake_conf({ALL_IN_ONE_CONTEXT_CONFIG_KEY: False, CHILD_CONN_CONFIG_KEY: child_conn})): telegram_output(context_wrapper_fixture) - child_conn.send.assert_called_once_with("FAKE TEXT") + child_conn.send.assert_called_once_with("fake text") # Master Process should return Delete and no error for telegram output when all_in_one_context is False with mocker.patch.object(context_wrapper_fixture, "conf",