From 6f2cb5d19c3f6ff4f109de95795aadc15117d70d Mon Sep 17 00:00:00 2001 From: davidovski Date: Sun, 9 Jan 2022 19:40:48 +0000 Subject: [PATCH] initial commit --- README.md | 5 +++++ config.example.py | 3 +++ garfeef.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 README.md create mode 100644 config.example.py create mode 100755 garfeef.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..9e3c29e --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# garfeef +a simple matrix bot that randomly posts randomly generated garfield comics + +requires some matrix python libraries + diff --git a/config.example.py b/config.example.py new file mode 100644 index 0000000..56b9d32 --- /dev/null +++ b/config.example.py @@ -0,0 +1,3 @@ +USERNAME="garfield" +PASSWORD="secure password" +SERVER="http://matrix.org" diff --git a/garfeef.py b/garfeef.py new file mode 100755 index 0000000..1dfdb2f --- /dev/null +++ b/garfeef.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +""" +A test bot using the Python Matrix Bot API +Test it out by adding it to a group chat and doing one of the following: +1. Say "Hi" +2. Say !echo this is a test! +3. Say !d6 to get a random size-sided die roll result +""" + +import random +import urllib.request +import time + +from matrix_bot_api.matrix_bot_api import MatrixBotAPI +from matrix_bot_api.mregex_handler import MRegexHandler +from matrix_bot_api.mcommand_handler import MCommandHandler + +# Global variables +from config import * + +def get_garfeed(): + contents = str(urllib.request.urlopen("https://www.bgreco.net/garfield/").read()) + # funny way of parsing this html + url = "https://www.bgreco.net/garfield/save.png" + contents.split("save.png")[1].split(">")[0] + with open("save.png", "wb") as file: + file.write(urllib.request.urlopen(url).read()) + return url + + +def sendmsg(bot): + for room in bot.rooms: + print(type(room)) + get_garfeed() + + with open("save.png", "rb") as file: + url = bot.client.upload(file, "image/png") + room.send_image(url, "garfeeld.png") + +def main(): + bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER) + bot.start_polling() + bot.client.api.upload_keys() + + print(f"up and running as {USERNAME}") + while True: + sendmsg(bot) + timenext = random.randint(60, 60*60) + print(f"next in {timenext} seconds") + time.sleep(timenext) + + +if __name__ == "__main__": + main()