Skip to content

IFTTT notifications#2053

Merged
liiight merged 6 commits intoFlexget:developfrom
kroozo:ifttt_notification
Jan 3, 2018
Merged

IFTTT notifications#2053
liiight merged 6 commits intoFlexget:developfrom
kroozo:ifttt_notification

Conversation

@kroozo
Copy link
Copy Markdown
Contributor

@kroozo kroozo commented Jan 2, 2018

Notification plugin to send notification to ifttt.com's webhook service.
(https://ifttt.com/maker_webhooks)

Motivation for changes:

Adds a simple notification plugin to send the notification to IFTTT webhook trigger that then can be used
in an IFTTT applet.

Config usage if relevant (new plugin or updated schema):

notify:
  task:
    via:
      - ifttt:
          keys:
            - some api key
            - other api key
          event: test

Log and/or tests output (preferably both):

2018-01-02 13:53 INFO     ifttt         notify_download Sent notification to key: some_valid_api_key
2018-01-02 13:53 ERROR    ifttt         notify_download Error sending to https://maker.ifttt.com/trigger/test/with/key/bad_key: 401 Client Error: Unauthorized for url: https://maker.ifttt.com/trigger/test/with/key/bad_key

kroozo added 2 commits January 2, 2018 15:44
Notification plugin to send notification to ifttt.com's webhook service.
(https://ifttt.com/maker_webhooks)
:param str title: message subject
:param dict config: plugin config
"""
notification_body = dict()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just declare the dict with the data directly

notification_body = dict()
notification_body['value1'] = title
notification_body['value2'] = message
for key in config['keys']:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work if keys is a string. You need to make sure it's a list. Look into other notifiers with prepare_data methods

for key in config['keys']:
url = self.url_template.format(config['event'], key)
try:
res = self.session.post(url, json = notification_body)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove spaces around key word calls.
Also, using Session() will raise on errors by default, so you'll never reach that else clause. Just remove the conditionals and assume that the notification was successful. Handle the failure in the except section

else:
log.error("Error sending to: {}: {} {} -- {}".format(url, res.status_code, res.reason, res.text))
except HTTPError as e:
log.error("Error sending to {}: {}".format(url, e))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please raise plugin.PluginWarning here. Look into other notifiers

log.info("Sent notification to key: {}".format(key))
else:
log.error("Error sending to: {}: {} {} -- {}".format(url, res.status_code, res.reason, res.text))
except HTTPError as e:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to catch all RequestError here, just in case

raise PluginWarning("Failed to send notifications")

def prepare_config(self, config):
if isinstance(config['keys'], StringTypes):
Copy link
Copy Markdown
Contributor

@cvium cvium Jan 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not isinstance(config['keys'], list):

I don't think we've ever used StringTypes. Either use str or the example I made.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list it is then :)
I am not a big fan of str with all the unicode hassle, that is why I usually resort to StringTypes. And testing for a list has other caveats (they end up being tuples sometimes for eg) but if it is more consistent with the rest of your code base, then sure. jsonschema makes it moot anyway most probably here.

@kroozo
Copy link
Copy Markdown
Contributor Author

kroozo commented Jan 3, 2018

@liiight Fixed it up based on your comments, thanks.

Few questions:

  • I've moved the PluginWarning after the loop so that a bad endpoint does not kill the rest. However, would it make sense to raise a PluginError instead if all of them fail?
  • Tests: does it make any sense for you to add any? The whole thing is pretty straightforward. I can add unit tests around some of these, like key is string, key is list, bad key at the front, whatever, but for me that mostly just sounds like a burden that will need to be changed if anyone touches this anyway. The only thing that would be beneficial is checking if the IFTTT endpoint is breaking, but I would be hesitant to hook some random account of theirs into your CI.

Also, while I was scratching my own itch I realized that you actually have a feature request for this: https://feathub.com/Flexget/Flexget/+19

url = self.url_template.format(config['event'], key)
try:
self.session.post(url, json=notification_body)
log.info("Sent notification to key: {}".format(key))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let the logger handle its own interpolation:
log.info("Sent notification to key: %s", key)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@liiight liiight merged commit 1cc6e0d into Flexget:develop Jan 3, 2018
@kroozo kroozo deleted the ifttt_notification branch January 3, 2018 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants