Skip to content

Commit

Permalink
Issue #149: Provide custom success/failure messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmo committed Oct 20, 2020
1 parent 46b3ed0 commit c59c8b5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions amazon_dash/confirmations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ def __init__(self, data):
raise InvalidConfig(extra_body='{} is a required parameter for {} confirmation'.format(key, self.name))
self.data = data

def send(self, message, success=True):
def format_message(self, message, success=True, **kwargs):
format_message = self.data.get('success_message' if success else 'failure_message')
format_message = format_message or '{message}'
return format_message.format(message=message, success=success, **kwargs)

def send(self, message, success=True, **kwargs):
raise NotImplementedError


class DisabledConfirmation(ConfirmationBase):
name = 'disabled'

def send(self, message, success=True):
def send(self, message, success=True, **kwargs):
pass


Expand All @@ -30,7 +35,8 @@ class TelegramConfirmation(ConfirmationBase):
name = 'telegram'
required_fields = ('token', 'to')

def send(self, message, success=True):
def send(self, message, success=True, **kwargs):
message = self.format_message(message, success, **kwargs)
try:
r = requests.post(self.url_base.format(self.data['token']), dict(
text=message, chat_id=self.data['to'],
Expand Down Expand Up @@ -74,7 +80,8 @@ def get_data(self, body, title=''):
data[self.to_field] = self.data[self.to_field]
return data

def send(self, message, success=True):
def send(self, message, success=True, **kwargs):
message = self.format_message(message, success, **kwargs)
try:
r = requests.post(self.url_base, json=self.get_data(message),
headers={'Access-Token': self.data['token']})
Expand Down
2 changes: 1 addition & 1 deletion amazon_dash/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def send_confirmation(self, message, success=True):
if not self.confirmation:
return
try:
self.confirmation.send(message, success)
self.confirmation.send(message, success, src=self.src, **self.data)
except Exception as e:
logger.warning('Error sending confirmation on device {}: {}'.format(self.name, e))

Expand Down

0 comments on commit c59c8b5

Please sign in to comment.