ananas The Python Bot Framework for Mastodon开源项目

我要开发同款
匿名用户2021年11月08日
108阅读

技术信息

开源地址
https://github.com/esl/elarm
授权协议
MIT License

作品详情

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=....Gettigstartedpipistallaaas

Theaaaspippackagecomeswithascripttohelpyoumaageyourbots.

Simplygiveitacofigfileadit'llloadyourbotsadclosethemsafelywheitreceivesakeyboarditerrupt,SIGINT,SIGTERM,orSIGKILL.

aaascofig.cfg

Ifyouhave'tspecifiedaclietid/secretoraccesstoke,thescriptwillexitulessyouruitwiththe--iteractiveflag,whichallowsittopromptyoufortheistacelogiiformatio.(Theolypartoftheiputyoueterherethat'sstoredithecofigfileistheistaceame--theemailadpasswordareolyusedtogeeratetheaccesstoke).

Cofiguratio

ThefollowigfieldsareiterpretedbythePieappleBotbaseclasssadwillworkforeverybot:

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.

WritigBots

Custombotclassesshouldbesubclassesofaaas.PieappleBot.Ifyouoverride__iit__,besuretocallthebaseclass's__iit__.

Decorators

Iorderforthebottodoaythig,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.

OverrideableFuctios

Youcaalsodefiethefollowigfuctiosadtheywillbecalledattherelevatpoitsithebot'slifecycle:

iit(self):calledbeforethecofiguratiofilehasbeeloaded,sothatyoucasetdefaultvaluesforcofigfieldsicasethecofigfiledoes'tspecifythem.

start(self):calledafteralloftheiteralPieappleBotiitializatioiscompleteadthemastodoAPIisreadytouse.Agoodplacetoloadfilesspecifiedithecofig,postastartupotice,orotherwisedobot-specificsetup.

stop(self):calledwhethebothasreceivedashutdowsigaladeedstostop.Thecofigfilewillbesavedafterthis,soifyoueedtomakeaylastmiutechagestothecofig,dothathere.

CofiguratioFields

Allofthecofiguratiofieldsforthecurretbotareavailablethroughtheself.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.

DistributigBots

Youcadistributebotshoweveryouwat;aslogastheclassisavailableisomemoduleipytho'ssys.pathoramoduleaccessiblefromthecurretdirectory,theruerscriptwillbeabletoloadit.

Ifyouthikyourbotmightbegeerallyusefultootherpeople,feelfreetocreateapullrequestothisrepositorytogetitaddedtothecollectioofdefaultbots.

Questios?PigmeoMastodoat@chr@cybre.spaceorshootmeaemailatchr@cybre.spaceadI'llaswerasbestIca!

功能介绍

Ananas What is Ananas? Ananas allows you to write simple (or complicated!) mastodon bots without ...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论