diff --git a/README.md b/README.md index c109097..6ef89f6 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,17 @@ The module uses a demo API Key which can be replaced by your own api key by sett You can create one at https://api.nasa.gov/#signUp + +### Inspirobot + +Query a randomly generated inspirational poster from https://inspirobot.me/ upload and send to the room. + +Command: + +* !inspire - Generate inspiration and post to the room +* !inspire help - Show this help + + ### Wolfram Alpha Make queries to Wolfram Alpha diff --git a/modules/inspire.py b/modules/inspire.py new file mode 100644 index 0000000..c5eeab2 --- /dev/null +++ b/modules/inspire.py @@ -0,0 +1,54 @@ +import html +import requests + +from modules.common.module import BotModule + + +class MatrixModule(BotModule): + + def __init__(self, name): + super().__init__(name) + self.url_generator_url = "https://inspirobot.me/api?generate=true" + self.matrix_uri_cache = dict() + + async def matrix_message(self, bot, room, event): + self.logger.debug(f"room: {room.name} sender: {event.sender} wants to be inspired!") + + args = event.body.split() + + if len(args) == 1: + await self.send_inspiration(bot, room, self.url_generator_url) + return + elif len(args) == 2: + if args[1] == "help": + await self.command_help(bot, room) + return + await bot.send_text(room, f"unknown command: {args}") + await self.command_help(bot, room) + + async def send_inspiration(self, bot, room, url_generator_url): + self.logger.debug(f"Asking inspirobot for pic url at {url_generator_url}") + response = requests.get(url_generator_url) + + if response.status_code != 200: + self.logger.error("unable to request inspirobot api. response: [status: %d text: %s]", response.status_code, response.text) + return await bot.send_text(room, f"sorry. something went wrong accessing inspirobot: {response.status_code}: {response.text}") + + pic_url = response.text + self.logger.debug("Sending image with src='%s'", pic_url) + await bot.upload_and_send_image(room, pic_url) + + def help(self): + return """I'm InspiroBot. + I am an artificial intelligence dedicated to generating unlimited amounts of unique inspirational quotes + for endless enrichment of pointless human existence. + https://inspirobot.me/ + """ + + async def command_help(self, bot, room): + msg = """usage: !inspire [command] + No command to generate an inspirational poster just for you! + - help - show this. Useful, isn't it? + """ + await bot.send_html(room, f"{html.escape(self.help())}", self.help()) + await bot.send_text(room, msg)