AaasWhatisAaas?
Aaasallowsyoutowritesimple(orcomplicated!)mastodobotswithouthavigtorewritecofigfileloadig,iterval-basedpostig,scheduledpostig,auto-replyig,adsoo.
Somebotsareassimpleasacofiguratiofile:
[bepis]class=tracery.TraceryBotaccess_toke=....grammar_file="bepis.jso"Butit'seasytowriteoewithcustomizedbehavior:
classMyBot(aaas.PieappleBot):defstart(self):withope('trivia.txt','r')astrivia_file:self.trivia=trivia_file.lies()@hourly(miute=17)defpost_trivia(self):self.mastodo.toot(radom.choice(self.trivia))@replydefrespod_trivia(self,status,user):self.mastodo.toot("@{}:{}".format(user["acct"],radom.choice(self.trivia)))Rumultiplebotsomultipleistacesoutofasiglecofigfile:
[jorts]class=custom.JortsBotdomai=botsi.spaceaccess_toke=....lie=632[roll]class=roll.DiceBotdomai=cybre.spaceaccess_toke=....AdusetheDEFAULTsectiotosharecommocofiguratiooptiosbetweethem:
[DEFAULT]domai=cybre.spacecliet_id=....cliet_secret=....GettigstartedpipistallaaasTheaaaspippackagecomeswithascripttohelpyoumaageyourbots.
Simplygiveitacofigfileadit'llloadyourbotsadclosethemsafelywheitreceivesakeyboarditerrupt,SIGINT,SIGTERM,orSIGKILL.
aaascofig.cfgIfyouhave'tspecifiedaclietid/secretoraccesstoke,thescriptwillexitulessyouruitwiththe--iteractiveflag,whichallowsittopromptyoufortheistacelogiiformatio.(Theolypartoftheiputyoueterherethat'sstoredithecofigfileistheistaceame--theemailadpasswordareolyusedtogeeratetheaccesstoke).
CofiguratioThefollowigfieldsareiterpretedbythePieappleBotbaseclasssadwillworkforeverybot:
class:thefully-specifiedpythoclassthattheruerscriptshouldistatiatetostartyourbot.e.g."aaas.default.TraceryBot"
domai¹:thedomaioftheistacetorutheboto.Mustsupporthttpscoectios.Olyicludethedomai,oprotocolorslashes.e.g."mastodo.social"
cliet_id¹,cliet_secret¹:thetokesthattheistaceusestoidetifywhatclietthisbotispostigfrom/as.Willbeusedtodetermiewhat'sdisplayedudereathallthepostsmadebythisbot.
access_toke¹:theaccesstokeusedtoautheticateAPIrequestswiththeistace.Makesurethisissecret,do'tdistributecofigfileswiththisfieldfilledoutorpeoplewillbeabletopostudertheaccoutthistokewascreatedwith.
admi:thefulluserame(withoutleadig@)oftheusertoDMerrorreportsto.Cabeleftuspecified,butisusefulforkeepigaeyeothehealthofthebotwithoutcostatlymoitorigthescriptlogs.e.g.admi@example.tow
¹:Filledoutautomaticallyifthebotisruiiteractivemode.
Additioalfieldsarespecifictothetypeofbot,refertothedocumetatioforthebot'sclassformoreiformatioaboutthefieldsitexpects.
WritigBotsCustombotclassesshouldbesubclassesofaaas.PieappleBot.Ifyouoverride__iit__,besuretocallthebaseclass's__iit__.
DecoratorsIorderforthebottodoaythig,youshouldaddamethoddecoratedwithatleastoeofthefollowigdecorators:
@aaas.reply:Callsthedecoratedfuctiowhethebotismetioedbyayotheruser.Decoratortakesoparameters,butshouldolybecalledofuctiosmatchigthissigature:defreply_f(self,metio,user).metiowillbethedictioarycorrespodigtothestatuscotaiigthemetio(asreturedbythemastodoAPI,userwillbethedictioarycorrespodigtotheuserthatmetioedthebot.
@aaas.iterval(secs):Callsthedecoratedfuctioeverysecssecods,startigwhethebotisiitialized.Foritervalslogertha~ahour,youmaywattouse@scheduleistead.e.g.@aaas.iterval(60)
@aaas.schedule(**kwargs):Allowsyoutoschedule,cro-style,thedecoratedfuctio.Acceptedkeywordsare"secod","miute","hour","day_of_week"or"day_of_moth"(butotboth),"moth",ad"year".Ifayofthesekeywordsareotspecified,theywillbetreatedlikecrotreatsa*,thatis,aslogasthetimematchestheothervalues,ayvaluewillbeaccepted.Speakigofwhich,thecro-likesytax"*"aswellas"*/3"arebothaccepted,adwillexpadtotheexpectedthig:forexample,schedule(hour="*/2",miute="*/10")willpostevery10miutesdurighourswhicharemultiplesof2.
@aaas.hourly(miute=0),@aaas.daily(hour=0,miute=0):Shortcutsfor@aaas.schedule()thatcallthedecoratedfuctiooceahouratthespecifiedmiuteoroceadayatthespecifiedhouradmiute.Ifparametersareomittedthey'llpostatthetopofthehourormidight(UTC).
@aaas.error_reporter:specifiescustombehaviorforreportigerrors.Thedecoratedfuctioshouldmatchthissigature:deferr(self,error)whereerrorisastrigrepresetatiooftheerror.
OverrideableFuctiosYoucaalsodefiethefollowigfuctiosadtheywillbecalledattherelevatpoitsithebot'slifecycle:
iit(self):calledbeforethecofiguratiofilehasbeeloaded,sothatyoucasetdefaultvaluesforcofigfieldsicasethecofigfiledoes'tspecifythem.
start(self):calledafteralloftheiteralPieappleBotiitializatioiscompleteadthemastodoAPIisreadytouse.Agoodplacetoloadfilesspecifiedithecofig,postastartupotice,orotherwisedobot-specificsetup.
stop(self):calledwhethebothasreceivedashutdowsigaladeedstostop.Thecofigfilewillbesavedafterthis,soifyoueedtomakeaylastmiutechagestothecofig,dothathere.
CofiguratioFieldsAllofthecofiguratiofieldsforthecurretbotareavailablethroughtheself.cofigobject,whichexposesthemwithbothfield-accessorsytaxaddictioary-accessorsytax,forexample:
foo=self.cofig.foobar=self.cofig["bar"]Thesecaberead(togettheuser'scofiguratiodata)orwritteto(toaffectthecofigfileoextsave)ordeleted(toremovethatfieldfromthecofigfile).
Youcacallself.cofig.load()togetthelatestvaluesfromthecofigfile.loadtakesaoptioalparameterame,whichistheameofthesectiotoloadithecofigfileicaseyouwattoloadadifferetoethathebotwasstartedwith.
Youcaalsocallself.cofig.save()towriteaychagesmadesicethelastloadbacktothecofigfile.
Notethatifyoucallself.cofig.load()durigbotoperatio,withoutfirstcalligself.cofig.save(),youwilldiscardaychagesmadetothecofiguratiosicethelastload.
DistributigBotsYoucadistributebotshoweveryouwat;aslogastheclassisavailableisomemoduleipytho'ssys.pathoramoduleaccessiblefromthecurretdirectory,theruerscriptwillbeabletoloadit.
Ifyouthikyourbotmightbegeerallyusefultootherpeople,feelfreetocreateapullrequestothisrepositorytogetitaddedtothecollectioofdefaultbots.
Questios?PigmeoMastodoat@chr@cybre.spaceorshootmeaemailatchr@cybre.spaceadI'llaswerasbestIca!







评论