Package: matrixbz
matrixbzmatrixbz is a library for quickly building Matrix bots. It usesmatrix-nioas its Matrix client library.ExamplesXKCD Botimportosimportpathlibimportrequestsfromlxmlimporthtmlimportmatrixbzimportmatrixbz.responseasresponseimportmatrixbz.cacheascacheXKCD_PATH=pathlib.Path(__file__).parent.absolute()CACHE_PATH=os.path.join(XKCD_PATH,'responsecache')@matrixbz.matrixbz_controller(bot_name='xkcdbot')classXKCDBotController():AUTH=matrixbz.auth.PublicBotCACHE=cache.FileTextCache(CACHE_PATH)@cache.cache_result@matrixbz.matrixbz_methodasyncdefnum(self,num,**kwargs):page_url=f'https://xkcd.com/{num}/'img_url=self._get_img_url(page_url)returnresponse.Image(img_url)def_get_img_url(self,url):res=requests.get(url)tree=html.fromstring(res.text)img_url=tree.xpath('//div[@id="comic"]/img')[0].get('src')returnf'https:{img_url}'creds={'homeserver':'https://matrix.MYSERVER.com','user':'@bot:MYSERVER.com','password':'bot_password'}bot=XKCDBotController.create_matrix_bot(creds)bot.run()Accepts all invites and responds to all users. Fetches XKCD image with:!xkcdbot num <xkcd_comic_number>Images are cached in local folder.Dice Botimportrandomimportreimportmatrixbzimportmatrixbz.responseasresponseimportmatrixbz.cacheascache@matrixbz.matrixbz_controller(bot_name='dicebot')classDiceBotController():AUTH=matrixbz.auth.PublicBot@matrixbz.matrixbz_methodasyncdefroll(self,dstring,**kwargs):regex=re.compile('^([0-9]+)d([0-9]+)$')match=re.search(regex,dstring)num_dice=int(match.group(1))sides=int(match.group(2))res=[]fordinrange(num_dice):res.append(random.randint(1,sides))returnresponse.Notice(f'rolling{dstring}:{res}')creds={'homeserver':'https://matrix.MYSERVER.com','user':'@bot:MYSERVER.com','password':'bot_password'}bot=DiceBotController.create_matrix_bot(creds)bot.run()Accepts all invites and responds to all users. Produces dice rolls in<N>d<S>format with:!dicebot roll <number_of_dice>d<number_of_sides>Decoratorsmatrixbz_controllerRequires specifying abot_name- bots are invoked by writing!<bot_name> <method>in chat. Marks a python class as a bot controller. Addscreate_matrix_botclass method that takes incredsand creates a bot. The bot'srunstarts the bot and blocks while the bot is running.credscredsis a dictionary withhomeserver,user, andpasswordcredentials for a bot.matrixbz_methodMarks a function as a bot method. Can be invoked with!<bot_name> <function_name>cache_resultAmatrixbz_methodmarked with this will have its result cached. If the exact invocation of the command has been called before, bot will return the result without calling the function again.matrixbz_msg_handlerA function marked with this will be invoked upon processing all non-command messagesmatrixbz_startup_methodA function marked with this will execute one time when the bot'srunis invoked.AuthA matrix bot controller can specify it's invite acceptance and command invocation authorization by settingAUTH.BlockAllThis is the default ifAUTHis not specified. Will not accept any invites and will not respond to any commands.PublicBotThe bot will accept all invites and respond to all commands.UserWhitelistThe bot will only accept invites from and respond to commands from users specified inUSER_WHITELISTlist on the controller class.ResponseTextTODONoticeTODOHtmlTODOImageTODOCacheNoCacheDefault. No cacheing.InMemoryTextCacheStores cached command results in memory.FileTextCacheStores cached command results on disk.