Merge pull request #162 from pintman/spaceapi

added module space api
This commit is contained in:
Ville Ranki 2021-08-09 00:00:46 +03:00 committed by GitHub
commit d62e6d8079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 0 deletions

View File

@ -274,6 +274,19 @@ Then just:
mxma requires all commands to be run as bot owner.
#### SpaceAPI
Polls the status of Hack- and Makerspaces that provide an endpoint
that conforms to the [SpaceAPI](https://spaceapi.io/) protocol and notifies
about changes of the opening status.
To add a new endpoint simply use
`!spaceapi add https://hackspace.example.org/status`
For Admins: A template and I18N can be configured via settings of
the module. Use `!bot export spacepi`, then change the
settings and import again with `!bot import spacepi SETTINGS`.
### Url
Watches all messages in a room and if a url is found tries to fetch it and

66
modules/spaceapi.py Normal file
View File

@ -0,0 +1,66 @@
from modules.common.pollingservice import PollingService
from urllib.request import urlopen
import json
import time
class MatrixModule(PollingService):
def __init__(self, name):
super().__init__(name)
self.accountroomid_laststatus = {}
self.template = '{spacename} is now {open_closed}'
self.i18n = {'open': 'open 🔓', 'closed': 'closed 🔒'}
async def poll_implementation(self, bot, account, roomid, send_messages):
self.logger.debug(f'polling space api {account}.')
spacename, is_open = MatrixModule.open_status(account)
open_str = self.i18n['open'] if is_open else self.i18n['closed']
text = self.template.format(spacename=spacename, open_closed=open_str)
self.logger.debug(text)
last_status = self.accountroomid_laststatus.get(account+roomid, False)
if send_messages and last_status != is_open:
await bot.send_text(bot.get_room_by_id(roomid), text)
self.accountroomid_laststatus[account+roomid] = is_open
bot.save_settings()
@staticmethod
def open_status(spaceurl):
with urlopen(spaceurl, timeout=5) as response:
js = json.load(response)
return js['space'], js['state']['open']
def get_settings(self):
data = super().get_settings()
data['laststatus'] = self.accountroomid_laststatus
data['template'] = self.template
data['i18n'] = self.i18n
return data
def set_settings(self, data):
super().set_settings(data)
if data.get('laststatus'):
self.accountroomid_laststatus = data['laststatus']
if data.get('template'):
self.template = data['template']
if data.get('i18n'):
self.i18n = data['i18n']
def help(self):
return "Notify about Space-API status changes (open or closed)."
def long_help(self, bot, event, **kwargs):
text = self.help() + \
' This is a polling service. Therefore there are additional ' + \
'commands: list, debug, poll, clear, add URL, del URL\n' + \
'!spaceapi add URL: to add a space-api endpoint\n' + \
'!spaceapi list: to list the endpoint configured for this room.\n' + \
f'I will look for changes roughly every {self.poll_interval_min} ' + \
'minutes. Find out more about Space-API at https://spaceapi.io/.'
if bot.is_owner(event):
text += '\nA template and I18N can be configured via settings of ' + \
'the module. Use "!bot export spacepi", then change the ' + \
'settings and import again with "!bot import spacepi SETTINGS".'
return text