webhooks/beeminder: Improve message formatting and punctuation.

This commit is contained in:
Eeshan Garg 2019-04-12 00:10:28 -02:30 committed by Tim Abbott
parent b7ea727087
commit 33b0bf3cb4
2 changed files with 20 additions and 13 deletions

View File

@ -11,11 +11,10 @@ class BeeminderHookTests(WebhookTestCase):
def test_beeminder_derail(self, time: Any) -> None: def test_beeminder_derail(self, time: Any) -> None:
time.return_value = 1517739100 # 5.6 hours from fixture value time.return_value = 1517739100 # 5.6 hours from fixture value
expected_topic = u"beekeeper" expected_topic = u"beekeeper"
expected_message = '\n'.join([ expected_message = """
'You are going to derail from goal **gainweight** in **{:0.1f} hours**'.format(5.6), You are going to derail from goal **gainweight** in **5.6 hours**. You need **+2 in 7 days (60)** to avoid derailing.
' You need **+2 in 7 days (60)** to avoid derailing', * Pledge: **0$** :relieved:
' * Pledge: **0$** :relieved:' """.strip()
])
self.send_and_test_stream_message('derail', self.send_and_test_stream_message('derail',
expected_topic, expected_topic,
@ -26,11 +25,11 @@ class BeeminderHookTests(WebhookTestCase):
def test_beeminder_derail_worried(self, time: Any) -> None: def test_beeminder_derail_worried(self, time: Any) -> None:
time.return_value = 1517739100 # 5.6 hours from fixture value time.return_value = 1517739100 # 5.6 hours from fixture value
expected_topic = u"beekeeper" expected_topic = u"beekeeper"
expected_message = '\n'.join([ expected_message = """
'You are going to derail from goal **gainweight** in **{:0.1f} hours**'.format(5.6), You are going to derail from goal **gainweight** in **5.6 hours**. You need **+2 in 7 days (60)** to avoid derailing.
' You need **+2 in 7 days (60)** to avoid derailing', * Pledge: **5$** :worried:
' * Pledge: **5$** :worried:' """.strip()
])
self.send_and_test_stream_message('derail_worried', self.send_and_test_stream_message('derail_worried',
expected_topic, expected_topic,
expected_message, expected_message,

View File

@ -8,6 +8,10 @@ from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile from zerver.models import UserProfile
import time import time
MESSAGE_TEMPLATE = ("You are going to derail from goal **{goal_name}** in **{time:0.1f} hours**. "
"You need **{limsum}** to avoid derailing.\n"
"* Pledge: **{pledge}$** {expression}\n")
def get_time(payload: Dict[str, Any]) -> Any: def get_time(payload: Dict[str, Any]) -> Any:
losedate = payload["goal"]["losedate"] losedate = payload["goal"]["losedate"]
time_remaining = (losedate - time.time())/3600 time_remaining = (losedate - time.time())/3600
@ -29,8 +33,12 @@ def api_beeminder_webhook(request: HttpRequest, user_profile: UserProfile,
expression = ':relieved:' expression = ':relieved:'
topic = u'beekeeper' topic = u'beekeeper'
body = u"You are going to derail from goal **{}** in **{:0.1f} hours**\n \ body = MESSAGE_TEMPLATE.format(
You need **{}** to avoid derailing\n * Pledge: **{}$** {}" goal_name=goal_name,
body = body.format(goal_name, time_remain, limsum, pledge, expression) time=time_remain,
limsum=limsum,
pledge=pledge,
expression=expression
)
check_send_webhook_message(request, user_profile, topic, body) check_send_webhook_message(request, user_profile, topic, body)
return json_success() return json_success()