-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from LanceMaverick/admin-dashboard
Improvements to api.
- Loading branch information
Showing
15 changed files
with
656 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Custom | ||
beard_cache/* | ||
db_binary_entries | ||
*.dbbin | ||
beards/*/config.yml | ||
config.py | ||
|
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,7 @@ | ||
A beard named relay_beard for skybeard-2. | ||
|
||
This beard is designed to relay information to the bot via the telegram bot API. When complete, it will provide endpoints like: | ||
|
||
/relayMYSUPERSECRETKEY/sendMessage | ||
|
||
etc.. |
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,84 @@ | ||
import string | ||
import random | ||
|
||
import pyconfig | ||
|
||
from skybeard.beards import BeardChatHandler | ||
from skybeard.bearddbtable import BeardDBTable | ||
from skybeard.decorators import onerror, admin | ||
from skybeard.server import app, web | ||
|
||
|
||
async def make_key(): | ||
"""Makes key for relay commands.""" | ||
return "".join([random.choice(string.ascii_letters) for x in range(20)]) | ||
|
||
|
||
class RelayBeard(BeardChatHandler): | ||
|
||
__userhelp__ = """This beard sets up a relay point for telegram commands. | ||
To use, get a key with /getrelaykey and then you can relay commands to the bot using the following syntax: | ||
<code><hostname>:<port>/relay<key>/<telegram api endpoint></code> | ||
""" | ||
|
||
__commands__ = [ | ||
('getrelaykey', 'get_key', 'Gets key for relay commands.'), | ||
('revokerelaykey', 'revoke_key', 'Revokes your personal relay key.') | ||
] | ||
|
||
# __init__ is implicit | ||
|
||
@admin | ||
@onerror | ||
async def get_key(self, msg): | ||
with type(self).key_table as table: | ||
e = table.find_one(user_id=msg['from']['id']) | ||
if not e: | ||
table.insert( | ||
dict( | ||
user_id=msg['from']['id'], | ||
key=await make_key() | ||
)) | ||
e = table.find_one(user_id=msg['from']['id']) | ||
|
||
await self.sender.sendMessage("Key is: {}".format(e['key'])) | ||
|
||
@admin | ||
@onerror | ||
async def revoke_key(self, msg): | ||
with type(self).key_table as table: | ||
e = table.find_one(user_id=msg['from']['id']) | ||
if e: | ||
table.delete(**e) | ||
await self.sender.sendMessage("Key revoked.") | ||
else: | ||
await self.sender.sendMessage("No key to revoke.") | ||
|
||
|
||
RelayBeard.key_table = BeardDBTable(RelayBeard, "key_table") | ||
|
||
|
||
@app.add_route('/relay{key:[a-zA-Z]+}/{command}', methods=['GET', 'POST']) | ||
async def relay_to_telegram(request): | ||
command_for_telegram = request.match_info['command'] | ||
key = request.match_info['key'] | ||
with RelayBeard.key_table as table: | ||
e = table.find_one(key=key) | ||
|
||
session = pyconfig.get('aiohttp_session') | ||
if e: | ||
if await request.read(): | ||
data = await request.json() | ||
else: | ||
data = None | ||
async with session.request( | ||
request.method, | ||
"https://api.telegram.org/bot{botkey}/{cmd}".format( | ||
botkey=pyconfig.get('key'), | ||
cmd=command_for_telegram), | ||
data=data) as resp: | ||
ret_json = await resp.json() | ||
|
||
return web.json_response(ret_json) |
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,5 @@ | ||
from skybeard.utils import setup_beard | ||
|
||
setup_beard( | ||
"relay_beard", | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
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,65 @@ | ||
(function(){ | ||
var app = angular.module( "APIBeardDemo", ['ng'] ); | ||
app.service( | ||
"APIBeardService", | ||
['$http', function APIBeardFunction($http) { | ||
var that = this; | ||
|
||
this.beards = []; | ||
this.availableCommands = []; | ||
|
||
this.setBeards = function(beards) { | ||
this.beards = beards; | ||
}; | ||
|
||
this.getBeards = function() { | ||
return this.beards; | ||
}; | ||
|
||
this.fetchBeards = function(){ | ||
$http.get("http://localhost:8000/loadedBeards").then( function(data) { | ||
that.setBeards(data.data); | ||
}); | ||
}; | ||
|
||
this.fetchAvailableCommands = function() { | ||
$http.get("http://localhost:8000/availableCommands").then( | ||
function(data) { | ||
that.availableCommands = data.data; | ||
}); | ||
}; | ||
} | ||
]); | ||
|
||
app.controller( | ||
'PanelController', | ||
["APIBeardService", function(APIBeardService) { | ||
this.tab = 1; | ||
|
||
APIBeardService.fetchBeards(); | ||
this.selectTab = function(setTab) { | ||
this.tab = setTab; | ||
// TODO make it not hard coded | ||
if(this.tab === 1) { | ||
APIBeardService.fetchBeards(); | ||
}; | ||
}; | ||
|
||
this.isSelected = function(checkTab) { | ||
return this.tab === checkTab; | ||
}; | ||
}]); | ||
|
||
app.controller( | ||
'loadedBeardsController', | ||
['APIBeardService', function(APIBeardService) { | ||
this.APIBeardService = APIBeardService; | ||
}]); | ||
|
||
app.directive('loadedBeards', function(){ | ||
return { | ||
restrict: "E", | ||
templateUrl: "loaded-beards.html" | ||
}; | ||
}); | ||
})(); // End of closure |
Large diffs are not rendered by default.
Oops, something went wrong.
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,24 @@ | ||
<!DOCTYPE html> | ||
<html ng-app="APIBeardDemo"> | ||
<head> | ||
<link rel="stylesheet" type="text/css" href="bootstrap.min.css" /> | ||
<script type="text/javascript" src="angular.min.js"></script> | ||
<script type="text/javascript" src="app.js"></script> | ||
<script type="text/javascript" src="https://pugjs.org/js/pug.js"></script> | ||
<script> | ||
var pug = require('pug') | ||
var html = pug.renderFile("index.pug"); | ||
</script> | ||
</head> | ||
|
||
<body> | ||
<section ng-controller="PanelController as panel"> | ||
<ul class="nav nav-pills navbar-center"> | ||
<li ng-class="{active:panel.isSelected(1)}"> | ||
<a href ng-click="panel.selectTab(1)">Beards</a> | ||
</li> | ||
</ul> | ||
<loaded-beards ng-show="panel.isSelected(1)"></loaded-beards> | ||
</section> | ||
</body> | ||
</html> |
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,2 @@ | ||
p | ||
| Hello there! |
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,22 @@ | ||
<section ng-controller="loadedBeardsController as lbCtrl"> | ||
<button type="button" | ||
class="btn btn-default" | ||
ng-click="lbCtrl.APIBeardService.loadBeards()">Click me to update beard info!</button> | ||
<div ng-show="lbCtrl.APIBeardService.getBeards().length > 0"> | ||
<ul ng-repeat="beard in lbCtrl.APIBeardService.getBeards()"> | ||
<li>{{beard}}</li> | ||
</ul> | ||
<h3>Available commands</h3> | ||
<button type="button" | ||
class="btn btn-default" | ||
ng-click="lbCtrl.APIBeardService.fetchAvailableCommands()"> | ||
Click me to get available commands. | ||
</button> | ||
<ul ng-repeat="(beard, commands) in lbCtrl.APIBeardService.availableCommands"> | ||
<li>{{ beard }}</li> | ||
<ul ng-repeat="command in commands"> | ||
<li> /{{command.command}} - {{command.hint}}</li> | ||
</ul> | ||
</ul> | ||
</div> | ||
</section> |
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,2 @@ | ||
#!/usr/bin/env bash | ||
python -m http.server 8001 |
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
Oops, something went wrong.