Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "group" support, where each group can have different recipients
This update pushes the system from using a single http and sms endpoint to using any number of endpoints. Messages can be tagged with a one or more groups, and each group can have one or more endpoints associated with it. This way users can send military or defensive alerts to their alliance without spamming them with messages about local economic conditions.
- Loading branch information
Showing
12 changed files
with
247 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import os | ||
import sys | ||
import yaml | ||
|
||
settings = False | ||
cwd = os.getcwd() | ||
path = cwd + '/.settings.yaml' | ||
assert os.path.isfile(path) | ||
with open(path, 'r') as f: | ||
settings = yaml.load(f) | ||
|
||
def getSettings(): | ||
return settings | ||
|
||
def getServicesFromGroups(groups): | ||
ret_services = [] | ||
for group in groups: | ||
if group in settings['groups']: | ||
if settings['groups'][group] == 'all': | ||
return list(settings['services'].keys()) | ||
for service in settings['groups'][group]: | ||
if service not in ret_services: | ||
ret_services.append(service) | ||
if len(ret_services) < 1: | ||
if 'default' not in groups: | ||
return getServicesFromGroups(['default']) | ||
return ret_services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import importlib | ||
from config import settings | ||
driver_cache = {} | ||
service_cache = {} | ||
assert 'services' in settings | ||
services = settings['services'] | ||
|
||
|
||
def getMessengerDriver(name): | ||
if name in service_cache: | ||
return service_cache[name] | ||
if name not in services: | ||
print('name not in services') | ||
return False | ||
service_settings = services[name] | ||
if 'type' not in service_settings: | ||
print('driver not in service settings') | ||
return False | ||
driver_module = getDriverModule(service_settings['type']) | ||
driver_class = getattr(driver_module, service_settings['type']) | ||
driver = driver_class(service_settings) | ||
|
||
service_cache[name] = driver | ||
return driver | ||
|
||
|
||
# Dynamically import module | ||
def getDriverModule(driver): | ||
if driver in driver_cache: | ||
return driver_cache[driver] | ||
driver = 'services.messengers.%s' % (driver,) | ||
driver_cache[driver] = importlib.import_module(driver, 'screeps_notify') | ||
return driver_cache[driver] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import services.config as config | ||
import requests | ||
|
||
|
||
class http: | ||
|
||
def __init__(self, settings): | ||
self.settings = settings | ||
|
||
def sendMessage(self, notification): | ||
print('sending message from http') | ||
|
||
notification['user'] = config.settings['screeps_username'] | ||
headers = {'user-agent': 'screeps_notify'} | ||
if 'api-key' in self.settings: | ||
headers['x-api-key'] = self.settings['api-key'] | ||
|
||
if 'http_user' in self.settings: | ||
r = requests.post(self.settings['url'], | ||
json=notification, | ||
headers=headers, | ||
auth=(self.settings['http_user'], | ||
self.settings['http_password'])) | ||
else: | ||
r = requests.post(self.settings['url'], | ||
json=notification, | ||
headers=headers) | ||
|
||
if r.status_code != requests.codes.ok: | ||
raise ValueError( | ||
'http request returned an error %s, the response is:\n%s' | ||
% (r.status_code, r.text)) | ||
return r.status_code == requests.codes.ok |
Oops, something went wrong.