From 0ee2023d41fe96c36231aaa463c993aafe1b1ba4 Mon Sep 17 00:00:00 2001 From: Robert Hafner Date: Sun, 14 Aug 2016 17:14:52 -0700 Subject: [PATCH] Send http posts if configured --- README.md | 20 +++++++++++ screeps_notify/notify.py | 75 ++++++++++++++++++++++++++++++++-------- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b36d680..528c73e 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ screeps_username: screeps_password: screeps_ptr: false +## To enable SMS Messages fill out the information below. + # Your Account SID from www.twilio.com/console twilio_sid: @@ -41,9 +43,27 @@ sms_from: '+15555555555' # This should be the number you want to receive the texts. sms_to: '+15555555555' + + +## To enable HTTP Messages fill out the information below. + +# URL to post to. +http: + +# Username, if required. +http_user: + +# Password, if required. +http_pass: + +# AWS Lambda API Key. +api-key: ``` + + + ## Installation ### vagrant diff --git a/screeps_notify/notify.py b/screeps_notify/notify.py index fdf3e7d..a6ff06d 100755 --- a/screeps_notify/notify.py +++ b/screeps_notify/notify.py @@ -1,7 +1,9 @@ #!/usr/bin/env python +import requests from screeps import ScreepsConnection import sys +import time from twilio.rest import TwilioRestClient import logging import os @@ -25,13 +27,18 @@ def getSettings(): return getSettings.settings getSettings.settings = False + def getScreepsConnection(): if not getScreepsConnection.sconn: settings = getSettings() - getScreepsConnection.sconn = ScreepsConnection(u=settings['screeps_username'],p=settings['screeps_password'],ptr=settings['screeps_ptr']) + getScreepsConnection.sconn = ScreepsConnection( + u=settings['screeps_username'], + p=settings['screeps_password'], + ptr=settings['screeps_ptr']) return getScreepsConnection.sconn getScreepsConnection.sconn = False + def getNotifications(): sconn = getScreepsConnection() notifications = sconn.memory(path='__notify') @@ -39,6 +46,7 @@ def getNotifications(): return False return notifications['data'] + def clearNotifications(tick=0): print 'clearing sent messages' sconn = getScreepsConnection() @@ -46,17 +54,54 @@ def clearNotifications(tick=0): javascript_clear += "if(typeof limit == 'undefined') var limit = 0; Memory.__notify = _.filter(Memory.__notify, function(notification){ return notification.tick > this.limit }.bind({'limit':limit}))" sconn.console(javascript_clear) -def sendSMS(message): + +def sendSMS(notification): + message = notification['message'] settings = getSettings() - settings['sms_to'] - if not sendSMS.client: - sendSMS.client = TwilioRestClient(settings['twilio_sid'], settings['twilio_token']) - message = sendSMS.client.messages.create( + + if 'twilio_sid' not in settings: + print('skipping sms due to lack of settings.') + return + + smsclient = TwilioRestClient(settings['twilio_sid'], + settings['twilio_token']) + message = smsclient.messages.create( body=message, to=settings['sms_to'], # Replace with your phone number - from_=settings['sms_from']) # Replace with your Twilio number + from_=settings['sms_from']) # Replace with your Twilio number print(message.sid) -sendSMS.client = False + + +def sendHTTP(notification): + settings = getSettings() + + if 'http' not in settings: + print('skipping http due to lack of settings.') + return + + notification['user'] = settings['screeps_username'] + headers = { + 'user-agent': 'screeps_notify', + } + + if 'api-key' in settings: + headers['x-api-key'] = settings['api-key'] + + print headers + if 'http_user' in settings: + r = requests.post(settings['http'], + json=notification, + headers=headers, + auth=(settings['http_user'], + settings['http_password'])) + else: + r = requests.post(settings['http'], + json=notification, + headers=headers) + + print r.text + print r.status_code + return r.status_code == requests.codes.ok class App(): @@ -64,24 +109,24 @@ class App(): def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/null' - #self.stdout_path = base_directory + '/screepsnotify.out' + # self.stdout_path = base_directory + '/screepsnotify.out' self.stderr_path = base_directory + '/screepsnotify.err' - self.pidfile_path = base_directory + '/screepsnotify.pid' + self.pidfile_path = base_directory + '/screepsnotify.pid' self.pidfile_timeout = 5 - def run(self): logging.basicConfig(level=logging.WARN) logger = logging.getLogger("ScreepsNotify") logger.setLevel(logging.INFO) - formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + formatter = logging.Formatter( + "%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler = logging.FileHandler(base_directory + "/screepsnotify.log") handler.setFormatter(formatter) logger.addHandler(handler) while True: notifications = getNotifications() - if len(notifications) <= 0: + if not notifications or len(notifications) <= 0: print 'No notifications to send.' sys.exit(0) limit = 0 @@ -89,9 +134,9 @@ def run(self): for notification in notifications: if notification['tick'] > limit: limit = notification['tick'] - sendSMS(notification['message']) + sendSMS(notification) + sendHTTP(notification) clearNotifications(limit) - time.sleep(5)