Skip to content

Commit

Permalink
Make this a generic hotline / call forwarder service.
Browse files Browse the repository at this point in the history
SMS accepted.

Instead of hardcoding the "PyCascades Code of Conduct Hotline",
retrieve the hotline description from environmemt variable.
  • Loading branch information
Mariatta committed Mar 2, 2019
1 parent 7156f41 commit c5bf0b1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
3 changes: 3 additions & 0 deletions readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ In Heroku, set the environment variables:
- ``ZAPIER_CATCH_HOOK_RECORDING_URL``: The `Webhooks By Zapier <https://zapier.com/page/webhooks/>`_ url.
This is only needed if ``AUTO_RECORD`` is ``True``.

- ``HOTLINE_DESC``: the description of this Hotline. For example: ``PyCascades Code of Conduct Hotline``
or ``PyCascades Head Office``.


Downloading the recording
-------------------------
Expand Down
55 changes: 30 additions & 25 deletions tests/test_webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
routes,
)

mock_api_key = "apikey"
MOCK_API_KEY = "apikey"

mock_api_secret = "sssh"
MOCK_API_SECRET = "sssh"

mock_private_key = """
MOCK_PRIVATE_KEY = """
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCxephXEnphnbrX
spKQR5hXvOFik/0PKvwt2u9/RXyae81rPH+bzJ1iQoYhnNjeGyluNLMewFvKMP9m
Expand All @@ -26,41 +26,43 @@
"""


mock_phone_numbers = [
MOCK_PHONE_NUMBERS = [
{"name": "Mariatta", "phone": "16040001234"},
{"name": "Miss Islington", "phone": "17782223333"},
]

MOCK_HOTLINE_DESC = "PyCascades Head Office"

def test_get_nexmo_client(monkeypatch):
monkeypatch.setitem(os.environ, "NEXMO_API_KEY", mock_api_key)
monkeypatch.setitem(os.environ, "NEXMO_API_SECRET", mock_api_secret)
monkeypatch.setitem(os.environ, "NEXMO_API_KEY", MOCK_API_KEY)
monkeypatch.setitem(os.environ, "NEXMO_API_SECRET", MOCK_API_SECRET)
monkeypatch.setitem(os.environ, "NEXMO_APP_ID", "app_id")
monkeypatch.setitem(os.environ, "NEXMO_PRIVATE_KEY_VOICE_APP", mock_private_key)
monkeypatch.setitem(os.environ, "NEXMO_PRIVATE_KEY_VOICE_APP", MOCK_PRIVATE_KEY)
monkeypatch.setitem(os.environ, "HOTLINE_DESC", MOCK_HOTLINE_DESC)

client = get_nexmo_client()
assert client.application_id == "app_id"
assert client.private_key == mock_private_key
assert client.api_key == mock_api_key
assert client.api_secret == mock_api_secret
assert client.private_key == MOCK_PRIVATE_KEY
assert client.api_key == MOCK_API_KEY
assert client.api_secret == MOCK_API_SECRET


def test_get_phone_numbers(monkeypatch):
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(mock_phone_numbers))
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(MOCK_PHONE_NUMBERS))

phone_numbers = get_phone_numbers()
assert phone_numbers == mock_phone_numbers
assert phone_numbers == MOCK_PHONE_NUMBERS


def test_get_phone_number_owner(monkeypatch):
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(mock_phone_numbers))
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(MOCK_PHONE_NUMBERS))

owner = get_phone_number_owner(mock_phone_numbers[0]["phone"])
assert owner == mock_phone_numbers[0]["name"]
owner = get_phone_number_owner(MOCK_PHONE_NUMBERS[0]["phone"])
assert owner == MOCK_PHONE_NUMBERS[0]["name"]


def test_get_phone_number_owner_not_found(monkeypatch):
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(mock_phone_numbers))
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(MOCK_PHONE_NUMBERS))

owner = get_phone_number_owner("0000000")
assert owner is None
Expand All @@ -84,9 +86,10 @@ def send_message(self, params=None, **kwargs):

@pytest.fixture
def webservice_cli(loop, aiohttp_client, monkeypatch):
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(mock_phone_numbers))
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(MOCK_PHONE_NUMBERS))
monkeypatch.setitem(os.environ, "NEXMO_APP_ID", "app_id")
monkeypatch.setitem(os.environ, "NEXMO_PRIVATE_KEY_VOICE_APP", mock_private_key)
monkeypatch.setitem(os.environ, "NEXMO_PRIVATE_KEY_VOICE_APP", MOCK_PRIVATE_KEY)
monkeypatch.setitem(os.environ, "HOTLINE_DESC", MOCK_HOTLINE_DESC)

app = web.Application()
app.router.add_routes(routes)
Expand All @@ -95,14 +98,16 @@ def webservice_cli(loop, aiohttp_client, monkeypatch):

@pytest.fixture
def webservice_cli_autorecord(loop, aiohttp_client, monkeypatch):
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(mock_phone_numbers))
monkeypatch.setitem(os.environ, "PHONE_NUMBERS", json.dumps(MOCK_PHONE_NUMBERS))
monkeypatch.setitem(os.environ, "NEXMO_APP_ID", "app_id")
monkeypatch.setitem(os.environ, "NEXMO_PRIVATE_KEY_VOICE_APP", mock_private_key)
monkeypatch.setitem(os.environ, "NEXMO_PRIVATE_KEY_VOICE_APP", MOCK_PRIVATE_KEY)
monkeypatch.setitem(
os.environ,
"ZAPIER_CATCH_HOOK_RECORDING_FINISHED_URL",
"https://hooks.zapier.com/1111/2222",
)
monkeypatch.setitem(os.environ, "HOTLINE_DESC", MOCK_HOTLINE_DESC)

monkeypatch.setitem(os.environ, "AUTO_RECORD", "True")

app = web.Application()
Expand All @@ -123,7 +128,7 @@ async def test_answer_call(webservice_cli):
assert response[0]["action"] == "talk"
assert (
response[0]["text"]
== "You've reached the PyCascades Code of Conduct Hotline."
== f"You've reached the {MOCK_HOTLINE_DESC}."
)

assert response[1]["action"] == "conversation"
Expand All @@ -150,7 +155,7 @@ async def test_answer_conference_call(webservice_cli):
assert response[0]["action"] == "talk"
assert (
response[0]["text"]
== "Hello Mariatta, connecting you to PyCascades hotline."
== f"Hello Mariatta, connecting you to {MOCK_HOTLINE_DESC}."
)

assert response[1]["action"] == "conversation"
Expand All @@ -171,7 +176,7 @@ async def test_answer_call_auto_record(webservice_cli_autorecord):
assert response[0]["action"] == "talk"
assert (
response[0]["text"]
== "You've reached the PyCascades Code of Conduct Hotline. This call is recorded."
== f"You've reached the {MOCK_HOTLINE_DESC}. This call is recorded."
)

assert response[1]["action"] == "conversation"
Expand Down Expand Up @@ -204,7 +209,7 @@ async def test_inbound_sms(webservice_cli):
assert len(nexmo_client.messages_sent) == 3

# Check that the organizers got the message.
for n, phone_number_dict in enumerate(mock_phone_numbers):
for n, phone_number_dict in enumerate(MOCK_PHONE_NUMBERS):
message = nexmo_client.messages_sent[n]
assert message["from"] == hotline_number
assert message["to"] == phone_number_dict["phone"]
Expand All @@ -214,4 +219,4 @@ async def test_inbound_sms(webservice_cli):
response_message = nexmo_client.messages_sent[-1]
assert response_message["to"] == reporter_number
assert response_message["from"] == hotline_number
assert "CoC" in response_message["text"]
assert MOCK_HOTLINE_DESC in response_message["text"]
10 changes: 7 additions & 3 deletions webservice/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def get_phone_number_owner(phone_number):
return None


def get_hotline_description():
"""Return the description of this hotline, e.g: CoC hotline, or Head office"""
return os.environ.get("HOTLINE_DESC")

def is_auto_recording():
autorecord_flag = os.environ.get("AUTO_RECORD", "false")
return autorecord_flag.lower() == "true"
Expand All @@ -79,7 +83,7 @@ async def answer_call(request):
hotline_number = request.rel_url.query["to"]
conversation_uuid = request.rel_url.query["conversation_uuid"].strip()
call_uuid = request.rel_url.query["uuid"].strip()
greeting = "You've reached the PyCascades Code of Conduct Hotline."
greeting = f"You've reached the {get_hotline_description()}."

conversation_ncco = {
"action": "conversation",
Expand Down Expand Up @@ -155,7 +159,7 @@ async def answer_conference_call(request):
ncco = [
{
"action": "talk",
"text": f"Hello {phone_number_owner}, connecting you to PyCascades hotline.",
"text": f"Hello {phone_number_owner}, connecting you to {get_hotline_description()}.",
},
{
"action": "conversation",
Expand Down Expand Up @@ -199,7 +203,7 @@ async def inbound_sms(request):
{
"from": hotline_number,
"to": from_number,
"text": "Thanks for contacting the CoC hotline. Someone should follow-up shortly. Note: they may follow up from a different number.",
"text": f"Thanks for contacting the {get_hotline_description()}. Someone should follow-up shortly. Note: they may follow up from a different number.",
}
)

Expand Down

0 comments on commit c5bf0b1

Please sign in to comment.