Skip to content

Commit

Permalink
Merge pull request #3 from screepers/groups
Browse files Browse the repository at this point in the history
Add "group" support, where each group can have different recipients
  • Loading branch information
tedivm authored Jun 25, 2017
2 parents 54cf8c4 + 8e674ad commit 7f6508e
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 127 deletions.
67 changes: 59 additions & 8 deletions .settings.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,65 @@ screeps_username:
screeps_password:
screeps_ptr: false

# Your Account SID from www.twilio.com/console
twilio_sid:
services:

# Your Auth Token from www.twilio.com/console
twilio_token:
sms:
# Set driver to twilio
type: sms

# You SMS number from twilio. https://www.twilio.com/console/phone-numbers/dashboard
sms_from: '+15555555555'
# Your Account SID from www.twilio.com/console
twilio_sid:

# This should be the number you want to receive the texts.
sms_to: '+15555555555'
# Your Auth Token from www.twilio.com/console
twilio_token:

# You SMS number from twilio. https://www.twilio.com/console/phone-numbers/dashboard
sms_from: '+15555555555'

# This should be the number you want to receive the texts.
sms_to: '+15555555555'

alliance:
# Set driver to HTTP
type: http

# Specify a url
url:

# Provide an API key for AWS Gateway (optional)
api-key:

logs:
# Set driver to HTTP
type: http

# Specify a url
url:

# Provide a username for basic http authentication
http_user:

# Provide a password for basic http authentication
http_password:


slack:

# Set driver to slack
type: slack

# Get webhook from slack.
webhook_url: 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'


groups:

default:
- sms
- logs

economy:
- logs
- slack

defense: all
93 changes: 73 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,95 @@ The settings file is a yaml file. Begin by copying the settings.dist file to
cp .settings.dist.yaml .settings.yaml
```

The settings file is in yaml and takes various authentication tokens.

### Screeps Settings

```yaml
# Copy this to .settings.yaml and fill it out.

# Screeps account info
screeps_username:
screeps_password:
screeps_ptr: false
```
### Define Services
Services are used by the system to send messages. Currently there are three
service types (HTTP, Slack, and SMS), each of which can be used multiple times
(for instance, you can define multiple slack hooks to send different types of
messages to different channels).
```yaml
services:

sms:
# Set driver to twilio
type: sms

# Your Account SID from www.twilio.com/console
twilio_sid:

## To enable SMS Messages fill out the information below.
# Your Auth Token from www.twilio.com/console
twilio_token:

# Your Account SID from www.twilio.com/console
twilio_sid:
# You SMS number from twilio. https://www.twilio.com/console/phone-numbers/dashboard
sms_from: '+15555555555'

# Your Auth Token from www.twilio.com/console
twilio_token:
# This should be the number you want to receive the texts.
sms_to: '+15555555555'

# You SMS number from twilio. https://www.twilio.com/console/phone-numbers/dashboard
sms_from: '+15555555555'
alliance:
# Set driver to HTTP
type: http

# This should be the number you want to receive the texts.
sms_to: '+15555555555'
# Specify a url
url: https://example.execute-api.us-east-1.amazonaws.com/prod/service

# Provide an API key for AWS Gateway (optional)
api-key:

## To enable HTTP Messages fill out the information below.
logs:
# Set driver to HTTP
type: http

# URL to post to.
http:
# Specify a url
url: https://example.execute-api.us-east-1.amazonaws.com/prod/service

# Username, if required.
http_user:
# Provide an API key for AWS Gateway (optional)
api-key:

# Password, if required.
http_pass:
slack:
# Set driver to SLACK
type: slack

# AWS Lambda API Key.
api-key:
# Specify a url
webhook_url:
```
### Define Groups
Groups define which services get used when a notification is sent. At a minimum
a `default` group should be set.

Groups can either be an array of services or the string `all` (which will make
sure the group sends a message to all available services).

```yaml
groups:
default:
- sms
- logs
- slack
economy:
- sms
- logs
defense: all
```


## Installation
Expand Down Expand Up @@ -120,6 +167,12 @@ Notify('Test Message')

// Will send immediately, but only once every 100 ticks.
Notify('Rate Limited Message', 100)

// Will send only to the economy group services, and only once every 100 ticks.
Notify('Rate Limited Group Message', 100, ['economy'])

// Will send immediately but only to the defense group services
Notify('Rate Limited Group Message', false, ['defense'])
```


Expand Down
1 change: 0 additions & 1 deletion bin/screepsnotify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ then
else
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
fi
cd $DIR/..

ENV="$DIR/../env/bin/activate"
if [ ! -f $ENV ]; then
Expand Down
20 changes: 13 additions & 7 deletions js/Notify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

var Notify = function (message, limit) {
var Notify = function (message, limit=false, groups=false) {

if(!groups) {
groups = ['default']
}

// If no limit then send immediately (and potentially repeatedly)
if(!limit) {
Expand All @@ -9,13 +13,15 @@ var Notify = function (message, limit) {

// In cases where there are limits we have to record the history.

var queue_message = message + '::' + groups.join('_')

if(!Memory.__notify_history) {
Memory.__notify_history = {}
}

// If the message was sent in the last LIMIT ticks then don't send again.
if(!!Memory.__notify_history[message]) {
var lastSent = Memory.__notify_history[message]
if(!!Memory.__notify_history[queue_message]) {
var lastSent = Memory.__notify_history[queue_message]
if(lastSent >= Game.time - limit) {
return
} else {
Expand All @@ -25,18 +31,19 @@ var Notify = function (message, limit) {
}

// Record message in history and send it.
Memory.__notify_history[message] = Game.time
Notify.queueMessage(message)
Memory.__notify_history[queue_message] = Game.time
Notify.queueMessage(message, groups)
return 0
}


Notify.queueMessage = function (message) {
Notify.queueMessage = function (message, groups) {
if(!Memory.__notify) {
Memory.__notify = []
}
Memory.__notify.push({
'message': message,
'groups': groups,
'tick': Game.time
})
}
Expand All @@ -60,5 +67,4 @@ Notify.cleanHistory = function (limit) {
}
}


module.exports = Notify
2 changes: 1 addition & 1 deletion js/notify.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "notify" {
function Notify(message: string, limit?: number): void;
function Notify(message: string, limit?: number, groups?: string[]): void;
export = Notify;
}
Loading

0 comments on commit 7f6508e

Please sign in to comment.