Skip to content

Commit

Permalink
Test fixes, yaml cleanup, how-to run telegram bot in README.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephbirkner committed May 15, 2019
1 parent a5ff1d4 commit d517b35
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 0 additions & 9 deletions config/generic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ config:
tickrate: 10
import:
- ravestate_conio
- ravestate_idle
- ravestate_ontology
- ravestate_nlp
- ravestate_interloc
- ravestate_fillers
- ravestate_wildtalk
- ravestate_hibye
Expand All @@ -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
6 changes: 1 addition & 5 deletions config/roboy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions config/telegram_test.yml
Original file line number Diff line number Diff line change
@@ -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: <sexycactus> # 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: <cornycrab> # 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
6 changes: 2 additions & 4 deletions modules/ravestate_telegramio/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,23 +287,21 @@ 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
token = ctx.conf(key=TOKEN_CONFIG_KEY)
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)
Expand Down
5 changes: 3 additions & 2 deletions test/modules/ravestate_telegramio/test_telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pytest_mock import mocker
from testfixtures import LogCapture

import ravestate_rawio as rawio
from ravestate_telegramio.telegram_bot import *


Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit d517b35

Please sign in to comment.