Skip to content

Commit

Permalink
Send http posts if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Aug 15, 2016
1 parent 4104e96 commit 0ee2023
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
75 changes: 60 additions & 15 deletions screeps_notify/notify.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,73 +27,116 @@ 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')
if 'data' not in notifications:
return False
return notifications['data']


def clearNotifications(tick=0):
print 'clearing sent messages'
sconn = getScreepsConnection()
javascript_clear = 'var limit=' + str(tick) + ';'
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():

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
print 'Sending notifications.'
for notification in notifications:
if notification['tick'] > limit:
limit = notification['tick']
sendSMS(notification['message'])
sendSMS(notification)
sendHTTP(notification)
clearNotifications(limit)

time.sleep(5)


Expand Down

0 comments on commit 0ee2023

Please sign in to comment.