1
0

ShellBot.sh 171 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091
  1. #!/usr/bin/env bash
  2. #-----------------------------------------------------------------------------------------------------------
  3. # DATA: 07 de Março de 2017
  4. # SCRIPT: ShellBot.sh
  5. # VERSÃO: 6.4.0
  6. # DESENVOLVIDO POR: Juliano Santos [SHAMAN]
  7. # PÁGINA: http://www.shellscriptx.blogspot.com.br
  8. # FANPAGE: https://www.facebook.com/shellscriptx
  9. # GITHUB: https://github.com/shellscriptx
  10. # CONTATO: shellscriptx@gmail.com
  11. #
  12. # DESCRIÇÃO: ShellBot é uma API não-oficial desenvolvida para facilitar a criação de
  13. # bots na plataforma TELEGRAM. Constituída por uma coleção de métodos
  14. # e funções que permitem ao desenvolvedor:
  15. #
  16. # * Gerenciar grupos, canais e membros.
  17. # * Enviar mensagens, documentos, músicas, contatos e etc.
  18. # * Enviar teclados (KeyboardMarkup e InlineKeyboard).
  19. # * Obter informações sobre membros, arquivos, grupos e canais.
  20. # * Para mais informações consulte a documentação:
  21. #
  22. # https://github.com/shellscriptx/ShellBot/wiki
  23. #
  24. # O ShellBot mantém o padrão da nomenclatura dos métodos registrados da
  25. # API original (Telegram), assim como seus campos e valores. Os métodos
  26. # requerem parâmetros e argumentos para a chamada e execução. Parâmetros
  27. # obrigatórios retornam uma mensagem de erro caso o argumento seja omitido.
  28. #
  29. # NOTAS: Desenvolvida na linguagem Shell Script, utilizando o interpretador de
  30. # comandos BASH e explorando ao máximo os recursos built-in do mesmo,
  31. # reduzindo o nível de dependências de pacotes externos.
  32. #-----------------------------------------------------------------------------------------------------------
  33. [[ $_SHELLBOT_SH_ ]] && return 1
  34. if ! awk 'BEGIN { exit ARGV[1] < 4.3 }' ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}; then
  35. echo "${BASH_SOURCE:-${0##*/}}: erro: requer o interpretador de comandos 'bash 4.3' ou superior." 1>&2
  36. exit 1
  37. fi
  38. # Informações
  39. readonly -A _SHELLBOT_=(
  40. [name]='ShellBot'
  41. [keywords]='Shell Script Telegram API'
  42. [description]='API não-oficial para criação de bots na plataforma Telegram.'
  43. [version]='6.4.0'
  44. [language]='shellscript'
  45. [shell]=${SHELL}
  46. [shell_version]=${BASH_VERSION}
  47. [author]='Juliano Santos [SHAMAN]'
  48. [email]='shellscriptx@gmail.com'
  49. [wiki]='https://github.com/shellscriptx/shellbot/wiki'
  50. [github]='https://github.com/shellscriptx/shellbot'
  51. [packages]='curl 7.0, getopt 2.0, jq 1.5'
  52. )
  53. # Verifica dependências.
  54. while read _pkg_ _ver_; do
  55. if command -v $_pkg_ &>/dev/null; then
  56. if [[ $($_pkg_ --version 2>&1) =~ [0-9]+\.[0-9]+ ]]; then
  57. if ! awk 'BEGIN { exit ARGV[1] < ARGV[2] }' $BASH_REMATCH $_ver_; then
  58. printf "%s: erro: requer o pacote '%s %s' ou superior.\n" ${_SHELLBOT_[name]} $_pkg_ $_ver_ 1>&2
  59. exit 1
  60. fi
  61. else
  62. printf "%s: erro: '%s' não foi possível obter a versão.\n" ${_SHELLBOT_[name]} $_pkg_ 1>&2
  63. exit 1
  64. fi
  65. else
  66. printf "%s: erro: '%s' o pacote requerido está ausente.\n" ${_SHELLBOT_[name]} $_pkg_ 1>&2
  67. exit 1
  68. fi
  69. done <<< "${_SHELLBOT_[packages]//,/$'\n'}"
  70. # bash (opções).
  71. shopt -s checkwinsize \
  72. cmdhist \
  73. complete_fullquote \
  74. expand_aliases \
  75. extglob \
  76. extquote \
  77. force_fignore \
  78. histappend \
  79. interactive_comments \
  80. progcomp \
  81. promptvars \
  82. sourcepath
  83. # Desabilita a expansão de nomes de arquivos (globbing).
  84. set -f
  85. readonly _SHELLBOT_SH_=1 # Inicialização
  86. readonly _BOT_SCRIPT_=${0##*/} # Script
  87. readonly _CURL_OPT_='--silent --request' # CURL (opções)
  88. # Erros
  89. readonly _ERR_TYPE_BOOL_='tipo incompatível: suporta somente "true" ou "false".'
  90. readonly _ERR_TYPE_INT_='tipo incompatível: suporta somente inteiro.'
  91. readonly _ERR_TYPE_FLOAT_='tipo incompatível: suporta somente float.'
  92. readonly _ERR_PARAM_REQUIRED_='opção requerida: verique se o(s) parâmetro(s) ou argumento(s) obrigatório(s) estão presente(s).'
  93. readonly _ERR_TOKEN_UNAUTHORIZED_='não autorizado: verifique se possui permissões para utilizar o token.'
  94. readonly _ERR_TOKEN_INVALID_='token inválido: verique o número do token e tente novamente.'
  95. readonly _ERR_BOT_ALREADY_INIT_='ação não permitida: o bot já foi inicializado.'
  96. readonly _ERR_FILE_NOT_FOUND_='falha ao acessar: não foi possível ler o arquivo.'
  97. readonly _ERR_DIR_WRITE_DENIED_='permissão negada: não é possível gravar no diretório.'
  98. readonly _ERR_DIR_NOT_FOUND_='Não foi possível acessar: diretório não encontrado.'
  99. readonly _ERR_FILE_INVALID_ID_='id inválido: arquivo não encontrado.'
  100. readonly _ERR_UNKNOWN_='erro desconhecido: ocorreu uma falha inesperada. Reporte o problema ao desenvolvedor.'
  101. readonly _ERR_SERVICE_NOT_ROOT_='acesso negado: requer privilégios de root.'
  102. readonly _ERR_SERVICE_EXISTS_='erro ao criar o serviço: o nome do serviço já existe.'
  103. readonly _ERR_SERVICE_SYSTEMD_NOT_FOUND_='erro ao ativar: o sistema não possui suporte ao gerenciamento de serviços "systemd".'
  104. readonly _ERR_SERVICE_USER_NOT_FOUND_='usuário não encontrado: a conta de usuário informada é inválida.'
  105. readonly _ERR_VAR_NAME_='variável não encontrada: o identificador é inválido ou não existe.'
  106. readonly _ERR_FUNCTION_NOT_FOUND_='função não encontrada: o identificador especificado é inválido ou não existe.'
  107. readonly _ERR_ARG_='argumento inválido: o argumento não é suportado pelo parâmetro especificado.'
  108. readonly _ERR_RULE_ALREADY_EXISTS_='falha ao definir: o nome da regra já existe.'
  109. readonly _ERR_HANDLE_EXISTS_='erro ao registar: já existe um handle vinculado ao callback'
  110. readonly _ERR_CONNECTION_='falha de conexão: não foi possível estabelecer conexão com o Telegram.'
  111. # Maps
  112. declare -A _BOT_HANDLE_
  113. declare -A _BOT_RULES_
  114. declare -A return
  115. declare -i _BOT_RULES_INDEX_
  116. declare _VAR_INIT_
  117. Json() { local obj=$(jq -Mc "$1" <<< "${*:2}"); obj=${obj#\"}; echo "${obj%\"}"; }
  118. SetDelmValues(){
  119. local obj=$(jq "[..|select(type == \"string\" or type == \"number\" or type == \"boolean\")|tostring]|join(\"${_BOT_DELM_/\"/\\\"}\")" <<< "$1")
  120. obj=${obj#\"}; echo "${obj%\"}"
  121. }
  122. GetAllValues(){
  123. jq '[..|select(type == "string" or type == "number" or type == "boolean")|tostring]|.[]' <<< "$1"
  124. }
  125. GetAllKeys(){
  126. jq -r 'path(..|select(type == "string" or type == "number" or type == "boolean"))|map(if type == "number" then .|tostring|"["+.+"]" else . end)|join(".")|gsub("\\.\\[";"[")' <<< "$1"
  127. }
  128. FlagConv()
  129. {
  130. local var str=$2
  131. while [[ $str =~ \$\{([a-z_]+)\} ]]; do
  132. if [[ ${BASH_REMATCH[1]} == @(${_VAR_INIT_// /|}) ]]; then
  133. var=${BASH_REMATCH[1]}[$1]
  134. str=${str//${BASH_REMATCH[0]}/${!var}}
  135. else
  136. str=${str//${BASH_REMATCH[0]}}
  137. fi
  138. done
  139. echo "$str"
  140. }
  141. CreateLog()
  142. {
  143. local fid fbot fname fuser lcode cid ctype
  144. local ctitle mid mdate mtext etype
  145. local i fmt obj oid
  146. for ((i=0; i < $1; i++)); do
  147. printf -v fmt "$_BOT_LOG_FORMAT_" || MessageError API
  148. # Suprimir erros.
  149. exec 5<&2
  150. exec 2<&-
  151. # Objeto (tipo)
  152. if [[ ${message_contact_phone_number[$i]:-${edited_message_contact_phone_number[$i]}} ]] ||
  153. [[ ${channel_post_contact_phone_number[$i]:-${edited_channel_post_contact_phone_number[$i]}} ]]; then obj=contact
  154. elif [[ ${message_sticker_file_id[$i]:-${edited_message_sticker_file_id[$i]}} ]] ||
  155. [[ ${channel_post_sticker_file_id[$i]:-${edited_channel_post_sticker_file_id[$i]}} ]]; then obj=sticker
  156. elif [[ ${message_animation_file_id[$i]:-${edited_message_animation_file_id[$i]}} ]] ||
  157. [[ ${channel_post_animation_file_id[$i]:-${edited_channel_post_animation_file_id[$i]}} ]]; then obj=animation
  158. elif [[ ${message_photo_file_id[$i]:-${edited_message_photo_file_id[$i]}} ]] ||
  159. [[ ${channel_post_photo_file_id[$i]:-${edited_channel_post_photo_file_id[$i]}} ]]; then obj=photo
  160. elif [[ ${message_audio_file_id[$i]:-${edited_message_audio_file_id[$i]}} ]] ||
  161. [[ ${channel_post_audio_file_id[$i]:-${edited_channel_post_audio_file_id[$i]}} ]]; then obj=audio
  162. elif [[ ${message_video_file_id[$i]:-${edited_message_video_file_id[$i]}} ]] ||
  163. [[ ${channel_post_video_file_id[$i]:-${edited_channel_post_video_file_id[$i]}} ]]; then obj=video
  164. elif [[ ${message_voice_file_id[$i]:-${edited_message_voice_file_id[$i]}} ]] ||
  165. [[ ${channel_post_voice_file_id[$i]:-${edited_channel_post_voice_file_id[$i]}} ]]; then obj=voice
  166. elif [[ ${message_document_file_id[$i]:-${edited_message_document_file_id[$i]}} ]] ||
  167. [[ ${channel_post_document_file_id[$i]:-${edited_channel_post_document_file_id[$i]}} ]]; then obj=document
  168. elif [[ ${message_venue_location_latitude[$i]:-${edited_message_venue_location_latitude[$i]}} ]] ||
  169. [[ ${channel_post_venue_location_latitude[$i]-${edited_channel_post_venue_location_latitude[$i]}} ]]; then obj=venue
  170. elif [[ ${message_location_latitude[$i]:-${edited_message_location_latitude[$i]}} ]] ||
  171. [[ ${channel_post_location_latitude[$i]:-${edited_channel_post_location_latitude[$i]}} ]]; then obj=location
  172. elif [[ ${message_text[$i]:-${edited_message_text[$i]}} ]] ||
  173. [[ ${channel_post_text[$i]:-${edited_channel_post_text[$i]}} ]]; then obj=text
  174. elif [[ ${callback_query_id[$i]} ]]; then obj=callback
  175. elif [[ ${inline_query_id[$i]} ]]; then obj=inline
  176. elif [[ ${chosen_inline_result_result_id[$i]} ]]; then obj=chosen
  177. fi
  178. # Objeto (id)
  179. [[ ${oid:=${message_contact_phone_number[$i]}} ]] ||
  180. [[ ${oid:=${message_sticker_file_id[$i]}} ]] ||
  181. [[ ${oid:=${message_animation_file_id[$i]}} ]] ||
  182. [[ ${oid:=${message_photo_file_id[$i]}} ]] ||
  183. [[ ${oid:=${message_audio_file_id[$i]}} ]] ||
  184. [[ ${oid:=${message_video_file_id[$i]}} ]] ||
  185. [[ ${oid:=${message_voice_file_id[$i]}} ]] ||
  186. [[ ${oid:=${message_document_file_id[$i]}} ]] ||
  187. [[ ${oid:=${edited_message_contact_phone_number[$i]}} ]] ||
  188. [[ ${oid:=${edited_message_sticker_file_id[$i]}} ]] ||
  189. [[ ${oid:=${edited_message_animation_file_id[$i]}} ]] ||
  190. [[ ${oid:=${edited_message_photo_file_id[$i]}} ]] ||
  191. [[ ${oid:=${edited_message_audio_file_id[$i]}} ]] ||
  192. [[ ${oid:=${edited_message_video_file_id[$i]}} ]] ||
  193. [[ ${oid:=${edited_message_voice_file_id[$i]}} ]] ||
  194. [[ ${oid:=${edited_message_document_file_id[$i]}} ]] ||
  195. [[ ${oid:=${channel_post_contact_phone_number[$i]}} ]] ||
  196. [[ ${oid:=${channel_post_sticker_file_id[$i]}} ]] ||
  197. [[ ${oid:=${channel_post_animation_file_id[$i]}} ]] ||
  198. [[ ${oid:=${channel_post_photo_file_id[$i]}} ]] ||
  199. [[ ${oid:=${channel_post_audio_file_id[$i]}} ]] ||
  200. [[ ${oid:=${channel_post_video_file_id[$i]}} ]] ||
  201. [[ ${oid:=${channel_post_voice_file_id[$i]}} ]] ||
  202. [[ ${oid:=${channel_post_document_file_id[$i]}} ]] ||
  203. [[ ${oid:=${edited_channel_post_contact_phone_number[$i]}} ]] ||
  204. [[ ${oid:=${edited_channel_post_sticker_file_id[$i]}} ]] ||
  205. [[ ${oid:=${edited_channel_post_animation_file_id[$i]}} ]] ||
  206. [[ ${oid:=${edited_channel_post_photo_file_id[$i]}} ]] ||
  207. [[ ${oid:=${edited_channel_post_audio_file_id[$i]}} ]] ||
  208. [[ ${oid:=${edited_channel_post_video_file_id[$i]}} ]] ||
  209. [[ ${oid:=${edited_channel_post_voice_file_id[$i]}} ]] ||
  210. [[ ${oid:=${edited_channel_post_document_file_id[$i]}} ]] ||
  211. [[ ${oid:=${message_message_id[$i]}} ]] ||
  212. [[ ${oid:=${edited_message_message_id[$i]}} ]] ||
  213. [[ ${oid:=${channel_post_message_id[$i]}} ]] ||
  214. [[ ${oid:=${edited_channel_post_message_id[$i]}} ]] ||
  215. [[ ${oid:=${callback_query_id[$i]}} ]] ||
  216. [[ ${oid:=${inline_query_id[$i]}} ]] ||
  217. [[ ${oid:=${chosen_inline_result_result_id[$i]}} ]]
  218. # Remetente (id)
  219. [[ ${fid:=${message_from_id[$i]}} ]] ||
  220. [[ ${fid:=${edited_message_from_id[$i]}} ]] ||
  221. [[ ${fid:=${callback_query_from_id[$i]}} ]] ||
  222. [[ ${fid:=${inline_query_from_id[$i]}} ]] ||
  223. [[ ${fid:=${chosen_inline_result_from_id[$i]}} ]]
  224. # Bot
  225. [[ ${fbot:=${message_from_is_bot[$i]}} ]] ||
  226. [[ ${fbot:=${edited_message_from_is_bot[$i]}} ]] ||
  227. [[ ${fbot:=${callback_query_from_is_bot[$i]}} ]] ||
  228. [[ ${fbot:=${inline_query_from_is_bot[$i]}} ]] ||
  229. [[ ${fbot:=${chosen_inline_result_from_is_bot[$i]}} ]]
  230. # Usuário (nome)
  231. [[ ${fname:=${message_from_first_name[$i]}} ]] ||
  232. [[ ${fname:=${edited_message_from_first_name[$i]}} ]] ||
  233. [[ ${fname:=${callback_query_from_first_name[$i]}} ]] ||
  234. [[ ${fname:=${inline_query_from_first_name[$i]}} ]] ||
  235. [[ ${fname:=${chosen_inline_result_from_first_name[$i]}} ]] ||
  236. [[ ${fname:=${channel_post_author_signature[$i]}} ]] ||
  237. [[ ${fname:=${edited_channel_post_author_signature[$i]}} ]]
  238. # Usuário (conta)
  239. [[ ${fuser:=${message_from_username[$i]}} ]] ||
  240. [[ ${fuser:=${edited_message_from_username[$i]}} ]] ||
  241. [[ ${fuser:=${callback_query_from_username[$i]}} ]] ||
  242. [[ ${fuser:=${inline_query_from_username[$i]}} ]] ||
  243. [[ ${fuser:=${chosen_inline_result_from_username[$i]}} ]]
  244. # Idioma
  245. [[ ${lcode:=${message_from_language_code[$i]}} ]] ||
  246. [[ ${lcode:=${edited_message_from_language_code[$i]}} ]] ||
  247. [[ ${lcode:=${callback_query_from_language_code[$i]}} ]] ||
  248. [[ ${lcode:=${inline_query_from_language_code[$i]}} ]] ||
  249. [[ ${lcode:=${chosen_inline_result_from_language_code[$i]}} ]]
  250. # Bate-papo (id)
  251. [[ ${cid:=${message_chat_id[$i]}} ]] ||
  252. [[ ${cid:=${edited_message_chat_id[$i]}} ]] ||
  253. [[ ${cid:=${callback_query_message_chat_id[$i]}} ]] ||
  254. [[ ${cid:=${channel_post_chat_id[$i]}} ]] ||
  255. [[ ${cid:=${edited_channel_post_chat_id[$i]}} ]]
  256. # Bate-papo (tipo)
  257. [[ ${ctype:=${message_chat_type[$i]}} ]] ||
  258. [[ ${ctype:=${edited_message_chat_type[$i]}} ]] ||
  259. [[ ${ctype:=${callback_query_message_chat_type[$i]}} ]] ||
  260. [[ ${ctype:=${channel_post_chat_type[$i]}} ]] ||
  261. [[ ${ctype:=${edited_channel_post_chat_type[$i]}} ]]
  262. # Bate-papo (título)
  263. [[ ${ctitle:=${message_chat_title[$i]}} ]] ||
  264. [[ ${ctitle:=${edited_message_chat_title[$i]}} ]] ||
  265. [[ ${ctitle:=${callback_query_message_chat_title[$i]}} ]] ||
  266. [[ ${ctitle:=${channel_post_chat_title[$i]}} ]] ||
  267. [[ ${ctitle:=${edited_channel_post_chat_title[$i]}} ]]
  268. # Mensagem (id)
  269. [[ ${mid:=${message_message_id[$i]}} ]] ||
  270. [[ ${mid:=${edited_message_message_id[$i]}} ]] ||
  271. [[ ${mid:=${callback_query_id[$i]}} ]] ||
  272. [[ ${mid:=${inline_query_id[$i]}} ]] ||
  273. [[ ${mid:=${chosen_inline_result_result_id[$i]}} ]] ||
  274. [[ ${mid:=${channel_post_message_id[$i]}} ]] ||
  275. [[ ${mid:=${edited_channel_post_message_id[$i]}} ]]
  276. # Mensagem (data)
  277. [[ ${mdate:=${message_date[$i]}} ]] ||
  278. [[ ${mdate:=${edited_message_date[$i]}} ]] ||
  279. [[ ${mdate:=${callback_query_message_date[$i]}} ]] ||
  280. [[ ${mdate:=${channel_post_date[$i]}} ]] ||
  281. [[ ${mdate:=${edited_channel_post_date[$i]}} ]]
  282. # Mensagem (texto)
  283. [[ ${mtext:=${message_text[$i]}} ]] ||
  284. [[ ${mtext:=${edited_message_text[$i]}} ]] ||
  285. [[ ${mtext:=${callback_query_message_text[$i]}} ]] ||
  286. [[ ${mtext:=${inline_query_query[$i]}} ]] ||
  287. [[ ${mtext:=${chosen_inline_result_query[$i]}} ]] ||
  288. [[ ${mtext:=${channel_post_text[$i]}} ]] ||
  289. [[ ${mtext:=${edited_channel_post_text[$i]}} ]]
  290. # Mensagem (tipo)
  291. [[ ${etype:=${message_entities_type[$i]}} ]] ||
  292. [[ ${etype:=${edited_message_entities_type[$i]}} ]] ||
  293. [[ ${etype:=${callback_query_message_entities_type[$i]}} ]] ||
  294. [[ ${etype:=${channel_post_entities_type[$i]}} ]] ||
  295. [[ ${etype:=${edited_channel_post_entities_type[$i]}} ]]
  296. # Flags
  297. fmt=${fmt//\{BOT_TOKEN\}/${_BOT_INFO_[0]:--}}
  298. fmt=${fmt//\{BOT_ID\}/${_BOT_INFO_[1]:--}}
  299. fmt=${fmt//\{BOT_FIRST_NAME\}/${_BOT_INFO_[2]:--}}
  300. fmt=${fmt//\{BOT_USERNAME\}/${_BOT_INFO_[3]:--}}
  301. fmt=${fmt//\{BASENAME\}/${_BOT_SCRIPT_:--}}
  302. fmt=${fmt//\{OK\}/${return[ok]:-${ok:--}}}
  303. fmt=${fmt//\{UPDATE_ID\}/${update_id[$i]:--}}
  304. fmt=${fmt//\{OBJECT_TYPE\}/${obj:--}}
  305. fmt=${fmt//\{OBJECT_ID\}/${oid:--}}
  306. fmt=${fmt//\{FROM_ID\}/${fid:--}}
  307. fmt=${fmt//\{FROM_IS_BOT\}/${fbot:--}}
  308. fmt=${fmt//\{FROM_FIRST_NAME\}/${fname:--}}
  309. fmt=${fmt//\{FROM_USERNAME\}/${fuser:--}}
  310. fmt=${fmt//\{FROM_LANGUAGE_CODE\}/${lcode:--}}
  311. fmt=${fmt//\{CHAT_ID\}/${cid:--}}
  312. fmt=${fmt//\{CHAT_TYPE\}/${ctype:--}}
  313. fmt=${fmt//\{CHAT_TITLE\}/${ctitle:--}}
  314. fmt=${fmt//\{MESSAGE_ID\}/${mid:--}}
  315. fmt=${fmt//\{MESSAGE_DATE\}/${mdate:--}}
  316. fmt=${fmt//\{MESSAGE_TEXT\}/${mtext:--}}
  317. fmt=${fmt//\{ENTITIES_TYPE\}/${etype:--}}
  318. fmt=${fmt//\{METHOD\}/${FUNCNAME[2]/main/ShellBot.getUpdates}}
  319. fmt=${fmt//\{RETURN\}/$(SetDelmValues "$2")}
  320. exec 2<&5
  321. # log
  322. [[ $fmt ]] && { echo "$fmt" >> "$_BOT_LOG_FILE_" || MessageError API; }
  323. # Limpa objetos
  324. fid= fbot= fname= fuser= lcode= cid= ctype=
  325. ctitle= mid= mdate= mtext= etype= obj= oid=
  326. done
  327. return $?
  328. }
  329. MethodReturn()
  330. {
  331. # Retorno
  332. case $_BOT_TYPE_RETURN_ in
  333. json) echo "$1";;
  334. value) SetDelmValues "$1";;
  335. map)
  336. local key val vars vals i obj
  337. return=()
  338. mapfile -t vars <<< $(GetAllKeys "$1")
  339. mapfile -t vals <<< $(GetAllValues "$1")
  340. for i in ${!vars[@]}; do
  341. key=${vars[$i]//[0-9\[\]]/}
  342. key=${key#result.}
  343. key=${key//./_}
  344. val=${vals[$i]}
  345. val=${val#\"}
  346. val=${val%\"}
  347. [[ ${return[$key]} ]] && return[$key]+=${_BOT_DELM_}${val} || return[$key]=$val
  348. [[ $_BOT_MONITOR_ ]] && printf "[%s]: return[%s] = '%s'\n" "${FUNCNAME[1]}" "$key" "$val"
  349. done
  350. ;;
  351. esac
  352. [[ $(jq -r '.ok' <<< "$1") == true ]]
  353. return $?
  354. }
  355. MessageError()
  356. {
  357. # Variáveis locais
  358. local err_message err_param assert i
  359. # A variável 'BASH_LINENO' é dinâmica e armazena o número da linha onde foi expandida.
  360. # Quando chamada dentro de um subshell, passa ser instanciada como um array, armazenando diversos
  361. # valores onde cada índice refere-se a um shell/subshell. As mesmas caracteristicas se aplicam a variável
  362. # 'FUNCNAME', onde é armazenado o nome da função onde foi chamada.
  363. # Obtem o índice da função na hierarquia de chamada.
  364. [[ ${FUNCNAME[1]} == CheckArgType ]] && i=2 || i=1
  365. # Lê o tipo de ocorrência.
  366. # TG - Erro externo retornado pelo core do telegram.
  367. # API - Erro interno gerado pela API do ShellBot.
  368. case $1 in
  369. TG)
  370. err_param="$(Json '.error_code' "$2")"
  371. err_message="$(Json '.description' "$2")"
  372. ;;
  373. API)
  374. err_param="${3:--}: ${4:--}"
  375. err_message="$2"
  376. assert=true
  377. ;;
  378. esac
  379. # Imprime erro
  380. printf "%s: erro: linha %s: %s: %s: %s\n" \
  381. "${_BOT_SCRIPT_}" \
  382. "${BASH_LINENO[$i]:--}" \
  383. "${FUNCNAME[$i]:--}" \
  384. "${err_param:--}" \
  385. "${err_message:-$_ERR_UNKNOWN_}" 1>&2
  386. # Finaliza script/thread em caso de erro interno, caso contrário retorna 1
  387. ${assert:-false} && exit 1 || return 1
  388. }
  389. CheckArgType()
  390. {
  391. # CheckArgType recebe os dados da função chamadora e verifica
  392. # o dado recebido com o tipo suportado pelo parâmetro.
  393. # É retornado '0' para sucesso, caso contrário uma mensagem
  394. # de erro é retornada e o script/thread é finalizado com status '1'.
  395. case $1 in
  396. user) id "$3" &>/dev/null || MessageError API "$_ERR_SERVICE_USER_NOT_FOUND_" "$2" "$3";;
  397. func) [[ $(type -t "$3") == function ]] || MessageError API "$_ERR_FUNCTION_NOT_FOUND_" "$2" "$3";;
  398. var) [[ -v $3 ]] || MessageError API "$_ERR_VAR_NAME_" "$2" "$3";;
  399. int) [[ $3 =~ ^-?[0-9]+$ ]] || MessageError API "$_ERR_TYPE_INT_" "$2" "$3";;
  400. float) [[ $3 =~ ^-?[0-9]+\.[0-9]+$ ]] || MessageError API "$_ERR_TYPE_FLOAT_" "$2" "$3";;
  401. bool) [[ $3 =~ ^(true|false)$ ]] || MessageError API "$_ERR_TYPE_BOOL_" "$2" "$3";;
  402. token) [[ $3 =~ ^[0-9]+:[a-zA-Z0-9_-]+$ ]] || MessageError API "$_ERR_TOKEN_INVALID_" "$2" "$3";;
  403. file) [[ $3 =~ ^@ && ! -f ${3#@} ]] && MessageError API "$_ERR_FILE_NOT_FOUND_" "$2" "$3";;
  404. return) [[ $3 == @(json|map|value) ]] || MessageError API "$_ERR_ARG_" "$2" "$3";;
  405. cmd) [[ $3 =~ ^/[a-zA-Z0-9_]+$ ]] || MessageError API "$_ERR_ARG_" "$2" "$3";;
  406. flag) [[ $3 =~ ^[a-zA-Z0-9_]+$ ]] || MessageError API "$_ERR_ARG_" "$2" "$3";;
  407. esac
  408. return $?
  409. }
  410. FlushOffset()
  411. {
  412. local sid eid jq_obj
  413. while :; do
  414. jq_obj=$(ShellBot.getUpdates --limit 100 --offset $(ShellBot.OffsetNext))
  415. IFS=' ' read -a update_id <<< $(jq -r '.result|.[]|.update_id' <<< $jq_obj)
  416. [[ $update_id ]] || break
  417. sid=${sid:-${update_id[0]}}
  418. eid=${update_id[-1]}
  419. done
  420. echo "${sid:-0}|${eid:-0}"
  421. return $?
  422. }
  423. CreateUnitService()
  424. {
  425. local service=${1%.*}.service
  426. local ok='\033[0;32m[OK]\033[0;m'
  427. local fail='\033[0;31m[FALHA]\033[0;m'
  428. ((UID == 0)) || MessageError API "$_ERR_SERVICE_NOT_ROOT_"
  429. # O modo 'service' requer que o sistema de gerenciamento de processos 'systemd'
  430. # esteja presente para que o Unit target seja linkado ao serviço.
  431. if ! which systemctl &>/dev/null; then
  432. MessageError API "$_ERR_SERVICE_SYSTEMD_NOT_FOUND_"; fi
  433. # Se o serviço existe.
  434. test -e /lib/systemd/system/$service && \
  435. MessageError API "$_ERR_SERVICE_EXISTS_" "$service"
  436. # Gerando as configurações do target.
  437. cat > /lib/systemd/system/$service << _eof
  438. [Unit]
  439. Description=$1 - (SHELLBOT)
  440. After=network-online.target
  441. [Service]
  442. User=$2
  443. WorkingDirectory=$PWD
  444. ExecStart=/bin/bash $1
  445. ExecReload=/bin/kill -HUP \$MAINPID
  446. ExecStop=/bin/kill -KILL \$MAINPID
  447. KillMode=process
  448. Restart=on-failure
  449. RestartPreventExitStatus=255
  450. Type=simple
  451. [Install]
  452. WantedBy=multi-user.target
  453. _eof
  454. [[ $? -eq 0 ]] && {
  455. printf '%s foi criado com sucesso !!\n' $service
  456. echo -n "Habilitando..."
  457. systemctl enable $service &>/dev/null && echo -e $ok || \
  458. { echo -e $fail; MessageError API; }
  459. sed -i -r '/^\s*ShellBot.init\s/s/\s--?(s(ervice)?|u(ser)?\s+\w+)\b//g' "$1"
  460. systemctl daemon-reload
  461. echo -n "Iniciando..."
  462. systemctl start $service &>/dev/null && {
  463. echo -e $ok
  464. systemctl status $service
  465. echo -e "\nUso: sudo systemctl {start|stop|restart|reload|status} $service"
  466. } || echo -e $fail
  467. } || MessageError API
  468. exit 0
  469. }
  470. # Inicializa o bot, definindo sua API e _TOKEN_.
  471. ShellBot.init()
  472. {
  473. local method_return delm ret logfmt jq_obj offset
  474. local token monitor flush service user logfile logfmt
  475. # Verifica se o bot já foi inicializado.
  476. [[ $_SHELLBOT_INIT_ ]] && MessageError API "$_ERR_BOT_ALREADY_INIT_"
  477. local param=$(getopt --name "$FUNCNAME" \
  478. --options 't:mfsu:l:o:r:d:' \
  479. --longoptions 'token:,
  480. monitor,
  481. flush,
  482. service,
  483. user:,
  484. log_file:,
  485. log_format:,
  486. return:,
  487. delimiter:' \
  488. -- "$@")
  489. # Define os parâmetros posicionais
  490. eval set -- "$param"
  491. while :
  492. do
  493. case $1 in
  494. -t|--token)
  495. CheckArgType token "$1" "$2"
  496. token=$2
  497. shift 2
  498. ;;
  499. -m|--monitor)
  500. # Ativa modo monitor
  501. monitor=true
  502. shift
  503. ;;
  504. -f|--flush)
  505. # Define a FLAG flush para o método 'ShellBot.getUpdates'. Se ativada, faz com que
  506. # o método obtenha somente as atualizações disponíveis, ignorando a extração dos
  507. # objetos JSON e a inicialização das variáveis.
  508. flush=true
  509. shift
  510. ;;
  511. -s|--service)
  512. service=true
  513. shift
  514. ;;
  515. -u|--user)
  516. CheckArgType user "$1" "$2"
  517. user=$2
  518. shift 2
  519. ;;
  520. -l|--log_file)
  521. logfile=$2
  522. shift 2
  523. ;;
  524. -o|--log_format)
  525. logfmt=$2
  526. shift 2
  527. ;;
  528. -r|--return)
  529. CheckArgType return "$1" "$2"
  530. ret=$2
  531. shift 2
  532. ;;
  533. -d|--delimiter)
  534. delm=$2
  535. shift 2
  536. ;;
  537. --)
  538. shift
  539. break
  540. ;;
  541. esac
  542. done
  543. # Parâmetro obrigatório.
  544. [[ $token ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --token]"
  545. [[ $user && ! $service ]] && MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --service]"
  546. [[ $service ]] && CreateUnitService "$_BOT_SCRIPT_" "${user:-$USER}"
  547. declare -gr _TOKEN_=$token # TOKEN
  548. declare -gr _API_TELEGRAM_="https://api.telegram.org/bot$_TOKEN_" # API
  549. # Testa conexão.
  550. curl -s "$_API_TELEGRAM_" &>- || MessageError API "$_ERR_CONNECTION_"
  551. # Um método simples para testar o token de autenticação do seu bot.
  552. # Não requer parâmetros. Retorna informações básicas sobre o bot em forma de um objeto Usuário.
  553. ShellBot.getMe()
  554. {
  555. # Chama o método getMe passando o endereço da API, seguido do nome do método.
  556. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.})
  557. # Verifica o status de retorno do método
  558. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  559. return $?
  560. }
  561. ShellBot.getMe &>- || MessageError API "$_ERR_TOKEN_UNAUTHORIZED_" '[-t, --token]'
  562. # Salva as informações do bot.
  563. declare -gr _BOT_INFO_=(
  564. [0]=$_TOKEN_
  565. [1]=$(Json '.result.id' $jq_obj)
  566. [2]=$(Json '.result.first_name' $jq_obj)
  567. [3]=$(Json '.result.username' $jq_obj)
  568. )
  569. # Configurações.
  570. declare -gr _BOT_FLUSH_=$flush
  571. declare -gr _BOT_MONITOR_=$monitor
  572. declare -gr _BOT_SERVICE_=$service
  573. declare -gr _BOT_USER_SERVICE_=$user
  574. declare -gr _BOT_TYPE_RETURN_=${ret:-value}
  575. declare -gr _BOT_DELM_=${delm:-|}
  576. declare -gr _BOT_LOG_FILE_=${logfile}
  577. declare -gr _BOT_LOG_FORMAT_=${logfmt:-%(%d/%m/%Y %H:%M:%S)T: \{BASENAME\}: \{BOT_USERNAME\}: \{UPDATE_ID\}: \{METHOD\}: \{CHAT_TYPE\}: \{FROM_USERNAME\}: \{OBJECT_TYPE\}: \{OBJECT_ID\}: \{MESSAGE_TEXT\}}
  578. declare -gr _SHELLBOT_INIT_=1
  579. # SHELLBOT (FUNÇÕES)
  580. # Inicializa as funções para chamadas aos métodos da API do telegram.
  581. ShellBot.ListUpdates(){ echo ${!update_id[@]}; }
  582. ShellBot.TotalUpdates(){ echo ${#update_id[@]}; }
  583. ShellBot.OffsetEnd(){ local -i offset=${update_id[@]: -1}; echo $offset; }
  584. ShellBot.OffsetNext(){ echo $((${update_id[@]: -1}+1)); }
  585. ShellBot.token() { echo "${_BOT_INFO_[0]}"; }
  586. ShellBot.id() { echo "${_BOT_INFO_[1]}"; }
  587. ShellBot.first_name() { echo "${_BOT_INFO_[2]}"; }
  588. ShellBot.username() { echo "${_BOT_INFO_[3]}"; }
  589. ShellBot.getConfig()
  590. {
  591. local jq_obj
  592. printf -v jq_obj '{"monitor":%s,"flush":%s,"service":%s,"return":"%s","delimiter":"%s","user":"%s","log_file":"%s","log_format":"%s"}' \
  593. "${_BOT_MONITOR_:-false}" \
  594. "${_BOT_FLUSH_:-false}" \
  595. "${_BOT_SERVICE_:-false}" \
  596. "${_BOT_TYPE_RETURN_}" \
  597. "${_BOT_DELM_}" \
  598. "${_BOT_USER_SERVICE_}" \
  599. "${_BOT_LOG_FILE_}" \
  600. "${_BOT_LOG_FORMAT_}"
  601. MethodReturn "$jq_obj"
  602. return $?
  603. }
  604. ShellBot.regHandleFunction()
  605. {
  606. local function data handle args
  607. local param=$(getopt --name "$FUNCNAME" \
  608. --options 'f:a:d:' \
  609. --longoptions 'function:,
  610. args:,
  611. callback_data:' \
  612. -- "$@")
  613. eval set -- "$param"
  614. while :
  615. do
  616. case $1 in
  617. -f|--function)
  618. CheckArgType func "$1" "$2"
  619. function=$2
  620. shift 2
  621. ;;
  622. -a|--args)
  623. args=$2
  624. shift 2
  625. ;;
  626. -d|--callback_data)
  627. data=$2
  628. shift 2
  629. ;;
  630. --)
  631. shift
  632. break
  633. ;;
  634. esac
  635. done
  636. [[ $function ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-f, --function]"
  637. [[ $data ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-d, --callback_data]"
  638. [[ ${_BOT_HANDLE_[$data]} ]] && MessageError API "$_ERR_HANDLE_EXISTS_" '[-d, --callback_data]'
  639. _BOT_HANDLE_[$data]=func:$function' '$args
  640. return 0
  641. }
  642. ShellBot.regHandleExec()
  643. {
  644. local cmd data
  645. local param=$(getopt --name "$FUNCNAME" \
  646. --options 'c:d:' \
  647. --longoptions 'command:,
  648. callback_data:' \
  649. -- "$@")
  650. eval set -- "$param"
  651. while :
  652. do
  653. case $1 in
  654. -c|--command)
  655. cmd=$2
  656. shift 2
  657. ;;
  658. -d|--callback_data)
  659. data=$2
  660. shift 2
  661. ;;
  662. --)
  663. shift
  664. break
  665. ;;
  666. esac
  667. done
  668. [[ $cmd ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --command]"
  669. [[ $data ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-d, --callback_data]"
  670. [[ ${_BOT_HANDLE_[$data]} ]] && MessageError API "$_ERR_HANDLE_EXISTS_" "[-d, --callback_data]"
  671. _BOT_HANDLE_[$data]=exec:$cmd
  672. return 0
  673. }
  674. ShellBot.watchHandle()
  675. {
  676. local data flag cmd
  677. local param=$(getopt --name "$FUNCNAME" \
  678. --options 'd' \
  679. --longoptions 'callback_data' \
  680. -- "$@")
  681. eval set -- "$param"
  682. while :
  683. do
  684. case $1 in
  685. -d|--callback_data)
  686. shift 2
  687. data=$1
  688. ;;
  689. *)
  690. shift
  691. break
  692. ;;
  693. esac
  694. done
  695. # Handles (somente-leitura)
  696. readonly _BOT_HANDLE_
  697. [[ $data ]] || return 1 # vazio
  698. IFS=':' read -r flag cmd <<< "${_BOT_HANDLE_[$data]}"
  699. case $flag in
  700. func) $cmd;;
  701. exec) eval "$cmd";;
  702. esac
  703. # retorno
  704. return 0
  705. }
  706. ShellBot.getWebhookInfo()
  707. {
  708. # Variável local
  709. local jq_obj
  710. # Chama o método getMe passando o endereço da API, seguido do nome do método.
  711. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.})
  712. # Verifica o status de retorno do método
  713. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  714. return $?
  715. }
  716. ShellBot.deleteWebhook()
  717. {
  718. # Variável local
  719. local jq_obj
  720. # Chama o método getMe passando o endereço da API, seguido do nome do método.
  721. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.})
  722. # Verifica o status de retorno do método
  723. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  724. return $?
  725. }
  726. ShellBot.setWebhook()
  727. {
  728. local url certificate max_connections allowed_updates jq_obj
  729. local param=$(getopt --name "$FUNCNAME" \
  730. --options 'u:c:m:a:' \
  731. --longoptions 'url:,
  732. certificate:,
  733. max_connections:,
  734. allowed_updates:' \
  735. -- "$@")
  736. eval set -- "$param"
  737. while :
  738. do
  739. case $1 in
  740. -u|--url)
  741. url=$2
  742. shift 2
  743. ;;
  744. -c|--certificate)
  745. CheckArgType file "$1" "$2"
  746. certificate=$2
  747. shift 2
  748. ;;
  749. -m|--max_connections)
  750. CheckArgType int "$1" "$2"
  751. max_connections=$2
  752. shift 2
  753. ;;
  754. -a|--allowed_updates)
  755. allowed_updates=$2
  756. shift 2
  757. ;;
  758. --)
  759. shift
  760. break
  761. ;;
  762. esac
  763. done
  764. [[ $url ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --url]"
  765. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  766. ${url:+-d url="$url"} \
  767. ${certificate:+-d certificate="$certificate"} \
  768. ${max_connections:+-d max_connections="$max_connections"} \
  769. ${allowed_updates:+-d allowed_updates="$allowed_updates"})
  770. # Testa o retorno do método.
  771. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  772. # Status
  773. return $?
  774. }
  775. ShellBot.setChatPhoto()
  776. {
  777. local chat_id photo jq_obj
  778. local param=$(getopt --name "$FUNCNAME" \
  779. --options 'c:p:' \
  780. --longoptions 'chat_id:,photo:' \
  781. -- "$@")
  782. eval set -- "$param"
  783. while :
  784. do
  785. case $1 in
  786. -c|--chat_id)
  787. chat_id=$2
  788. shift 2
  789. ;;
  790. -p|--photo)
  791. CheckArgType file "$1" "$2"
  792. photo=$2
  793. shift 2
  794. ;;
  795. --)
  796. shift
  797. break
  798. ;;
  799. esac
  800. done
  801. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  802. [[ $photo ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-p, --photo]"
  803. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  804. ${chat_id:+-F chat_id="$chat_id"} \
  805. ${photo:+-F photo="$photo"})
  806. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  807. # Status
  808. return $?
  809. }
  810. ShellBot.deleteChatPhoto()
  811. {
  812. local chat_id jq_obj
  813. local param=$(getopt --name "$FUNCNAME" \
  814. --options 'c:' \
  815. --longoptions 'chat_id:' \
  816. -- "$@")
  817. eval set -- "$param"
  818. while :
  819. do
  820. case $1 in
  821. -c|--chat_id)
  822. chat_id=$2
  823. shift 2
  824. ;;
  825. --)
  826. shift
  827. break
  828. ;;
  829. esac
  830. done
  831. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  832. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  833. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  834. # Status
  835. return $?
  836. }
  837. ShellBot.setChatTitle()
  838. {
  839. local chat_id title jq_obj
  840. local param=$(getopt --name "$FUNCNAME" \
  841. --options 'c:t:' \
  842. --longoptions 'chat_id:,title:' \
  843. -- "$@")
  844. eval set -- "$param"
  845. while :
  846. do
  847. case $1 in
  848. -c|--chat_id)
  849. chat_id=$2
  850. shift 2
  851. ;;
  852. -t|--title)
  853. title=$2
  854. shift 2
  855. ;;
  856. --)
  857. shift
  858. break
  859. ;;
  860. esac
  861. done
  862. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  863. [[ $title ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --title]"
  864. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  865. ${chat_id:+-d chat_id="$chat_id"} \
  866. ${title:+-d title="$title"})
  867. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  868. # Status
  869. return $?
  870. }
  871. ShellBot.setChatDescription()
  872. {
  873. local chat_id description jq_obj
  874. local param=$(getopt --name "$FUNCNAME" \
  875. --options 'c:d:' \
  876. --longoptions 'chat_id:,description:' \
  877. -- "$@")
  878. eval set -- "$param"
  879. while :
  880. do
  881. case $1 in
  882. -c|--chat_id)
  883. chat_id=$2
  884. shift 2
  885. ;;
  886. -d|--description)
  887. description=$2
  888. shift 2
  889. ;;
  890. --)
  891. shift
  892. break
  893. ;;
  894. esac
  895. done
  896. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  897. [[ $description ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-d, --description]"
  898. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  899. ${chat_id:+-d chat_id="$chat_id"} \
  900. ${description:+-d description="$description"})
  901. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  902. # Status
  903. return $?
  904. }
  905. ShellBot.pinChatMessage()
  906. {
  907. local chat_id message_id disable_notification jq_obj
  908. local param=$(getopt --name "$FUNCNAME" \
  909. --options 'c:m:n:' \
  910. --longoptions 'chat_id:,
  911. message_id:,
  912. disable_notification:' \
  913. -- "$@")
  914. eval set -- "$param"
  915. while :
  916. do
  917. case $1 in
  918. -c|--chat_id)
  919. chat_id=$2
  920. shift 2
  921. ;;
  922. -m|--message_id)
  923. CheckArgType int "$1" "$2"
  924. message_id=$2
  925. shift 2
  926. ;;
  927. -n|--disable_notification)
  928. CheckArgType bool "$1" "$2"
  929. disable_notification=$2
  930. shift 2
  931. ;;
  932. --)
  933. shift
  934. break
  935. ;;
  936. esac
  937. done
  938. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  939. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  940. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  941. ${chat_id:+-d chat_id="$chat_id"} \
  942. ${message_id:+-d message_id="$message_id"} \
  943. ${disable_notification:+-d disable_notification="$disable_notification"})
  944. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  945. # Status
  946. return $?
  947. }
  948. ShellBot.unpinChatMessage()
  949. {
  950. local chat_id jq_obj
  951. local param=$(getopt --name "$FUNCNAME" \
  952. --options 'c:' \
  953. --longoptions 'chat_id:' \
  954. -- "$@")
  955. eval set -- "$param"
  956. while :
  957. do
  958. case $1 in
  959. -c|--chat_id)
  960. chat_id=$2
  961. shift 2
  962. ;;
  963. --)
  964. shift
  965. break
  966. ;;
  967. esac
  968. done
  969. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  970. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  971. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  972. # Status
  973. return $?
  974. }
  975. ShellBot.restrictChatMember()
  976. {
  977. local chat_id user_id until_date permissions jq_obj
  978. local param=$(getopt --name "$FUNCNAME" \
  979. --options 'c:u:d:p:' \
  980. --longoptions 'chat_id:,
  981. user_id:,
  982. until_date:,
  983. permissions:' \
  984. -- "$@")
  985. eval set -- "$param"
  986. while :
  987. do
  988. case $1 in
  989. -c|--chat_id)
  990. chat_id=$2
  991. shift 2
  992. ;;
  993. -u|--user_id)
  994. CheckArgType int "$1" "$2"
  995. user_id=$2
  996. shift 2
  997. ;;
  998. -d|--until_date)
  999. CheckArgType int "$1" "$2"
  1000. until_date=$2
  1001. shift 2
  1002. ;;
  1003. -p|--permissions)
  1004. permissions=$2
  1005. shift 2
  1006. ;;
  1007. --)
  1008. shift
  1009. break
  1010. ;;
  1011. esac
  1012. done
  1013. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1014. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  1015. [[ $permissions ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-p, --permissions]"
  1016. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1017. ${chat_id:+-d chat_id="$chat_id"} \
  1018. ${user_id:+-d user_id="$user_id"} \
  1019. ${until_date:+-d until_date="$until_date"} \
  1020. ${permissions:+-d permissions="$permissions"})
  1021. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1022. # Status
  1023. return $?
  1024. }
  1025. ShellBot.promoteChatMember()
  1026. {
  1027. local chat_id user_id can_change_info can_post_messages \
  1028. can_edit_messages can_delete_messages can_invite_users \
  1029. can_restrict_members can_pin_messages can_promote_members \
  1030. jq_obj
  1031. local param=$(getopt --name "$FUNCNAME" \
  1032. --options 'c:u:i:p:e:d:v:r:f:m:' \
  1033. --longoptions 'chat_id:,
  1034. user_id:,
  1035. can_change_info:,
  1036. can_post_messages:,
  1037. can_edit_messages:,
  1038. can_delete_messages:,
  1039. can_invite_users:,
  1040. can_restrict_members:,
  1041. can_pin_messages:,
  1042. can_promote_members:' \
  1043. -- "$@")
  1044. eval set -- "$param"
  1045. while :
  1046. do
  1047. case $1 in
  1048. -c|--chat_id)
  1049. chat_id=$2
  1050. shift 2
  1051. ;;
  1052. -u|--user_id)
  1053. CheckArgType int "$1" "$2"
  1054. user_id=$2
  1055. shift 2
  1056. ;;
  1057. -i|--can_change_info)
  1058. CheckArgType bool "$1" "$2"
  1059. can_change_info=$2
  1060. shift 2
  1061. ;;
  1062. -p|--can_post_messages)
  1063. CheckArgType bool "$1" "$2"
  1064. can_post_messages=$2
  1065. shift 2
  1066. ;;
  1067. -e|--can_edit_messages)
  1068. CheckArgType bool "$1" "$2"
  1069. can_edit_messages=$2
  1070. shift 2
  1071. ;;
  1072. -d|--can_delete_messages)
  1073. CheckArgType bool "$1" "$2"
  1074. can_delete_messages=$2
  1075. shift 2
  1076. ;;
  1077. -v|--can_invite_users)
  1078. CheckArgType bool "$1" "$2"
  1079. can_invite_users=$2
  1080. shift 2
  1081. ;;
  1082. -r|--can_restrict_members)
  1083. CheckArgType bool "$1" "$2"
  1084. can_restrict_members=$2
  1085. shift 2
  1086. ;;
  1087. -f|--can_pin_messages)
  1088. CheckArgType bool "$1" "$2"
  1089. can_pin_messages=$2
  1090. shift 2
  1091. ;;
  1092. -m|--can_promote_members)
  1093. CheckArgType bool "$1" "$2"
  1094. can_promote_members=$2
  1095. shift 2
  1096. ;;
  1097. --)
  1098. shift
  1099. break
  1100. ;;
  1101. esac
  1102. done
  1103. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1104. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --user_id]"
  1105. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1106. ${chat_id:+-d chat_id="$chat_id"} \
  1107. ${user_id:+-d user_id="$user_id"} \
  1108. ${can_change_info:+-d can_change_info="$can_change_info"} \
  1109. ${can_post_messages:+-d can_post_messages="$can_post_messages"} \
  1110. ${can_edit_messages:+-d can_edit_messages="$can_edit_messages"} \
  1111. ${can_delete_messages:+-d can_delete_messages="$can_delete_messages"} \
  1112. ${can_invite_users:+-d can_invite_users="$can_invite_users"} \
  1113. ${can_restrict_members:+-d can_restrict_members="$can_restrict_members"} \
  1114. ${can_pin_messages:+-d can_pin_messages="$can_pin_messages"} \
  1115. ${can_promote_members:+-d can_promote_members="$can_promote_members"})
  1116. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1117. # Status
  1118. return $?
  1119. }
  1120. ShellBot.exportChatInviteLink()
  1121. {
  1122. local chat_id jq_obj
  1123. local param=$(getopt --name "$FUNCNAME" \
  1124. --options 'c:' \
  1125. --longoptions 'chat_id:' \
  1126. -- "$@")
  1127. eval set -- "$param"
  1128. while :
  1129. do
  1130. case $1 in
  1131. -c|--chat_id)
  1132. chat_id=$2
  1133. shift 2
  1134. ;;
  1135. --)
  1136. shift
  1137. break
  1138. ;;
  1139. esac
  1140. done
  1141. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1142. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  1143. # Testa o retorno do método.
  1144. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1145. # Status
  1146. return $?
  1147. }
  1148. ShellBot.sendVideoNote()
  1149. {
  1150. local chat_id video_note duration length disable_notification \
  1151. reply_to_message_id reply_markup jq_obj
  1152. local param=$(getopt --name "$FUNCNAME" \
  1153. --options 'c:v:t:l:n:r:k:' \
  1154. --longoptions 'chat_id:,
  1155. video_note:,
  1156. duration:,
  1157. length:,
  1158. disable_notification:,
  1159. reply_to_message_id:,
  1160. reply_markup:' \
  1161. -- "$@")
  1162. # Define os parâmetros posicionais
  1163. eval set -- "$param"
  1164. while :
  1165. do
  1166. case $1 in
  1167. -c|--chat_id)
  1168. chat_id=$2
  1169. shift 2
  1170. ;;
  1171. -v|--video_note)
  1172. CheckArgType file "$1" "$2"
  1173. video_note=$2
  1174. shift 2
  1175. ;;
  1176. -t|--duration)
  1177. CheckArgType int "$1" "$2"
  1178. duration=$2
  1179. shift 2
  1180. ;;
  1181. -l|--length)
  1182. CheckArgType int "$1" "$2"
  1183. length=$2
  1184. shift 2
  1185. ;;
  1186. -n|--disable_notification)
  1187. CheckArgType bool "$1" "$2"
  1188. disable_notification=$2
  1189. shift 2
  1190. ;;
  1191. -r|--reply_to_message_id)
  1192. CheckArgType int "$1" "$2"
  1193. reply_to_message_id=$2
  1194. shift 2
  1195. ;;
  1196. -k|--reply_markup)
  1197. reply_markup=$2
  1198. shift 2
  1199. ;;
  1200. --)
  1201. shift
  1202. break
  1203. ;;
  1204. esac
  1205. done
  1206. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1207. [[ $video_note ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-v, --video_note]"
  1208. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1209. ${chat_id:+-F chat_id="$chat_id"} \
  1210. ${video_note:+-F video_note="$video_note"} \
  1211. ${duration:+-F duration="$duration"} \
  1212. ${length:+-F length="$length"} \
  1213. ${disable_notification:+-F disable_notification="$disable_notification"} \
  1214. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  1215. ${reply_markup:+-F reply_markup="$reply_markup"})
  1216. # Testa o retorno do método.
  1217. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1218. # Status
  1219. return $?
  1220. }
  1221. ShellBot.InlineKeyboardButton()
  1222. {
  1223. local __button __line __text __url __callback_data \
  1224. __switch_inline_query __switch_inline_query_current_chat
  1225. local __param=$(getopt --name "$FUNCNAME" \
  1226. --options 'b:l:t:u:c:q:s:' \
  1227. --longoptions 'button:,
  1228. line:,
  1229. text:,
  1230. url:,
  1231. callback_data:,
  1232. switch_inline_query:,
  1233. switch_inline_query_chat:' \
  1234. -- "$@")
  1235. eval set -- "$__param"
  1236. while :
  1237. do
  1238. case $1 in
  1239. -b|--button)
  1240. # Ponteiro que recebe o endereço de "button" com as definições
  1241. # da configuração do botão inserido.
  1242. CheckArgType var "$1" "$2"
  1243. __button=$2
  1244. shift 2
  1245. ;;
  1246. -l|--line)
  1247. CheckArgType int "$1" "$2"
  1248. __line=$(($2-1))
  1249. shift 2
  1250. ;;
  1251. -t|--text)
  1252. __text=$(echo -e "$2")
  1253. shift 2
  1254. ;;
  1255. -u|--url)
  1256. __url=$2
  1257. shift 2
  1258. ;;
  1259. -c|--callback_data)
  1260. __callback_data=$2
  1261. shift 2
  1262. ;;
  1263. -q|--switch_inline_query)
  1264. __switch_inline_query=$2
  1265. shift 2
  1266. ;;
  1267. -s|--switch_inline_query_current_chat)
  1268. __switch_inline_query_current_chat=$2
  1269. shift 2
  1270. ;;
  1271. --)
  1272. shift
  1273. break
  1274. ;;
  1275. esac
  1276. done
  1277. [[ $__button ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-b, --button]"
  1278. [[ $__text ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --text]"
  1279. [[ $__callback_data ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --callback_data]"
  1280. [[ $__line ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-l, --line]"
  1281. __button=$__button[$__line]
  1282. printf -v $__button '%s' "${!__button#[}"
  1283. printf -v $__button '%s' "${!__button%]}"
  1284. printf -v $__button '%s {"text": "%s", "callback_data": "%s", "url": "%s", "switch_inline_query": "%s", "switch_inline_query_current_chat": "%s"}' \
  1285. "${!__button:+${!__button},}" \
  1286. "${__text}" \
  1287. "${__callback_data}" \
  1288. "${__url}" \
  1289. "${__switch_inline_query}" \
  1290. "${__switch_inline_query_current_chat}"
  1291. printf -v $__button '%s' "[${!__button}]"
  1292. return $?
  1293. }
  1294. ShellBot.InlineKeyboardMarkup()
  1295. {
  1296. local __button __keyboard
  1297. local __param=$(getopt --name "$FUNCNAME" \
  1298. --options 'b:' \
  1299. --longoptions 'button:' \
  1300. -- "$@")
  1301. eval set -- "$__param"
  1302. while :
  1303. do
  1304. case $1 in
  1305. -b|--button)
  1306. # Ponteiro que recebe o endereço da variável "teclado" com as definições
  1307. # de configuração do botão inserido.
  1308. CheckArgType var "$1" "$2"
  1309. __button="$2"
  1310. shift 2
  1311. ;;
  1312. --)
  1313. shift
  1314. break
  1315. ;;
  1316. esac
  1317. done
  1318. [[ $__button ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-b, --button]"
  1319. __button=$__button[@]
  1320. printf -v __keyboard '%s,' "${!__button}"
  1321. printf -v __keyboard '%s' "${__keyboard%,}"
  1322. # Constroi a estrutura dos objetos + array keyboard, define os valores e salva as configurações.
  1323. # Por padrão todos os valores são 'false' até que seja definido.
  1324. printf '{"inline_keyboard": [%s]}' "${__keyboard}"
  1325. return $?
  1326. }
  1327. ShellBot.answerCallbackQuery()
  1328. {
  1329. local callback_query_id text show_alert url cache_time jq_obj
  1330. local param=$(getopt --name "$FUNCNAME" \
  1331. --options 'c:t:s:u:e:' \
  1332. --longoptions 'callback_query_id:,
  1333. text:,
  1334. show_alert:,
  1335. url:,
  1336. cache_time:' \
  1337. -- "$@")
  1338. eval set -- "$param"
  1339. while :
  1340. do
  1341. case $1 in
  1342. -c|--callback_query_id)
  1343. callback_query_id=$2
  1344. shift 2
  1345. ;;
  1346. -t|--text)
  1347. text=$(echo -e "$2")
  1348. shift 2
  1349. ;;
  1350. -s|--show_alert)
  1351. # boolean
  1352. CheckArgType bool "$1" "$2"
  1353. show_alert=$2
  1354. shift 2
  1355. ;;
  1356. -u|--url)
  1357. url=$2
  1358. shift 2
  1359. ;;
  1360. -e|--cache_time)
  1361. # inteiro
  1362. CheckArgType int "$1" "$2"
  1363. cache_time=$2
  1364. shift 2
  1365. ;;
  1366. --)
  1367. shift
  1368. break
  1369. ;;
  1370. esac
  1371. done
  1372. [[ $callback_query_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --callback_query_id]"
  1373. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1374. ${callback_query_id:+-d callback_query_id="$callback_query_id"} \
  1375. ${text:+-d text="$text"} \
  1376. ${show_alert:+-d show_alert="$show_alert"} \
  1377. ${url:+-d url="$url"} \
  1378. ${cache_time:+-d cache_time="$cache_time"})
  1379. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1380. return $?
  1381. }
  1382. # Cria objeto que representa um teclado personalizado com opções de resposta
  1383. ShellBot.ReplyKeyboardMarkup()
  1384. {
  1385. # Variáveis locais
  1386. local __button __resize_keyboard __on_time_keyboard __selective __keyboard
  1387. # Lê os parâmetros da função.
  1388. local __param=$(getopt --name "$FUNCNAME" \
  1389. --options 'b:r:t:s:' \
  1390. --longoptions 'button:,
  1391. resize_keyboard:,
  1392. one_time_keyboard:,
  1393. selective:' \
  1394. -- "$@")
  1395. # Transforma os parâmetros da função em parâmetros posicionais
  1396. #
  1397. # Exemplo:
  1398. # --param1 arg1 --param2 arg2 --param3 arg3 ...
  1399. # $1 $2 $3
  1400. eval set -- "$__param"
  1401. # Aguarda leitura dos parâmetros
  1402. while :
  1403. do
  1404. # Lê o parâmetro da primeira posição "$1"; Se for um parâmetro válido,
  1405. # salva o valor do argumento na posição '$2' e desloca duas posições a esquerda (shift 2); Repete o processo
  1406. # até que o valor de '$1' seja igual '--' e finaliza o loop.
  1407. case $1 in
  1408. -b|--button)
  1409. CheckArgType var "$1" "$2"
  1410. __button=$2
  1411. shift 2
  1412. ;;
  1413. -r|--resize_keyboard)
  1414. # Tipo: boolean
  1415. CheckArgType bool "$1" "$2"
  1416. __resize_keyboard=$2
  1417. shift 2
  1418. ;;
  1419. -t|--one_time_keyboard)
  1420. # Tipo: boolean
  1421. CheckArgType bool "$1" "$2"
  1422. __on_time_keyboard=$2
  1423. shift 2
  1424. ;;
  1425. -s|--selective)
  1426. # Tipo: boolean
  1427. CheckArgType bool "$1" "$2"
  1428. __selective=$2
  1429. shift 2
  1430. ;;
  1431. --)
  1432. shift
  1433. break
  1434. ;;
  1435. esac
  1436. done
  1437. # Imprime mensagem de erro se o parâmetro obrigatório for omitido.
  1438. [[ $__button ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-b, --button]"
  1439. __button=$__button[@]
  1440. printf -v __keyboard '%s,' "${!__button}"
  1441. printf -v __keyboard '%s' "${__keyboard%,}"
  1442. # Constroi a estrutura dos objetos + array keyboard, define os valores e salva as configurações.
  1443. # Por padrão todos os valores são 'false' até que seja definido.
  1444. printf '{"keyboard": [%s], "resize_keyboard": %s, "one_time_keyboard": %s, "selective": %s}' \
  1445. "${__keyboard}" \
  1446. "${__resize_keyboard:-false}" \
  1447. "${__on_time_keyboard:-false}" \
  1448. "${__selective:-false}"
  1449. # status
  1450. return $?
  1451. }
  1452. ShellBot.KeyboardButton()
  1453. {
  1454. local __text __contact __location __button __line __request_poll
  1455. local __param=$(getopt --name "$FUNCNAME" \
  1456. --options 'b:l:t:c:o:r:' \
  1457. --longoptions 'button:,
  1458. line:,
  1459. text:,
  1460. request_contact:,
  1461. request_location:,
  1462. request_poll:' \
  1463. -- "$@")
  1464. eval set -- "$__param"
  1465. while :
  1466. do
  1467. case $1 in
  1468. -b|--button)
  1469. CheckArgType var "$1" "$2"
  1470. __button=$2
  1471. shift 2
  1472. ;;
  1473. -l|--line)
  1474. CheckArgType int "$1" "$2"
  1475. __line=$(($2-1))
  1476. shift 2
  1477. ;;
  1478. -t|--text)
  1479. __text=$(echo -e "$2")
  1480. shift 2
  1481. ;;
  1482. -c|--request_contact)
  1483. CheckArgType bool "$1" "$2"
  1484. __contact=$2
  1485. shift 2
  1486. ;;
  1487. -o|--request_location)
  1488. CheckArgType bool "$1" "$2"
  1489. __location=$2
  1490. shift 2
  1491. ;;
  1492. -r|--request_poll)
  1493. __request_poll=$2
  1494. shift 2
  1495. ;;
  1496. --)
  1497. shift
  1498. break
  1499. ;;
  1500. esac
  1501. done
  1502. [[ $__button ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-b, --button]"
  1503. [[ $__text ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --text]"
  1504. [[ $__line ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-l, --line]"
  1505. __button=$__button[$__line]
  1506. printf -v $__button '%s' "${!__button#[}"
  1507. printf -v $__button '%s' "${!__button%]}"
  1508. printf -v $__button '%s {"text": "%s", "request_contact": %s, "request_location": %s, "request_poll": %s}' \
  1509. "${!__button:+${!__button},}" \
  1510. "${__text}" \
  1511. "${__contact:-false}" \
  1512. "${__location:-false}" \
  1513. "${__request_poll:-\"\"}"
  1514. printf -v $__button '%s' "[${!__button}]"
  1515. return $?
  1516. }
  1517. ShellBot.ForceReply()
  1518. {
  1519. local selective
  1520. local param=$(getopt --name "$FUNCNAME" \
  1521. --options 's:' \
  1522. --longoptions 'selective:' \
  1523. -- "$@")
  1524. eval set -- "$param"
  1525. while :
  1526. do
  1527. case $1 in
  1528. -s|--selective)
  1529. CheckArgType bool "$1" "$2"
  1530. selective=$2
  1531. shift 2
  1532. ;;
  1533. --)
  1534. shift
  1535. break
  1536. ;;
  1537. esac
  1538. done
  1539. printf '{"force_reply": true, "selective": %s}' ${selective:-false}
  1540. return $?
  1541. }
  1542. ShellBot.ReplyKeyboardRemove()
  1543. {
  1544. local selective
  1545. local param=$(getopt --name "$FUNCNAME" \
  1546. --options 's:' \
  1547. --longoptions 'selective:' \
  1548. -- "$@")
  1549. eval set -- "$param"
  1550. while :
  1551. do
  1552. case $1 in
  1553. -s|--selective)
  1554. CheckArgType bool "$1" "$2"
  1555. selective=$2
  1556. shift 2
  1557. ;;
  1558. --)
  1559. shift
  1560. break
  1561. ;;
  1562. esac
  1563. done
  1564. printf '{"remove_keyboard": true, "selective": %s}' ${selective:-false}
  1565. return $?
  1566. }
  1567. # Envia mensagens
  1568. ShellBot.sendMessage()
  1569. {
  1570. # Variáveis locais
  1571. local chat_id text parse_mode disable_web_page_preview
  1572. local disable_notification reply_to_message_id reply_markup jq_obj
  1573. # Lê os parâmetros da função
  1574. local param=$(getopt --name "$FUNCNAME" \
  1575. --options 'c:t:p:w:n:r:k:' \
  1576. --longoptions 'chat_id:,
  1577. text:,
  1578. parse_mode:,
  1579. disable_web_page_preview:,
  1580. disable_notification:,
  1581. reply_to_message_id:,
  1582. reply_markup:' \
  1583. -- "$@")
  1584. # Define os parâmetros posicionais
  1585. eval set -- "$param"
  1586. while :
  1587. do
  1588. case $1 in
  1589. -c|--chat_id)
  1590. chat_id=$2
  1591. shift 2
  1592. ;;
  1593. -t|--text)
  1594. text=$(echo -e "$2")
  1595. shift 2
  1596. ;;
  1597. -p|--parse_mode)
  1598. # Tipo: "markdown" ou "html"
  1599. parse_mode=$2
  1600. shift 2
  1601. ;;
  1602. -w|--disable_web_page_preview)
  1603. # Tipo: boolean
  1604. CheckArgType bool "$1" "$2"
  1605. disable_web_page_preview=$2
  1606. shift 2
  1607. ;;
  1608. -n|--disable_notification)
  1609. # Tipo: boolean
  1610. CheckArgType bool "$1" "$2"
  1611. disable_notification=$2
  1612. shift 2
  1613. ;;
  1614. -r|--reply_to_message_id)
  1615. # Tipo: inteiro
  1616. CheckArgType int "$1" "$2"
  1617. reply_to_message_id=$2
  1618. shift 2
  1619. ;;
  1620. -k|--reply_markup)
  1621. reply_markup=$2
  1622. shift 2
  1623. ;;
  1624. --)
  1625. shift
  1626. break
  1627. ;;
  1628. esac
  1629. done
  1630. # Parâmetros obrigatórios.
  1631. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1632. [[ $text ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --text]"
  1633. # Chama o método da API, utilizando o comando request especificado; Os parâmetros
  1634. # e valores são passados no form e lidos pelo método. O retorno do método é redirecionado para o arquivo 'update.Json'.
  1635. # Variáveis com valores nulos são ignoradas e consequentemente os respectivos parâmetros omitidos.
  1636. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1637. ${chat_id:+-d chat_id="$chat_id"} \
  1638. ${text:+-d text="$text"} \
  1639. ${parse_mode:+-d parse_mode="$parse_mode"} \
  1640. ${disable_web_page_preview:+-d disable_web_page_preview="$disable_web_page_preview"} \
  1641. ${disable_notification:+-d disable_notification="$disable_notification"} \
  1642. ${reply_to_message_id:+-d reply_to_message_id="$reply_to_message_id"} \
  1643. ${reply_markup:+-d reply_markup="$reply_markup"})
  1644. # Testa o retorno do método.
  1645. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1646. # Status
  1647. return $?
  1648. }
  1649. # Função para reencaminhar mensagens de qualquer tipo.
  1650. ShellBot.forwardMessage()
  1651. {
  1652. # Variáveis locais
  1653. local chat_id form_chat_id disable_notification message_id jq_obj
  1654. # Lê os parâmetros da função
  1655. local param=$(getopt --name "$FUNCNAME" \
  1656. --options 'c:f:n:m:' \
  1657. --longoptions 'chat_id:,
  1658. from_chat_id:,
  1659. disable_notification:,
  1660. message_id:' \
  1661. -- "$@")
  1662. # Define os parâmetros posicionais
  1663. eval set -- "$param"
  1664. while :
  1665. do
  1666. case $1 in
  1667. -c|--chat_id)
  1668. chat_id="$2"
  1669. shift 2
  1670. ;;
  1671. -f|--from_chat_id)
  1672. from_chat_id="$2"
  1673. shift 2
  1674. ;;
  1675. -n|--disable_notification)
  1676. # Tipo: boolean
  1677. CheckArgType bool "$1" "$2"
  1678. disable_notification="$2"
  1679. shift 2
  1680. ;;
  1681. -m|--message_id)
  1682. # Tipo: inteiro
  1683. CheckArgType int "$1" "$2"
  1684. message_id="$2"
  1685. shift 2
  1686. ;;
  1687. --)
  1688. shift
  1689. break
  1690. ;;
  1691. esac
  1692. done
  1693. # Parâmetros obrigatórios.
  1694. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1695. [[ $from_chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-f, --from_chat_id]"
  1696. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  1697. # Chama o método
  1698. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1699. ${chat_id:+-d chat_id="$chat_id"} \
  1700. ${from_chat_id:+-d from_chat_id="$from_chat_id"} \
  1701. ${disable_notification:+-d disable_notification="$disable_notification"} \
  1702. ${message_id:+-d message_id="$message_id"})
  1703. # Retorno do método
  1704. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1705. # status
  1706. return $?
  1707. }
  1708. # Utilize essa função para enviar fotos.
  1709. ShellBot.sendPhoto()
  1710. {
  1711. # Variáveis locais
  1712. local chat_id photo caption disable_notification
  1713. local parse_mode reply_to_message_id reply_markup jq_obj
  1714. # Lê os parâmetros da função
  1715. local param=$(getopt --name "$FUNCNAME" \
  1716. --options 'c:p:t:m:n:r:k:' \
  1717. --longoptions 'chat_id:,
  1718. photo:,
  1719. caption:,
  1720. parse_mode:,
  1721. disable_notification:,
  1722. reply_to_message_id:,
  1723. reply_markup:' \
  1724. -- "$@")
  1725. # Define os parâmetros posicionais
  1726. eval set -- "$param"
  1727. while :
  1728. do
  1729. case $1 in
  1730. -c|--chat_id)
  1731. chat_id=$2
  1732. shift 2
  1733. ;;
  1734. -p|--photo)
  1735. CheckArgType file "$1" "$2"
  1736. photo=$2
  1737. shift 2
  1738. ;;
  1739. -t|--caption)
  1740. # Limite máximo de caracteres: 200
  1741. caption=$(echo -e "$2")
  1742. shift 2
  1743. ;;
  1744. -m|--parse_mode)
  1745. parse_mode=$2
  1746. shift 2
  1747. ;;
  1748. -n|--disable_notification)
  1749. # Tipo: boolean
  1750. CheckArgType bool "$1" "$2"
  1751. disable_notification=$2
  1752. shift 2
  1753. ;;
  1754. -r|--reply_to_message_id)
  1755. # Tipo: inteiro
  1756. CheckArgType int "$1" "$2"
  1757. reply_to_message_id=$2
  1758. shift 2
  1759. ;;
  1760. -k|--reply_markup)
  1761. reply_markup=$2
  1762. shift 2
  1763. ;;
  1764. --)
  1765. shift
  1766. break
  1767. ;;
  1768. esac
  1769. done
  1770. # Parâmetros obrigatórios
  1771. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1772. [[ $photo ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-p, --photo]"
  1773. # Chama o método
  1774. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1775. ${chat_id:+-F chat_id="$chat_id"} \
  1776. ${photo:+-F photo="$photo"} \
  1777. ${caption:+-F caption="$caption"} \
  1778. ${parse_mode:+-F parse_mode="$parse_mode"} \
  1779. ${disable_notification:+-F disable_notification="$disable_notification"} \
  1780. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  1781. ${reply_markup:+-F reply_markup="$reply_markup"})
  1782. # Retorno do método
  1783. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1784. # Status
  1785. return $?
  1786. }
  1787. # Utilize essa função para enviar arquivos de audio.
  1788. ShellBot.sendAudio()
  1789. {
  1790. # Variáveis locais
  1791. local chat_id audio caption duration performer title
  1792. local parse_mode disable_notification reply_to_message_id reply_markup jq_obj
  1793. # Lê os parâmetros da função
  1794. local param=$(getopt --name "$FUNCNAME" \
  1795. --options 'c:a:t:m:d:e:i:n:r:k' \
  1796. --longoptions 'chat_id:,
  1797. audio:,
  1798. caption:,
  1799. parse_mode:,
  1800. duration:,
  1801. performer:,
  1802. title:,
  1803. disable_notification:,
  1804. reply_to_message_id:,
  1805. reply_markup:' \
  1806. -- "$@")
  1807. # Define os parâmetros posicionais
  1808. eval set -- "$param"
  1809. while :
  1810. do
  1811. case $1 in
  1812. -c|--chat_id)
  1813. chat_id=$2
  1814. shift 2
  1815. ;;
  1816. -a|--audio)
  1817. CheckArgType file "$1" "$2"
  1818. audio=$2
  1819. shift 2
  1820. ;;
  1821. -t|--caption)
  1822. caption=$(echo -e "$2")
  1823. shift 2
  1824. ;;
  1825. -m|--parse_mode)
  1826. parse_mode=$2
  1827. shift 2
  1828. ;;
  1829. -d|--duration)
  1830. # Tipo: inteiro
  1831. CheckArgType int "$1" "$2"
  1832. duration=$2
  1833. shift 2
  1834. ;;
  1835. -e|--performer)
  1836. performer=$2
  1837. shift 2
  1838. ;;
  1839. -i|--title)
  1840. title=$2
  1841. shift 2
  1842. ;;
  1843. -n|--disable_notification)
  1844. # Tipo: boolean
  1845. CheckArgType bool "$1" "$2"
  1846. disable_notification=$2
  1847. shift 2
  1848. ;;
  1849. -r|--reply_to_message_id)
  1850. # Tipo: inteiro
  1851. CheckArgType int "$1" "$2"
  1852. reply_to_message_id=$2
  1853. shift 2
  1854. ;;
  1855. -k|--reply_markup)
  1856. reply_markup=$2
  1857. shift 2
  1858. ;;
  1859. --)
  1860. shift
  1861. break
  1862. ;;
  1863. esac
  1864. done
  1865. # Parâmetros obrigatórios
  1866. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1867. [[ $audio ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-a, --audio]"
  1868. # Chama o método
  1869. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1870. ${chat_id:+-F chat_id="$chat_id"} \
  1871. ${audio:+-F audio="$audio"} \
  1872. ${caption:+-F caption="$caption"} \
  1873. ${parse_mode:+-F parse_mode="$parse_mode"} \
  1874. ${duration:+-F duration="$duration"} \
  1875. ${performer:+-F performer="$performer"} \
  1876. ${title:+-F title="$title"} \
  1877. ${disable_notification:+-F disable_notification="$disable_notification"} \
  1878. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  1879. ${reply_markup:+-F reply_markup="$reply_markup"})
  1880. # Retorno do método
  1881. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1882. # Status
  1883. return $?
  1884. }
  1885. # Utilize essa função para enviar documentos.
  1886. ShellBot.sendDocument()
  1887. {
  1888. # Variáveis locais
  1889. local chat_id document caption disable_notification
  1890. local parse_mode reply_to_message_id reply_markup jq_obj
  1891. # Lê os parâmetros da função
  1892. local param=$(getopt --name "$FUNCNAME" \
  1893. --options 'c:d:t:m:n:r:k:' \
  1894. --longoptions 'chat_id:,
  1895. document:,
  1896. caption:,
  1897. parse_mode:,
  1898. disable_notification:,
  1899. reply_to_message_id:,
  1900. reply_markup:' \
  1901. -- "$@")
  1902. # Define os parâmetros posicionais
  1903. eval set -- "$param"
  1904. while :
  1905. do
  1906. case $1 in
  1907. -c|--chat_id)
  1908. chat_id=$2
  1909. shift 2
  1910. ;;
  1911. -d|--document)
  1912. CheckArgType file "$1" "$2"
  1913. document=$2
  1914. shift 2
  1915. ;;
  1916. -t|--caption)
  1917. caption=$(echo -e "$2")
  1918. shift 2
  1919. ;;
  1920. -m|--parse_mode)
  1921. parse_mode=$2
  1922. shift 2
  1923. ;;
  1924. -n|--disable_notification)
  1925. CheckArgType bool "$1" "$2"
  1926. disable_notification=$2
  1927. shift 2
  1928. ;;
  1929. -r|--reply_to_message_id)
  1930. CheckArgType int "$1" "$2"
  1931. reply_to_message_id=$2
  1932. shift 2
  1933. ;;
  1934. -k|--reply_markup)
  1935. reply_markup=$2
  1936. shift 2
  1937. ;;
  1938. --)
  1939. shift
  1940. break
  1941. ;;
  1942. esac
  1943. done
  1944. # Parâmetros obrigatórios
  1945. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  1946. [[ $document ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-d, --document]"
  1947. # Chama o método
  1948. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  1949. ${chat_id:+-F chat_id="$chat_id"} \
  1950. ${document:+-F document="$document"} \
  1951. ${caption:+-F caption="$caption"} \
  1952. ${parse_mode:+-F parse_mode="$parse_mode"} \
  1953. ${disable_notification:+-F disable_notification="$disable_notification"} \
  1954. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  1955. ${reply_markup:+-F reply_markup="$reply_markup"})
  1956. # Retorno do método
  1957. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  1958. # Status
  1959. return $?
  1960. }
  1961. # Utilize essa função para enviat stickers
  1962. ShellBot.sendSticker()
  1963. {
  1964. # Variáveis locais
  1965. local chat_id sticker disable_notification reply_to_message_id reply_markup jq_obj
  1966. # Lê os parâmetros da função
  1967. local param=$(getopt --name "$FUNCNAME" \
  1968. --options 'c:s:n:r:k:' \
  1969. --longoptions 'chat_id:,
  1970. sticker:,
  1971. disable_notification:,
  1972. reply_to_message_id:,
  1973. reply_markup:' \
  1974. -- "$@")
  1975. # Define os parâmetros posicionais
  1976. eval set -- "$param"
  1977. while :
  1978. do
  1979. case $1 in
  1980. -c|--chat_id)
  1981. chat_id=$2
  1982. shift 2
  1983. ;;
  1984. -s|--sticker)
  1985. CheckArgType file "$1" "$2"
  1986. sticker=$2
  1987. shift 2
  1988. ;;
  1989. -n|--disable_notification)
  1990. # Tipo: boolean
  1991. CheckArgType bool "$1" "$2"
  1992. disable_notification=$2
  1993. shift 2
  1994. ;;
  1995. -r|--reply_to_message_id)
  1996. # Tipo: inteiro
  1997. CheckArgType int "$1" "$2"
  1998. reply_to_message_id=$2
  1999. shift 2
  2000. ;;
  2001. -k|--reply_markup)
  2002. reply_markup=$2
  2003. shift 2
  2004. ;;
  2005. --)
  2006. shift
  2007. break
  2008. ;;
  2009. esac
  2010. done
  2011. # Parâmetros obrigatórios
  2012. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2013. [[ $sticker ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --sticker]"
  2014. # Chama o método
  2015. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2016. ${chat_id:+-F chat_id="$chat_id"} \
  2017. ${sticker:+-F sticker="$sticker"} \
  2018. ${disable_notification:+-F disable_notification="$disable_notification"} \
  2019. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  2020. ${reply_markup:+-F reply_markup="$reply_markup"})
  2021. # Testa o retorno do método
  2022. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2023. # Status
  2024. return $?
  2025. }
  2026. ShellBot.getStickerSet()
  2027. {
  2028. local name jq_obj
  2029. local param=$(getopt --name "$FUNCNAME" \
  2030. --options 'n:' \
  2031. --longoptions 'name:' \
  2032. -- "$@")
  2033. # parâmetros posicionais
  2034. eval set -- "$param"
  2035. while :
  2036. do
  2037. case $1 in
  2038. -n|--name)
  2039. name=$2
  2040. shift 2
  2041. ;;
  2042. --)
  2043. shift
  2044. break
  2045. ;;
  2046. esac
  2047. done
  2048. [[ $name ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-n, --name]"
  2049. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} ${name:+-d name="$name"})
  2050. # Testa o retorno do método
  2051. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2052. # Status
  2053. return $?
  2054. }
  2055. ShellBot.uploadStickerFile()
  2056. {
  2057. local user_id png_sticker jq_obj
  2058. local param=$(getopt --name "$FUNCNAME" \
  2059. --options 'u:s:' \
  2060. --longoptions 'user_id:,
  2061. png_sticker:' \
  2062. -- "$@")
  2063. eval set -- "$param"
  2064. while :
  2065. do
  2066. case $1 in
  2067. -u|--user_id)
  2068. CheckArgType int "$1" "$2"
  2069. user_id=$2
  2070. shift 2
  2071. ;;
  2072. -s|--png_sticker)
  2073. CheckArgType file "$1" "$2"
  2074. png_sticker=$2
  2075. shift 2
  2076. ;;
  2077. --)
  2078. shift
  2079. break
  2080. ;;
  2081. esac
  2082. done
  2083. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  2084. [[ $png_sticker ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --png_sticker]"
  2085. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2086. ${user_id:+-F user_id="$user_id"} \
  2087. ${png_sticker:+-F png_sticker="$png_sticker"})
  2088. # Testa o retorno do método
  2089. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2090. # Status
  2091. return $?
  2092. }
  2093. ShellBot.setStickerPositionInSet()
  2094. {
  2095. local sticker position jq_obj
  2096. local param=$(getopt --name "$FUNCNAME" \
  2097. --options 's:p:' \
  2098. --longoptions 'sticker:,
  2099. position:' \
  2100. -- "$@")
  2101. eval set -- "$param"
  2102. while :
  2103. do
  2104. case $1 in
  2105. -s|--sticker)
  2106. sticker=$2
  2107. shift 2
  2108. ;;
  2109. -p|--position)
  2110. CheckArgType int "$1" "$2"
  2111. position=$2
  2112. shift 2
  2113. ;;
  2114. --)
  2115. shift
  2116. break
  2117. ;;
  2118. esac
  2119. done
  2120. [[ $sticker ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --sticker]"
  2121. [[ $position ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-p, --position]"
  2122. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2123. ${sticker:+-d sticker="$sticker"} \
  2124. ${position:+-d position="$position"})
  2125. # Testa o retorno do método
  2126. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2127. # Status
  2128. return $?
  2129. }
  2130. ShellBot.deleteStickerFromSet()
  2131. {
  2132. local sticker jq_obj
  2133. local param=$(getopt --name "$FUNCNAME" \
  2134. --options 's:' \
  2135. --longoptions 'sticker:' \
  2136. -- "$@")
  2137. eval set -- "$param"
  2138. while :
  2139. do
  2140. case $1 in
  2141. -s|--sticker)
  2142. sticker=$2
  2143. shift 2
  2144. ;;
  2145. --)
  2146. shift
  2147. break
  2148. ;;
  2149. esac
  2150. done
  2151. [[ $sticker ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --sticker]"
  2152. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} ${sticker:+-d sticker="$sticker"})
  2153. # Testa o retorno do método
  2154. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2155. # Status
  2156. return $?
  2157. }
  2158. ShellBot.stickerMaskPosition()
  2159. {
  2160. local point x_shift y_shift scale zoom
  2161. local param=$(getopt --name "$FUNCNAME" \
  2162. --options 'p:x:y:s:z:' \
  2163. --longoptions 'point:,
  2164. x_shift:,
  2165. y_shift:,
  2166. scale:,
  2167. zoom:' \
  2168. -- "$@")
  2169. eval set -- "$param"
  2170. while :
  2171. do
  2172. case $1 in
  2173. -p|--point)
  2174. point=$2
  2175. shift 2
  2176. ;;
  2177. -x|--x_shift)
  2178. CheckArgType float "$1" "$2"
  2179. x_shift=$2
  2180. shift 2
  2181. ;;
  2182. -y|--y_shift)
  2183. CheckArgType float "$1" "$2"
  2184. y_shift=$2
  2185. shift 2
  2186. ;;
  2187. -s|--scale)
  2188. CheckArgType float "$1" "$2"
  2189. scale=$2
  2190. shift 2
  2191. ;;
  2192. -z|--zoom)
  2193. CheckArgType float "$1" "$2"
  2194. zoom=$2
  2195. shift 2
  2196. ;;
  2197. --)
  2198. shift
  2199. break
  2200. ;;
  2201. esac
  2202. done
  2203. [[ $point ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-p, --point]"
  2204. [[ $x_shift ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-x, --x_shift]"
  2205. [[ $y_shift ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-y, --y_shift]"
  2206. [[ $scale ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --scale]"
  2207. [[ $zoom ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-z, --zoom]"
  2208. cat << _EOF
  2209. { "point": "$point", "x_shift": $x_shift, "y_shift": $y_shift, "scale": $scale, "zoom": $zoom }
  2210. _EOF
  2211. return 0
  2212. }
  2213. ShellBot.createNewStickerSet()
  2214. {
  2215. local user_id name title png_sticker emojis contains_masks mask_position jq_obj
  2216. local param=$(getopt --name "$FUNCNAME" \
  2217. --options 'u:n:t:s:e:c:m:' \
  2218. --longoptions 'user_id:,
  2219. name:,
  2220. title:,
  2221. png_sticker:,
  2222. emojis:,
  2223. contains_mask:,
  2224. mask_position:' \
  2225. -- "$@")
  2226. eval set -- "$param"
  2227. while :
  2228. do
  2229. case $1 in
  2230. -u|--user_id)
  2231. CheckArgType int "$1" "$2"
  2232. user_id=$2
  2233. shift 2
  2234. ;;
  2235. -n|--name)
  2236. name=$2
  2237. shift 2
  2238. ;;
  2239. -t|--title)
  2240. title=$2
  2241. shift 2
  2242. ;;
  2243. -s|--png_sticker)
  2244. CheckArgType file "$1" "$2"
  2245. png_sticker=$2
  2246. shift 2
  2247. ;;
  2248. -e|--emojis)
  2249. emojis=$2
  2250. shift 2
  2251. ;;
  2252. -c|--contains_masks)
  2253. CheckArgType bool "$1" "$2"
  2254. contains_masks=$2
  2255. shift 2
  2256. ;;
  2257. -m|--mask_position)
  2258. mask_position=$2
  2259. shift 2
  2260. ;;
  2261. --)
  2262. shift
  2263. break
  2264. ;;
  2265. esac
  2266. done
  2267. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  2268. [[ $name ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-n, --name]"
  2269. [[ $title ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --title]"
  2270. [[ $png_sticker ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --png_sticker]"
  2271. [[ $emojis ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-e, --emojis]"
  2272. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2273. ${user_id:+-F user_id="$user_id"} \
  2274. ${name:+-F name="$name"} \
  2275. ${title:+-F title="$title"} \
  2276. ${png_sticker:+-F png_sticker="$png_sticker"} \
  2277. ${emojis:+-F emojis="$emojis"} \
  2278. ${contains_masks:+-F contains_masks="$contains_masks"} \
  2279. ${mask_position:+-F mask_position="$mask_position"})
  2280. # Testa o retorno do método
  2281. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2282. # Status
  2283. return $?
  2284. }
  2285. ShellBot.addStickerToSet()
  2286. {
  2287. local user_id name png_sticker emojis mask_position jq_obj
  2288. local param=$(getopt --name "$FUNCNAME" \
  2289. --options 'u:n:s:e:m:' \
  2290. --longoptions 'user_id:,
  2291. name:,
  2292. png_sticker:,
  2293. emojis:,
  2294. mask_position:' \
  2295. -- "$@")
  2296. eval set -- "$param"
  2297. while :
  2298. do
  2299. case $1 in
  2300. -u|--user_id)
  2301. CheckArgType int "$1" "$2"
  2302. user_id=$2
  2303. shift 2
  2304. ;;
  2305. -n|--name)
  2306. name=$2
  2307. shift 2
  2308. ;;
  2309. -s|--png_sticker)
  2310. CheckArgType file "$1" "$2"
  2311. png_sticker=$2
  2312. shift 2
  2313. ;;
  2314. -e|--emojis)
  2315. emojis=$2
  2316. shift 2
  2317. ;;
  2318. -m|--mask_position)
  2319. mask_position=$2
  2320. shift 2
  2321. ;;
  2322. --)
  2323. shift
  2324. break
  2325. ;;
  2326. esac
  2327. done
  2328. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  2329. [[ $name ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-n, --name]"
  2330. [[ $png_sticker ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --png_sticker]"
  2331. [[ $emojis ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-e, --emojis]"
  2332. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2333. ${user_id:+-F user_id="$user_id"} \
  2334. ${name:+-F name="$name"} \
  2335. ${png_sticker:+-F png_sticker="$png_sticker"} \
  2336. ${emojis:+-F emojis="$emojis"} \
  2337. ${mask_position:+-F mask_position="$mask_position"})
  2338. # Testa o retorno do método
  2339. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2340. # Status
  2341. return $?
  2342. }
  2343. # Função para enviar arquivos de vídeo.
  2344. ShellBot.sendVideo()
  2345. {
  2346. # Variáveis locais
  2347. local chat_id video duration width height caption disable_notification
  2348. local parse_mode reply_to_message_id reply_markup jq_obj supports_streaming
  2349. # Lê os parâmetros da função
  2350. local param=$(getopt --name "$FUNCNAME" \
  2351. --options 'c:v:d:w:h:t:m:n:r:k:s:' \
  2352. --longoptions 'chat_id:,
  2353. video:,
  2354. duration:,
  2355. width:,
  2356. height:,
  2357. caption:,
  2358. parse_mode:,
  2359. disable_notification:,
  2360. reply_to_message_id:,
  2361. reply_markup:,
  2362. supports_streaming:' \
  2363. -- "$@")
  2364. # Define os parâmetros posicionais
  2365. eval set -- "$param"
  2366. while :
  2367. do
  2368. case $1 in
  2369. -c|--chat_id)
  2370. chat_id=$2
  2371. shift 2
  2372. ;;
  2373. -v|--video)
  2374. CheckArgType file "$1" "$2"
  2375. video=$2
  2376. shift 2
  2377. ;;
  2378. -d|--duration)
  2379. # Tipo: inteiro
  2380. CheckArgType int "$1" "$2"
  2381. duration=$2
  2382. shift 2
  2383. ;;
  2384. -w|--width)
  2385. # Tipo: inteiro
  2386. CheckArgType int "$1" "$2"
  2387. width=$2
  2388. shift 2
  2389. ;;
  2390. -h|--height)
  2391. # Tipo: inteiro
  2392. CheckArgType int "$1" "$2"
  2393. height=$2
  2394. shift 2
  2395. ;;
  2396. -t|--caption)
  2397. caption=$(echo -e "$2")
  2398. shift 2
  2399. ;;
  2400. -m|--parse_mode)
  2401. parse_mode=$2
  2402. shift 2
  2403. ;;
  2404. -n|--disable_notification)
  2405. # Tipo: boolean
  2406. CheckArgType bool "$1" "$2"
  2407. disable_notification=$2
  2408. shift 2
  2409. ;;
  2410. -r|--reply_to_message_id)
  2411. CheckArgType int "$1" "$2"
  2412. reply_to_message_id=$2
  2413. shift 2
  2414. ;;
  2415. -k|--reply_markup)
  2416. reply_markup=$2
  2417. shift 2
  2418. ;;
  2419. -s|--supports_streaming)
  2420. CheckArgType bool "$1" "$2"
  2421. supports_streaming=$2
  2422. shift 2
  2423. ;;
  2424. --)
  2425. shift
  2426. break
  2427. ;;
  2428. esac
  2429. done
  2430. # Parâmetros obrigatórios.
  2431. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2432. [[ $video ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-v, --video]"
  2433. # Chama o método
  2434. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2435. ${chat_id:+-F chat_id="$chat_id"} \
  2436. ${video:+-F video="$video"} \
  2437. ${duration:+-F duration="$duration"} \
  2438. ${width:+-F width="$width"} \
  2439. ${height:+-F height="$height"} \
  2440. ${caption:+-F caption="$caption"} \
  2441. ${parse_mode:+-F parse_mode="$parse_mode"} \
  2442. ${disable_notification:+-F disable_notification="$disable_notification"} \
  2443. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  2444. ${reply_markup:+-F reply_markup="$reply_markup"} \
  2445. ${supports_streaming:+-F supports_streaming="$supports_streaming"})
  2446. # Testa o retorno do método
  2447. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2448. # Status
  2449. return $?
  2450. }
  2451. # Função para enviar audio.
  2452. ShellBot.sendVoice()
  2453. {
  2454. # Variáveis locais
  2455. local chat_id voice caption duration disable_notification
  2456. local parse_mode reply_to_message_id reply_markup jq_obj
  2457. # Lê os parâmetros da função
  2458. local param=$(getopt --name "$FUNCNAME" \
  2459. --options 'c:v:t:m:d:n:r:k:' \
  2460. --longoptions 'chat_id:,
  2461. voice:,
  2462. caption:,
  2463. parse_mode:,
  2464. duration:,
  2465. disable_notification:,
  2466. reply_to_message_id:,
  2467. reply_markup:' \
  2468. -- "$@")
  2469. # Define os parâmetros posicionais
  2470. eval set -- "$param"
  2471. while :
  2472. do
  2473. case $1 in
  2474. -c|--chat_id)
  2475. chat_id=$2
  2476. shift 2
  2477. ;;
  2478. -v|--voice)
  2479. CheckArgType file "$1" "$2"
  2480. voice=$2
  2481. shift 2
  2482. ;;
  2483. -t|--caption)
  2484. caption=$(echo -e "$2")
  2485. shift 2
  2486. ;;
  2487. -m|--parse_mode)
  2488. parse_mode=$2
  2489. shift 2
  2490. ;;
  2491. -d|--duration)
  2492. # Tipo: inteiro
  2493. CheckArgType int "$1" "$2"
  2494. duration=$2
  2495. shift 2
  2496. ;;
  2497. -n|--disable_notification)
  2498. # Tipo: boolean
  2499. CheckArgType bool "$1" "$2"
  2500. disable_notification=$2
  2501. shift 2
  2502. ;;
  2503. -r|--reply_to_message_id)
  2504. # Tipo: inteiro
  2505. CheckArgType int "$1" "$2"
  2506. reply_to_message_id=$2
  2507. shift 2
  2508. ;;
  2509. -k|--reply_markup)
  2510. reply_markup=$2
  2511. shift 2
  2512. ;;
  2513. --)
  2514. shift
  2515. break
  2516. ;;
  2517. esac
  2518. done
  2519. # Parâmetros obrigatórios.
  2520. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2521. [[ $voice ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-v, --voice]"
  2522. # Chama o método
  2523. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2524. ${chat_id:+-F chat_id="$chat_id"} \
  2525. ${voice:+-F voice="$voice"} \
  2526. ${caption:+-F caption="$caption"} \
  2527. ${parse_mode:+-F parse_mode="$parse_mode"} \
  2528. ${duration:+-F duration="$duration"} \
  2529. ${disable_notification:+-F disable_notification="$disable_notification"} \
  2530. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  2531. ${reply_markup:+-F reply_markup="$reply_markup"})
  2532. # Testa o retorno do método
  2533. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2534. # Status
  2535. return $?
  2536. }
  2537. # Função utilizada para enviar uma localidade utilizando coordenadas de latitude e longitude.
  2538. ShellBot.sendLocation()
  2539. {
  2540. # Variáveis locais
  2541. local chat_id latitude longitude live_period
  2542. local disable_notification reply_to_message_id reply_markup jq_obj
  2543. # Lê os parâmetros da função
  2544. local param=$(getopt --name "$FUNCNAME" \
  2545. --options 'c:l:g:p:n:r:k:' \
  2546. --longoptions 'chat_id:,
  2547. latitude:,
  2548. longitude:,
  2549. live_period:,
  2550. disable_notification:,
  2551. reply_to_message_id:,
  2552. reply_markup:' \
  2553. -- "$@")
  2554. # Define os parâmetros posicionais
  2555. eval set -- "$param"
  2556. while :
  2557. do
  2558. case $1 in
  2559. -c|--chat_id)
  2560. chat_id=$2
  2561. shift 2
  2562. ;;
  2563. -l|--latitude)
  2564. # Tipo: float
  2565. CheckArgType float "$1" "$2"
  2566. latitude=$2
  2567. shift 2
  2568. ;;
  2569. -g|--longitude)
  2570. # Tipo: float
  2571. CheckArgType float "$1" "$2"
  2572. longitude=$2
  2573. shift 2
  2574. ;;
  2575. -p|--live_period)
  2576. CheckArgType int "$1" "$2"
  2577. live_period=$2
  2578. shift 2
  2579. ;;
  2580. -n|--disable_notification)
  2581. # Tipo: boolean
  2582. CheckArgType bool "$1" "$2"
  2583. disable_notification=$2
  2584. shift 2
  2585. ;;
  2586. -r|--reply_to_message_id)
  2587. # Tipo: inteiro
  2588. CheckArgType int "$1" "$2"
  2589. reply_to_message_id=$2
  2590. shift 2
  2591. ;;
  2592. -k|--reply_markup)
  2593. reply_markup=$2
  2594. shift 2
  2595. ;;
  2596. --)
  2597. shift
  2598. break
  2599. ;;
  2600. esac
  2601. done
  2602. # Parâmetros obrigatórios
  2603. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2604. [[ $latitude ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-l, --latitude]"
  2605. [[ $longitude ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-g, --longitude]"
  2606. # Chama o método
  2607. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2608. ${chat_id:+-F chat_id="$chat_id"} \
  2609. ${latitude:+-F latitude="$latitude"} \
  2610. ${longitude:+-F longitude="$longitude"} \
  2611. ${live_period:+-F live_period="$live_period"} \
  2612. ${disable_notification:+-F disable_notification="$disable_notification"} \
  2613. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  2614. ${reply_markup:+-F reply_markup="$reply_markup"})
  2615. # Testa o retorno do método
  2616. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2617. return $?
  2618. }
  2619. # Função utlizada para enviar detalhes de um local.
  2620. ShellBot.sendVenue()
  2621. {
  2622. # Variáveis locais
  2623. local chat_id latitude longitude title address foursquare_id disable_notification reply_to_message_id reply_markup jq_obj
  2624. # Lê os parâmetros da função
  2625. local param=$(getopt --name "$FUNCNAME" \
  2626. --options 'c:l:g:i:a:f:n:r:k:' \
  2627. --longoptions 'chat_id:,
  2628. latitude:,
  2629. longitude:,
  2630. title:,
  2631. address:,
  2632. foursquare_id:,
  2633. disable_notification:,
  2634. reply_to_message_id:,
  2635. reply_markup:' \
  2636. -- "$@")
  2637. # Define os parâmetros posicionais
  2638. eval set -- "$param"
  2639. while :
  2640. do
  2641. case $1 in
  2642. -c|--chat_id)
  2643. chat_id=$2
  2644. shift 2
  2645. ;;
  2646. -l|--latitude)
  2647. # Tipo: float
  2648. CheckArgType float "$1" "$2"
  2649. latitude=$2
  2650. shift 2
  2651. ;;
  2652. -g|--longitude)
  2653. # Tipo: float
  2654. CheckArgType float "$1" "$2"
  2655. longitude=$2
  2656. shift 2
  2657. ;;
  2658. -i|--title)
  2659. title=$2
  2660. shift 2
  2661. ;;
  2662. -a|--address)
  2663. address=$2
  2664. shift 2
  2665. ;;
  2666. -f|--foursquare_id)
  2667. foursquare_id=$2
  2668. shift 2
  2669. ;;
  2670. -n|--disable_notification)
  2671. # Tipo: boolean
  2672. CheckArgType bool "$1" "$2"
  2673. disable_notification=$2
  2674. shift 2
  2675. ;;
  2676. -r|--reply_to_message_id)
  2677. # Tipo: inteiro
  2678. CheckArgType int "$1" "$2"
  2679. reply_to_message_id=$2
  2680. shift 2
  2681. ;;
  2682. -k|--reply_markup)
  2683. reply_markup=$2
  2684. shift 2
  2685. ;;
  2686. --)
  2687. shift
  2688. break
  2689. ;;
  2690. esac
  2691. done
  2692. # Parâmetros obrigatórios.
  2693. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2694. [[ $latitude ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-l, --latitude]"
  2695. [[ $longitude ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-g, --longitude]"
  2696. [[ $title ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-i, --title]"
  2697. [[ $address ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-a, --address]"
  2698. # Chama o método
  2699. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2700. ${chat_id:+-F chat_id="$chat_id"} \
  2701. ${latitude:+-F latitude="$latitude"} \
  2702. ${longitude:+-F longitude="$longitude"} \
  2703. ${title:+-F title="$title"} \
  2704. ${address:+-F address="$address"} \
  2705. ${foursquare_id:+-F foursquare_id="$foursquare_id"} \
  2706. ${disable_notification:+-F disable_notification="$disable_notification"} \
  2707. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  2708. ${reply_markup:+-F reply_markup="$reply_markup"})
  2709. # Testa o retorno do método
  2710. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2711. # Status
  2712. return $?
  2713. }
  2714. # Utilize essa função para enviar um contato + numero
  2715. ShellBot.sendContact()
  2716. {
  2717. # Variáveis locais
  2718. local chat_id phone_number first_name last_name disable_notification reply_to_message_id reply_markup jq_obj
  2719. # Lê os parâmetros da função
  2720. local param=$(getopt --name "$FUNCNAME" \
  2721. --options 'c:p:f:l:n:r:k:' \
  2722. --longoptions 'chat_id:,
  2723. phone_number:,
  2724. first_name:,
  2725. last_name:,
  2726. disable_notification:,
  2727. reply_to_message_id:,
  2728. reply_markup:' \
  2729. -- "$@")
  2730. # Define os parâmetros posicionais
  2731. eval set -- "$param"
  2732. while :
  2733. do
  2734. case $1 in
  2735. -c|--chat_id)
  2736. chat_id=$2
  2737. shift 2
  2738. ;;
  2739. -p|--phone_number)
  2740. phone_number=$2
  2741. shift 2
  2742. ;;
  2743. -f|--first_name)
  2744. first_name=$2
  2745. shift 2
  2746. ;;
  2747. -l|--last_name)
  2748. last_name=$2
  2749. shift 2
  2750. ;;
  2751. -n|--disable_notification)
  2752. # Tipo: boolean
  2753. CheckArgType bool "$1" "$2"
  2754. disable_notification=$2
  2755. shift 2
  2756. ;;
  2757. -r|--reply_to_message_id)
  2758. # Tipo: inteiro
  2759. CheckArgType int "$1" "$2"
  2760. reply_to_message_id=$2
  2761. shift 2
  2762. ;;
  2763. -k|--reply_markup)
  2764. reply_markup=$2
  2765. shift 2
  2766. ;;
  2767. --)
  2768. shift
  2769. break
  2770. ;;
  2771. esac
  2772. done
  2773. # Parâmetros obrigatórios.
  2774. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2775. [[ $phone_number ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-p, --phone_number]"
  2776. [[ $first_name ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-f, --first_name]"
  2777. # Chama o método
  2778. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2779. ${chat_id:+-F chat_id="$chat_id"} \
  2780. ${phone_number:+-F phone_number="$phone_number"} \
  2781. ${first_name:+-F first_name="$first_name"} \
  2782. ${last_name:+-F last_name="$last_name"} \
  2783. ${disable_notification:+-F disable_notification="$disable_notification"} \
  2784. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  2785. ${reply_markup:+-F reply_markup="$reply_markup"})
  2786. # Testa o retorno do método
  2787. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2788. # Status
  2789. return $?
  2790. }
  2791. # Envia uma ação para bot.
  2792. ShellBot.sendChatAction()
  2793. {
  2794. # Variáveis locais
  2795. local chat_id action jq_obj
  2796. # Lê os parâmetros da função
  2797. local param=$(getopt --name "$FUNCNAME" \
  2798. --options 'c:a:' \
  2799. --longoptions 'chat_id:,
  2800. action:' \
  2801. -- "$@")
  2802. # Define os parâmetros posicionais
  2803. eval set -- "$param"
  2804. while :
  2805. do
  2806. case $1 in
  2807. -c|--chat_id)
  2808. chat_id=$2
  2809. shift 2
  2810. ;;
  2811. -a|--action)
  2812. action=$2
  2813. shift 2
  2814. ;;
  2815. --)
  2816. shift
  2817. break
  2818. ;;
  2819. esac
  2820. done
  2821. # Parâmetros obrigatórios.
  2822. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2823. [[ $action ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-a, --action]"
  2824. # Chama o método
  2825. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2826. ${chat_id:+-d chat_id="$chat_id"} \
  2827. ${action:+-d action="$action"})
  2828. # Testa o retorno do método
  2829. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2830. # Status
  2831. return $?
  2832. }
  2833. # Utilize essa função para obter as fotos de um determinado usuário.
  2834. ShellBot.getUserProfilePhotos()
  2835. {
  2836. # Variáveis locais
  2837. local user_id offset limit ind last index max item total jq_obj
  2838. # Lê os parâmetros da função
  2839. local param=$(getopt --name "$FUNCNAME" \
  2840. --options 'u:o:l:' \
  2841. --longoptions 'user_id:,
  2842. offset:,
  2843. limit:' \
  2844. -- "$@")
  2845. # Define os parâmetros posicionais
  2846. eval set -- "$param"
  2847. while :
  2848. do
  2849. case $1 in
  2850. -u|--user_id)
  2851. CheckArgType int "$1" "$2"
  2852. user_id=$2
  2853. shift 2
  2854. ;;
  2855. -o|--offset)
  2856. CheckArgType int "$1" "$2"
  2857. offset=$2
  2858. shift 2
  2859. ;;
  2860. -l|--limit)
  2861. CheckArgType int "$1" "$2"
  2862. limit=$2
  2863. shift 2
  2864. ;;
  2865. --)
  2866. shift
  2867. break
  2868. ;;
  2869. esac
  2870. done
  2871. # Parâmetros obrigatórios.
  2872. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  2873. # Chama o método
  2874. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} \
  2875. ${user_id:+-d user_id="$user_id"} \
  2876. ${offset:+-d offset="$offset"} \
  2877. ${limit:+-d limit="$limit"})
  2878. # Verifica se ocorreu erros durante a chamada do método
  2879. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2880. # Status
  2881. return $?
  2882. }
  2883. # Função para listar informações do arquivo especificado.
  2884. ShellBot.getFile()
  2885. {
  2886. # Variáveis locais
  2887. local file_id jq_obj
  2888. # Lê os parâmetros da função
  2889. local param=$(getopt --name "$FUNCNAME" \
  2890. --options 'f:' \
  2891. --longoptions 'file_id:' \
  2892. -- "$@")
  2893. # Define os parâmetros posicionais
  2894. eval set -- "$param"
  2895. while :
  2896. do
  2897. case $1 in
  2898. -f|--file_id)
  2899. file_id=$2
  2900. shift 2
  2901. ;;
  2902. --)
  2903. shift
  2904. break
  2905. ;;
  2906. esac
  2907. done
  2908. # Parâmetros obrigatórios.
  2909. [[ $file_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-f, --file_id]"
  2910. # Chama o método.
  2911. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} ${file_id:+-d file_id="$file_id"})
  2912. # Testa o retorno do método.
  2913. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2914. # Status
  2915. return $?
  2916. }
  2917. # Essa função kicka o usuário do chat ou canal. (somente administradores)
  2918. ShellBot.kickChatMember()
  2919. {
  2920. # Variáveis locais
  2921. local chat_id user_id until_date jq_obj
  2922. # Lê os parâmetros da função
  2923. local param=$(getopt --name "$FUNCNAME" \
  2924. --options 'c:u:d:' \
  2925. --longoptions 'chat_id:,
  2926. user_id:,
  2927. until_date:' \
  2928. -- "$@")
  2929. # Define os parâmetros posicionais
  2930. eval set -- "$param"
  2931. # Trata os parâmetros
  2932. while :
  2933. do
  2934. case $1 in
  2935. -c|--chat_id)
  2936. chat_id=$2
  2937. shift 2
  2938. ;;
  2939. -u|--user_id)
  2940. CheckArgType int "$1" "$2"
  2941. user_id=$2
  2942. shift 2
  2943. ;;
  2944. -d|--until_date)
  2945. CheckArgType int "$1" "$2"
  2946. until_date=$2
  2947. shift 2
  2948. ;;
  2949. --)
  2950. shift
  2951. break
  2952. ;;
  2953. esac
  2954. done
  2955. # Parametros obrigatórios.
  2956. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2957. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  2958. # Chama o método
  2959. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  2960. ${chat_id:+-d chat_id="$chat_id"} \
  2961. ${user_id:+-d user_id="$user_id"} \
  2962. ${until_date:+-d until_date="$until_date"})
  2963. # Verifica se ocorreu erros durante a chamada do método
  2964. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2965. # Status
  2966. return $?
  2967. }
  2968. # Utilize essa função para remove o bot do grupo ou canal.
  2969. ShellBot.leaveChat()
  2970. {
  2971. # Variáveis locais
  2972. local chat_id jq_obj
  2973. # Lê os parâmetros da função
  2974. local param=$(getopt --name "$FUNCNAME" \
  2975. --options 'c:' \
  2976. --longoptions 'chat_id:' \
  2977. -- "$@")
  2978. # Define os parâmetros posicionais
  2979. eval set -- "$param"
  2980. while :
  2981. do
  2982. case $1 in
  2983. -c|--chat_id)
  2984. chat_id=$2
  2985. shift 2
  2986. ;;
  2987. --)
  2988. shift
  2989. break
  2990. ;;
  2991. esac
  2992. done
  2993. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  2994. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  2995. # Verifica se ocorreu erros durante a chamada do método
  2996. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  2997. return $?
  2998. }
  2999. ShellBot.unbanChatMember()
  3000. {
  3001. local chat_id user_id jq_obj
  3002. # Lê os parâmetros da função
  3003. local param=$(getopt --name "$FUNCNAME" \
  3004. --options 'c:u:' \
  3005. --longoptions 'chat_id:,
  3006. user_id:' \
  3007. -- "$@")
  3008. # Define os parâmetros posicionais
  3009. eval set -- "$param"
  3010. while :
  3011. do
  3012. case $1 in
  3013. -c|--chat_id)
  3014. chat_id=$2
  3015. shift 2
  3016. ;;
  3017. -u|--user_id)
  3018. CheckArgType int "$1" "$2"
  3019. user_id=$2
  3020. shift 2
  3021. ;;
  3022. --)
  3023. shift
  3024. break
  3025. ;;
  3026. esac
  3027. done
  3028. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3029. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  3030. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3031. ${chat_id:+-d chat_id="$chat_id"} \
  3032. ${user_id:+-d user_id="$user_id"})
  3033. # Verifica se ocorreu erros durante a chamada do método
  3034. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3035. return $?
  3036. }
  3037. ShellBot.getChat()
  3038. {
  3039. # Variáveis locais
  3040. local chat_id jq_obj
  3041. # Lê os parâmetros da função
  3042. local param=$(getopt --name "$FUNCNAME" \
  3043. --options 'c:' \
  3044. --longoptions 'chat_id:' \
  3045. -- "$@")
  3046. # Define os parâmetros posicionais
  3047. eval set -- "$param"
  3048. while :
  3049. do
  3050. case $1 in
  3051. -c|--chat_id)
  3052. chat_id=$2
  3053. shift 2
  3054. ;;
  3055. --)
  3056. shift
  3057. break
  3058. ;;
  3059. esac
  3060. done
  3061. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3062. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  3063. # Verifica se ocorreu erros durante a chamada do método
  3064. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3065. # Status
  3066. return $?
  3067. }
  3068. ShellBot.getChatAdministrators()
  3069. {
  3070. local chat_id total key index jq_obj
  3071. # Lê os parâmetros da função
  3072. local param=$(getopt --name "$FUNCNAME" \
  3073. --options 'c:' \
  3074. --longoptions 'chat_id:' \
  3075. -- "$@")
  3076. # Define os parâmetros posicionais
  3077. eval set -- "$param"
  3078. while :
  3079. do
  3080. case $1 in
  3081. -c|--chat_id)
  3082. chat_id=$2
  3083. shift 2
  3084. ;;
  3085. --)
  3086. shift
  3087. break
  3088. ;;
  3089. esac
  3090. done
  3091. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3092. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  3093. # Verifica se ocorreu erros durante a chamada do método
  3094. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3095. # Status
  3096. return $?
  3097. }
  3098. ShellBot.getChatMembersCount()
  3099. {
  3100. local chat_id jq_obj
  3101. # Lê os parâmetros da função
  3102. local param=$(getopt --name "$FUNCNAME" \
  3103. --options 'c:' \
  3104. --longoptions 'chat_id:' \
  3105. -- "$@")
  3106. # Define os parâmetros posicionais
  3107. eval set -- "$param"
  3108. while :
  3109. do
  3110. case $1 in
  3111. -c|--chat_id)
  3112. chat_id=$2
  3113. shift 2
  3114. ;;
  3115. --)
  3116. shift
  3117. break
  3118. ;;
  3119. esac
  3120. done
  3121. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3122. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  3123. # Verifica se ocorreu erros durante a chamada do método
  3124. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3125. return $?
  3126. }
  3127. ShellBot.getChatMember()
  3128. {
  3129. # Variáveis locais
  3130. local chat_id user_id jq_obj
  3131. # Lê os parâmetros da função
  3132. local param=$(getopt --name "$FUNCNAME" \
  3133. --options 'c:u:' \
  3134. --longoptions 'chat_id:,
  3135. user_id:' \
  3136. -- "$@")
  3137. # Define os parâmetros posicionais
  3138. eval set -- "$param"
  3139. while :
  3140. do
  3141. case $1 in
  3142. -c|--chat_id)
  3143. chat_id=$2
  3144. shift 2
  3145. ;;
  3146. -u|--user_id)
  3147. CheckArgType int "$1" "$2"
  3148. user_id=$2
  3149. shift 2
  3150. ;;
  3151. --)
  3152. shift
  3153. break
  3154. ;;
  3155. esac
  3156. done
  3157. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3158. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  3159. jq_obj=$(curl $_CURL_OPT_ GET $_API_TELEGRAM_/${FUNCNAME#*.} \
  3160. ${chat_id:+-d chat_id="$chat_id"} \
  3161. ${user_id:+-d user_id="$user_id"})
  3162. # Verifica se ocorreu erros durante a chamada do método
  3163. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3164. return $?
  3165. }
  3166. ShellBot.editMessageText()
  3167. {
  3168. local chat_id message_id inline_message_id text parse_mode disable_web_page_preview reply_markup jq_obj
  3169. local param=$(getopt --name "$FUNCNAME" \
  3170. --options 'c:m:i:t:p:w:r:' \
  3171. --longoptions 'chat_id:,
  3172. message_id:,
  3173. inline_message_id:,
  3174. text:,
  3175. parse_mode:,
  3176. disable_web_page_preview:,
  3177. reply_markup:' \
  3178. -- "$@")
  3179. eval set -- "$param"
  3180. while :
  3181. do
  3182. case $1 in
  3183. -c|--chat_id)
  3184. chat_id=$2
  3185. shift 2
  3186. ;;
  3187. -m|--message_id)
  3188. CheckArgType int "$1" "$2"
  3189. message_id=$2
  3190. shift 2
  3191. ;;
  3192. -i|--inline_message_id)
  3193. CheckArgType int "$1" "$2"
  3194. inline_message_id=$2
  3195. shift 2
  3196. ;;
  3197. -t|--text)
  3198. text=$(echo -e "$2")
  3199. shift 2
  3200. ;;
  3201. -p|--parse_mode)
  3202. parse_mode=$2
  3203. shift 2
  3204. ;;
  3205. -w|--disable_web_page_preview)
  3206. CheckArgType bool "$1" "$2"
  3207. disable_web_page_preview=$2
  3208. shift 2
  3209. ;;
  3210. -r|--reply_markup)
  3211. reply_markup=$2
  3212. shift 2
  3213. ;;
  3214. --)
  3215. shift
  3216. break
  3217. ;;
  3218. esac
  3219. done
  3220. [[ $text ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --text]"
  3221. [[ $inline_message_id ]] && unset chat_id message_id || {
  3222. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3223. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  3224. }
  3225. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3226. ${chat_id:+-d chat_id="$chat_id"} \
  3227. ${message_id:+-d message_id="$message_id"} \
  3228. ${inline_message_id:+-d inline_message_id="$inline_message_id"} \
  3229. ${text:+-d text="$text"} \
  3230. ${parse_mode:+-d parse_mode="$parse_mode"} \
  3231. ${disable_web_page_preview:+-d disable_web_page_preview="$disable_web_page_preview"} \
  3232. ${reply_markup:+-d reply_markup="$reply_markup"})
  3233. # Verifica se ocorreu erros durante a chamada do método
  3234. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3235. return $?
  3236. }
  3237. ShellBot.editMessageCaption()
  3238. {
  3239. local chat_id message_id inline_message_id
  3240. local parse_mode caption reply_markup jq_obj
  3241. local param=$(getopt --name "$FUNCNAME" \
  3242. --options 'c:m:i:t:p:r:' \
  3243. --longoptions 'chat_id:,
  3244. message_id:,
  3245. inline_message_id:,
  3246. caption:,
  3247. parse_mode:,
  3248. reply_markup:' \
  3249. -- "$@")
  3250. eval set -- "$param"
  3251. while :
  3252. do
  3253. case $1 in
  3254. -c|--chat_id)
  3255. chat_id=$2
  3256. shift 2
  3257. ;;
  3258. -m|--message_id)
  3259. CheckArgType int "$1" "$2"
  3260. message_id=$2
  3261. shift 2
  3262. ;;
  3263. -i|--inline_message_id)
  3264. CheckArgType int "$1" "$2"
  3265. inline_message_id=$2
  3266. shift 2
  3267. ;;
  3268. -t|--caption)
  3269. caption=$(echo -e "$2")
  3270. shift 2
  3271. ;;
  3272. -p|--parse_mode)
  3273. parse_mode=$2
  3274. shift 2
  3275. ;;
  3276. -r|--reply_markup)
  3277. reply_markup=$2
  3278. shift 2
  3279. ;;
  3280. --)
  3281. shift
  3282. break
  3283. ;;
  3284. esac
  3285. done
  3286. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3287. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  3288. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3289. ${chat_id:+-d chat_id="$chat_id"} \
  3290. ${message_id:+-d message_id="$message_id"} \
  3291. ${inline_message_id:+-d inline_message_id="$inline_message_id"} \
  3292. ${caption:+-d caption="$caption"} \
  3293. ${parse_mode:+-d parse_mode="$parse_mode"} \
  3294. ${reply_markup:+-d reply_markup="$reply_markup"})
  3295. # Verifica se ocorreu erros durante a chamada do método
  3296. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3297. return $?
  3298. }
  3299. ShellBot.editMessageReplyMarkup()
  3300. {
  3301. local chat_id message_id inline_message_id reply_markup jq_obj
  3302. local param=$(getopt --name "$FUNCNAME" \
  3303. --options 'c:m:i:r:' \
  3304. --longoptions 'chat_id:,
  3305. message_id:,
  3306. inline_message_id:,
  3307. reply_markup:' \
  3308. -- "$@")
  3309. eval set -- "$param"
  3310. while :
  3311. do
  3312. case $1 in
  3313. -c|--chat_id)
  3314. chat_id=$2
  3315. shift 2
  3316. ;;
  3317. -m|--message_id)
  3318. CheckArgType int "$1" "$2"
  3319. message_id=$2
  3320. shift 2
  3321. ;;
  3322. -i|--inline_message_id)
  3323. CheckArgType int "$1" "$2"
  3324. inline_message_id=$2
  3325. shift 2
  3326. ;;
  3327. -r|--reply_markup)
  3328. reply_markup=$2
  3329. shift 2
  3330. ;;
  3331. --)
  3332. shift
  3333. break
  3334. ;;
  3335. esac
  3336. done
  3337. [[ $inline_message_id ]] && unset chat_id message_id || {
  3338. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3339. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  3340. }
  3341. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3342. ${chat_id:+-d chat_id="$chat_id"} \
  3343. ${message_id:+-d message_id="$message_id"} \
  3344. ${inline_message_id:+-d inline_message_id="$inline_message_id"} \
  3345. ${reply_markup:+-d reply_markup="$reply_markup"})
  3346. # Verifica se ocorreu erros durante a chamada do método
  3347. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3348. return $?
  3349. }
  3350. ShellBot.deleteMessage()
  3351. {
  3352. local chat_id message_id jq_obj
  3353. local param=$(getopt --name "$FUNCNAME" \
  3354. --options 'c:m:' \
  3355. --longoptions 'chat_id:,
  3356. message_id:' \
  3357. -- "$@")
  3358. eval set -- "$param"
  3359. while :
  3360. do
  3361. case $1 in
  3362. -c|--chat_id)
  3363. chat_id=$2
  3364. shift 2
  3365. ;;
  3366. -m|--message_id)
  3367. CheckArgType int "$1" "$2"
  3368. message_id=$2
  3369. shift 2
  3370. ;;
  3371. --)
  3372. shift
  3373. break
  3374. ;;
  3375. esac
  3376. done
  3377. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3378. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  3379. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3380. ${chat_id:+-d chat_id="$chat_id"} \
  3381. ${message_id:+-d message_id="$message_id"})
  3382. # Verifica se ocorreu erros durante a chamada do método
  3383. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3384. return $?
  3385. }
  3386. ShellBot.downloadFile()
  3387. {
  3388. local file_path dir ext file jq_obj
  3389. local uri="https://api.telegram.org/file/bot$_TOKEN_"
  3390. local param=$(getopt --name "$FUNCNAME" \
  3391. --options 'f:d:' \
  3392. --longoptions 'file_path:,
  3393. dir:' \
  3394. -- "$@")
  3395. eval set -- "$param"
  3396. while :
  3397. do
  3398. case $1 in
  3399. -f|--file_path)
  3400. [[ $2 =~ \.[^.]+$ ]]
  3401. ext=$BASH_REMATCH
  3402. file_path=$2
  3403. shift 2
  3404. ;;
  3405. -d|--dir)
  3406. [[ -d $2 ]] || MessageError API "$_ERR_DIR_NOT_FOUND_" "$1" "$2"
  3407. [[ -w $2 ]] || MessageError API "$_ERR_DIR_WRITE_DENIED_" "$1" "$2"
  3408. dir=${2%/}
  3409. shift 2
  3410. ;;
  3411. --)
  3412. shift
  3413. break
  3414. ;;
  3415. esac
  3416. done
  3417. [[ $file_path ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-f, --file_path]"
  3418. [[ $dir ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-d, --dir]"
  3419. # Gera o nome do arquivo anexando o horário de criação.
  3420. file=file$(date +%d%m%Y%H%M%S%N)${ext:-.dat}
  3421. # Executa o download da uri especificada e retorna um objeto json
  3422. # com as informações do processo. Se tiver sucesso o diretório de
  3423. # destino é retornado, caso contrário uma mensagem de erro é apresentada.
  3424. if wget -qO "$dir/$file" "$uri/$file_path"; then
  3425. # Sucesso
  3426. printf -v jq_obj '{"ok":true,"result":{"file_path":"%s"}}' "$dir/$file"
  3427. else
  3428. # Falha
  3429. printf -v jq_obj '{"ok":false,"error_code":404,"description":"Bad Request: file not found"}'
  3430. rm -f "$dir/$file" 2>/dev/null # Remove arquivo inválido.
  3431. fi
  3432. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3433. return $?
  3434. }
  3435. ShellBot.editMessageLiveLocation()
  3436. {
  3437. local chat_id message_id inline_message_id
  3438. local latitude longitude reply_markup jq_obj
  3439. local param=$(getopt --name "$FUNCNAME" \
  3440. --options 'c:m:i:l:g:r:' \
  3441. --longoptions 'chat_id:,
  3442. message_id:,
  3443. inline_message_id:,
  3444. latitude:,
  3445. longitude:,
  3446. reply_markup:' \
  3447. -- "$@")
  3448. eval set -- "$param"
  3449. while :
  3450. do
  3451. case $1 in
  3452. -c|--chat_id)
  3453. chat_id=$2
  3454. shift 2
  3455. ;;
  3456. -m|--message_id)
  3457. CheckArgType int "$1" "$2"
  3458. message_id=$2
  3459. shift 2
  3460. ;;
  3461. -i|--inline_message_id)
  3462. CheckArgType int "$1" "$2"
  3463. inline_message_id=$2
  3464. shift 2
  3465. ;;
  3466. -l|--latitude)
  3467. # Tipo: float
  3468. CheckArgType float "$1" "$2"
  3469. latitude=$2
  3470. shift 2
  3471. ;;
  3472. -g|--longitude)
  3473. # Tipo: float
  3474. CheckArgType float "$1" "$2"
  3475. longitude=$2
  3476. shift 2
  3477. ;;
  3478. -r|--reply_markup)
  3479. reply_markup=$2
  3480. shift 2
  3481. ;;
  3482. --)
  3483. shift
  3484. break
  3485. ;;
  3486. esac
  3487. done
  3488. [[ $inline_message_id ]] && unset chat_id message_id || {
  3489. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3490. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  3491. }
  3492. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3493. ${chat_id:+-d chat_id="$chat_id"} \
  3494. ${message_id:+-d message_id="$message_id"} \
  3495. ${inline_message_id:+-d inline_message_id="$inline_message_id"} \
  3496. ${latitude:+-d latitude="$latitude"} \
  3497. ${longitude:+-d longitude="$longitude"} \
  3498. ${reply_markup:+-d reply_markup="$reply_markup"})
  3499. # Testa o retorno do método
  3500. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3501. return $?
  3502. }
  3503. ShellBot.stopMessageLiveLocation()
  3504. {
  3505. local chat_id message_id inline_message_id reply_markup jq_obj
  3506. local param=$(getopt --name "$FUNCNAME" \
  3507. --options 'c:m:i:r:' \
  3508. --longoptions 'chat_id:,
  3509. message_id:,
  3510. inline_message_id:,
  3511. reply_markup:' \
  3512. -- "$@")
  3513. eval set -- "$param"
  3514. while :
  3515. do
  3516. case $1 in
  3517. -c|--chat_id)
  3518. chat_id=$2
  3519. shift 2
  3520. ;;
  3521. -m|--message_id)
  3522. CheckArgType int "$1" "$2"
  3523. message_id=$2
  3524. shift 2
  3525. ;;
  3526. -i|--inline_message_id)
  3527. CheckArgType int "$1" "$2"
  3528. inline_message_id=$2
  3529. shift 2
  3530. ;;
  3531. -r|--reply_markup)
  3532. reply_markup=$2
  3533. shift 2
  3534. ;;
  3535. --)
  3536. shift
  3537. break
  3538. ;;
  3539. esac
  3540. done
  3541. [[ $inline_message_id ]] && unset chat_id message_id || {
  3542. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3543. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --message_id]"
  3544. }
  3545. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3546. ${chat_id:+-d chat_id="$chat_id"} \
  3547. ${message_id:+-d message_id="$message_id"} \
  3548. ${inline_message_id:+-d inline_message_id="$inline_message_id"} \
  3549. ${reply_markup:+-d reply_markup="$reply_markup"})
  3550. # Testa o retorno do método
  3551. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3552. return $?
  3553. }
  3554. ShellBot.setChatStickerSet()
  3555. {
  3556. local chat_id sticker_set_name jq_obj
  3557. local param=$(getopt --name "$FUNCNAME" \
  3558. --options 'c:s:' \
  3559. --longoptions 'chat_id:,
  3560. sticker_set_name:' \
  3561. -- "$@")
  3562. eval set -- "$param"
  3563. while :
  3564. do
  3565. case $1 in
  3566. -c|--chat_id)
  3567. chat_id=$2
  3568. shift 2
  3569. ;;
  3570. -s|--sticker_set_name)
  3571. sticker_set_name=$2
  3572. shift 2
  3573. ;;
  3574. --)
  3575. shift
  3576. break
  3577. ;;
  3578. esac
  3579. done
  3580. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3581. [[ $sticker_set_name ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --sticker_set_name]"
  3582. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3583. ${chat_id:+-d chat_id="$chat_id"} \
  3584. ${sticker_set_name:+-d sticker_set_name="$sticker_set_name"})
  3585. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3586. return $?
  3587. }
  3588. ShellBot.deleteChatStickerSet()
  3589. {
  3590. local chat_id jq_obj
  3591. local param=$(getopt --name "$FUNCNAME" \
  3592. --options 'c:' \
  3593. --longoptions 'chat_id:' \
  3594. -- "$@")
  3595. eval set -- "$param"
  3596. while :
  3597. do
  3598. case $1 in
  3599. -c|--chat_id)
  3600. chat_id=$2
  3601. shift 2
  3602. ;;
  3603. --)
  3604. shift
  3605. break
  3606. ;;
  3607. esac
  3608. done
  3609. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3610. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} ${chat_id:+-d chat_id="$chat_id"})
  3611. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3612. return $?
  3613. }
  3614. ShellBot.inputMedia()
  3615. {
  3616. local __type __input __media __caption __parse_mode __thumb __width
  3617. local __height __duration __supports_streaming __performer __title
  3618. local __param=$(getopt --name "$FUNCNAME" \
  3619. --options 't:i:m:c:p:b:w:h:d:s:f:e:' \
  3620. --longoptions 'type:,
  3621. input:,
  3622. media:,
  3623. caption:,
  3624. parse_mode:,
  3625. thumb:,
  3626. witdh:,
  3627. height:,
  3628. duration:,
  3629. supports_streaming:,
  3630. performer:,
  3631. title:' \
  3632. -- "$@")
  3633. eval set -- "$__param"
  3634. while :
  3635. do
  3636. case $1 in
  3637. -t|--type)
  3638. __type=$2
  3639. shift 2
  3640. ;;
  3641. -i|--input)
  3642. CheckArgType var "$1" "$2"
  3643. __input=$2
  3644. shift 2
  3645. ;;
  3646. -m|--media)
  3647. CheckArgType file "$1" "$2"
  3648. __media=$2
  3649. shift 2
  3650. ;;
  3651. -c|--caption)
  3652. __caption=$(echo -e "$2")
  3653. shift 2
  3654. ;;
  3655. -p|--parse_mode)
  3656. __parse_mode=$2
  3657. shift 2
  3658. ;;
  3659. -b|--thumb)
  3660. CheckArgType file "$1" "$2"
  3661. __thumb=$2
  3662. shift 2
  3663. ;;
  3664. -w|--width)
  3665. CheckArgType int "$1" "$2"
  3666. __width=$2
  3667. shift 2
  3668. ;;
  3669. -h|--height)
  3670. CheckArgType int "$1" "$2"
  3671. __height=$2
  3672. shift 2
  3673. ;;
  3674. -d|--duration)
  3675. CheckArgType int "$1" "$2"
  3676. __duration=$2
  3677. shift 2
  3678. ;;
  3679. -s|--supports_streaming)
  3680. CheckArgType bool "$1" "$2"
  3681. __supports_streaming=$2
  3682. shift 2
  3683. ;;
  3684. -f|--performer)
  3685. __performer=$2
  3686. shift 2
  3687. ;;
  3688. -e|--title)
  3689. __title=$2
  3690. shift 2
  3691. ;;
  3692. --)
  3693. shift
  3694. break
  3695. ;;
  3696. esac
  3697. done
  3698. [[ $__type ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --type]"
  3699. [[ $__input ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-i, --input]"
  3700. [[ $__media ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --media]"
  3701. local -n __input=$__input
  3702. __input=${__input:+$__input,}{\"type\":\"$__type\",
  3703. __input+=\"media\":\"$__media\"
  3704. __input+=${__caption:+,\"caption\":\"$__caption\"}
  3705. __input+=${__parse_mode:+,\"parse_mode\":\"$__parse_mode\"}
  3706. __input+=${__thumb:+,\"thumb\":\"$__thumb\"}
  3707. __input+=${__width:+,\"width\":$__width}
  3708. __input+=${__height:+,\"height\":$__height}
  3709. __input+=${__duration:+,\"duration\":$__duration}
  3710. __input+=${__supports_streaming:+,\"supports_streaming\":$__supports_streaming}
  3711. __input+=${__performer:+,\"performer\":\"$__performer\"}
  3712. __input+=${__title:+,\"title\":\"$__title\"}}
  3713. return $?
  3714. }
  3715. ShellBot.sendMediaGroup()
  3716. {
  3717. local chat_id media disable_notification reply_to_message_id jq_obj
  3718. local param=$(getopt --name "$FUNCNAME" \
  3719. --options 'c:m:n:r:' \
  3720. --longoptions 'chat_id:,
  3721. media:,
  3722. disable_notification:,
  3723. reply_to_message_id:' \
  3724. -- "$@")
  3725. eval set -- "$param"
  3726. while :
  3727. do
  3728. case $1 in
  3729. -c|--chat_id)
  3730. chat_id=$2
  3731. shift 2
  3732. ;;
  3733. -m|--media)
  3734. media=[$2]
  3735. shift 2
  3736. ;;
  3737. -n|--disable_notification)
  3738. CheckArgType bool "$1" "$2"
  3739. disable_notification=$2
  3740. shift 2
  3741. ;;
  3742. -r|--reply_to_message_id)
  3743. CheckArgType int "$1" "$2"
  3744. reply_to_message_id=$2
  3745. shift 2
  3746. ;;
  3747. --)
  3748. shift
  3749. break
  3750. ;;
  3751. esac
  3752. done
  3753. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3754. [[ $media ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --media]"
  3755. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3756. ${chat_id:+-F chat_id="$chat_id"} \
  3757. ${media:+-F media="$media"} \
  3758. ${disable_notification:+-F disable_notification="$disable_notification"} \
  3759. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"})
  3760. # Retorno do método
  3761. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3762. # Status
  3763. return $?
  3764. }
  3765. ShellBot.editMessageMedia()
  3766. {
  3767. local chat_id message_id inline_message_id media reply_markup jq_obj
  3768. local param=$(getopt --name "$FUNCNAME" \
  3769. --options 'c:i:n:m:k:' \
  3770. --longoptions 'chat_id:,
  3771. message_id:,
  3772. inline_message_id:,
  3773. media:,
  3774. reply_markup:' \
  3775. -- "$@")
  3776. eval set -- "$param"
  3777. while :
  3778. do
  3779. case $1 in
  3780. -c|--chat_id)
  3781. chat_id=$2
  3782. shift 2
  3783. ;;
  3784. -i|--message_id)
  3785. CheckArgType int "$1" "$2"
  3786. message_id=$2
  3787. shift 2
  3788. ;;
  3789. -n|--inline_message_id)
  3790. CheckArgType int "$1" "$2"
  3791. inline_message_id=$2
  3792. shift 2
  3793. ;;
  3794. -m|--media)
  3795. media=$2
  3796. shift 2
  3797. ;;
  3798. -k|--reply_markup)
  3799. reply_markup=$2
  3800. shift 2
  3801. ;;
  3802. --)
  3803. shift
  3804. break
  3805. ;;
  3806. esac
  3807. done
  3808. [[ $inline_message_id ]] || {
  3809. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3810. [[ $message_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-i, --message_id]"
  3811. }
  3812. [[ $media ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-m, --media]"
  3813. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3814. ${chat_id:+-F chat_id="$chat_id"} \
  3815. ${message_id:+-F message_id="$message_id"} \
  3816. ${inline_message_id:+-F inline_message_id="$inline_message_id"} \
  3817. ${media:+-F media="$media"} \
  3818. ${reply_markup:+-F reply_markup="$reply_markup"})
  3819. # Retorno do método
  3820. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3821. # Status
  3822. return $?
  3823. }
  3824. ShellBot.sendAnimation()
  3825. {
  3826. local chat_id animation duration width height
  3827. local thumb caption parse_mode disable_notification
  3828. local reply_to_message_id reply_markup jq_obj
  3829. local param=$(getopt --name "$FUNCNAME" \
  3830. --options 'c:a:d:w:h:b:o:p:n:r:k:' \
  3831. --longoptions 'chat_id:,
  3832. animation:,
  3833. duration:,
  3834. width:,
  3835. height:,
  3836. thumb:,
  3837. caption:,
  3838. parse_mode:,
  3839. disable_notification:,
  3840. reply_to_message_id:,
  3841. reply_markup:' \
  3842. -- "$@")
  3843. eval set -- "$param"
  3844. while :
  3845. do
  3846. case $1 in
  3847. -c|--chat_id)
  3848. chat_id=$2
  3849. shift 2
  3850. ;;
  3851. -a|--animation)
  3852. CheckArgType file "$1" "$2"
  3853. animation=$2
  3854. shift 2
  3855. ;;
  3856. -d|--duration)
  3857. CheckArgType int "$1" "$2"
  3858. duartion=$2
  3859. shift 2
  3860. ;;
  3861. -w|--width)
  3862. CheckArgType int "$1" "$2"
  3863. width=$2
  3864. shift 2
  3865. ;;
  3866. -h|--height)
  3867. CheckArgType int "$1" "$2"
  3868. height=$2
  3869. shift 2
  3870. ;;
  3871. -b|--thumb)
  3872. CheckArgType file "$1" "$2"
  3873. thumb=$2
  3874. shift 2
  3875. ;;
  3876. -o|--caption)
  3877. caption=$(echo -e "$2")
  3878. shift 2
  3879. ;;
  3880. -p|--parse_mode)
  3881. parse_mode=$2
  3882. shift 2
  3883. ;;
  3884. -n|--disable_notification)
  3885. CheckArgType bool "$1" "$2"
  3886. disable_notification=$2
  3887. shift 2
  3888. ;;
  3889. -r|--reply_to_message_id)
  3890. CheckArgType int "$1" "$2"
  3891. reply_to_message_id=$2
  3892. shift 2
  3893. ;;
  3894. -k|--reply_markup)
  3895. reply_markup=$2
  3896. shift 2
  3897. ;;
  3898. --)
  3899. shift
  3900. break
  3901. ;;
  3902. esac
  3903. done
  3904. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  3905. [[ $animation ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-a, --animation]"
  3906. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3907. ${chat_id:+-F chat_id="$chat_id"} \
  3908. ${animation:+-F animation="$animation"} \
  3909. ${duration:+-F duration="$duration"} \
  3910. ${width:+-F width="$width"} \
  3911. ${height:+-F height="$height"} \
  3912. ${thumb:+-F thumb="$thumb"} \
  3913. ${caption:+-F caption="$caption"} \
  3914. ${parse_mode:+-F parse_mode="$parse_mode"} \
  3915. ${disable_notification:+-F disable_notification="$disable_notification"} \
  3916. ${reply_to_message_id:+-F reply_to_message_id="$reply_to_message_id"} \
  3917. ${reply_markup:+-F reply_markup="$reply_markup"})
  3918. # Retorno do método
  3919. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3920. # Status
  3921. return $?
  3922. }
  3923. ShellBot.answerInlineQuery()
  3924. {
  3925. local inline_query_id results cache_time is_personal
  3926. local next_offset switch_pm_text switch_pm_parameter
  3927. local jq_obj
  3928. local param=$(getopt --name "$FUNCNAME" \
  3929. --options 'i:r:c:p:o:s:m:' \
  3930. --longoptions 'inline_query_id:,
  3931. results:,
  3932. cache_time:,
  3933. is_personal:,
  3934. next_offset:,
  3935. switch_pm_text:,
  3936. switch_pm_parameter:' \
  3937. -- "$@")
  3938. eval set -- "$param"
  3939. while :
  3940. do
  3941. case $1 in
  3942. -i|--inline_query_id) inline_query_id=$2; shift 2;;
  3943. -r|--results) results=[$2]; shift 2;;
  3944. -c|--cache_time) cache_time=$2; shift 2;;
  3945. -p|--is_personal) cache_time=$2; shift 2;;
  3946. -o|--next_offset) next_offset=$2; shift 2;;
  3947. -s|--switch_pm_text) switch_pm_text=$2; shift 2;;
  3948. -m|--switch_pm_parameter) switch_pm_parameter=$2; shift 2;;
  3949. --) shift; break;;
  3950. esac
  3951. done
  3952. [[ $inline_query_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-i, --inline_query_id]"
  3953. [[ $results ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-r, --results]"
  3954. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  3955. ${inline_query_id:+-F inline_query_id="$inline_query_id"} \
  3956. ${results:+-F results="$results"} \
  3957. ${cache_time:+-F cache_time="$cache_time"} \
  3958. ${is_personal:+-F is_personal="$is_personal"} \
  3959. ${next_offset:+-F next_offset="$next_offset"} \
  3960. ${switch_pm_text:+-F switch_pm_text="$switch_pm_text"} \
  3961. ${switch_pm_parameter:+-F switch_pm_parameter="$switch_pm_parameter"})
  3962. # Retorno do método
  3963. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  3964. # Status
  3965. return $?
  3966. }
  3967. ShellBot.InlineQueryResult()
  3968. {
  3969. local __input __type __title __caption __reply_markup __parse_mode
  3970. local __description __input_message_content __address __audio_duration
  3971. local __audio_file_id __audio_url __document_file_id __document_url
  3972. local __first_name __foursquare_id __foursquare_type __gif_duration
  3973. local __gif_file_id __gif_height __gif_url __gif_width __hide_url
  3974. local __last_name __latitude __live_period __longitude __mime_type
  3975. local __mpeg4_duration __mpeg4_file_id __mpeg4_height __mpeg4_url
  3976. local __mpeg4_width __performer __photo_file_id __photo_height
  3977. local __photo_url __photo_width __sticker_file_id __vcard __phone_number
  3978. local __thumb_height __thumb_url __thumb_width __url __id
  3979. local __video_duration __video_file_id __video_height __video_url
  3980. local __video_width __voice_duration __voice_file_id __voice_url
  3981. local __param=$(getopt --name "$FUNCNAME" \
  3982. --options 'i:t:l:c:k:p:r:d:m:b:s:x:w:v:z:y:q:a:f:u:g:o:n:h:j:e:
  3983. N:R:D:A:X:G:C:Q:L:Y:E:V:H:Z:T:F:U:M:S:O:I:K:B:P:J:W:' \
  3984. --longoptions 'input:,
  3985. type:,
  3986. title:,
  3987. caption:,
  3988. reply_markup:,
  3989. parse_mode:,
  3990. id:,
  3991. description:,
  3992. input_message_content:,
  3993. address:,
  3994. audio_duration:,
  3995. audio_file_id:,
  3996. audio_url:,
  3997. document_file_id:,
  3998. document_url:,
  3999. first_name:,
  4000. foursquare_id:,
  4001. foursquare_type:,
  4002. gif_duration:,
  4003. gif_file_id:,
  4004. gif_height:,
  4005. gif_url:,
  4006. gif_width:,
  4007. hide_url:,
  4008. last_name:,
  4009. latitude:,
  4010. live_period:,
  4011. longitude:,
  4012. mime_type:,
  4013. mpeg4_duration:,
  4014. mpeg4_file_id:,
  4015. mpeg4_height:,
  4016. mpeg4_url:,
  4017. mpeg4_width:,
  4018. performer:,
  4019. photo_file_id:,
  4020. photo_height:,
  4021. photo_url:,
  4022. photo_width:,
  4023. sticker_file_id:,
  4024. thumb_height:,
  4025. thumb_url:,
  4026. thumb_width:,
  4027. url:,
  4028. vcard:,
  4029. video_duration:,
  4030. video_file_id:,
  4031. video_height:,
  4032. video_url:,
  4033. video_width:,
  4034. voice_duration:,
  4035. voice_file_id:,
  4036. voice_url:,
  4037. phone_number:' \
  4038. -- "$@")
  4039. eval set -- "$__param"
  4040. while :
  4041. do
  4042. case $1 in
  4043. -i|--input) CheckArgType var "$1" "$2"
  4044. __input=$2; shift 2;;
  4045. -t|--type) __type=$2; shift 2;;
  4046. -l|--title) __title=$2; shift 2;;
  4047. -c|--caption) __caption=$2; shift 2;;
  4048. -k|--reply_markup) __reply_markup=$2; shift 2;;
  4049. -p|--parse_mode) __parse_mode=$2; shift 2;;
  4050. -r|--id) __id=$2; shift 2;;
  4051. -d|--description) __description=$2; shift 2;;
  4052. -m|--input_message_content) __input_message_content=$2; shift 2;;
  4053. -b|--address) __address=$2; shift 2;;
  4054. -s|--audio_duration) __audio_duration=$2; shift 2;;
  4055. -x|--audio_file_id) __audio_file_id=$2; shift 2;;
  4056. -w|--audio_url) __audio_url=$2; shift 2;;
  4057. -v|--document_file_id) __document_file_id=$2; shift 2;;
  4058. -z|--document_url) __document_url=$2; shift 2;;
  4059. -y|--first_name) __first_name=$2; shift 2;;
  4060. -q|--foursquare_id) __foursquare_id=$2; shift 2;;
  4061. -a|--foursquare_type) __foursquare_type=$2; shift 2;;
  4062. -f|--gif_duration) __gif_duration=$2; shift 2;;
  4063. -u|--gif_file_id) __gif_file_id=$2 shift 2;;
  4064. -g|--gif_height) __gif_height=$2; shift 2;;
  4065. -o|--gif_url) __gif_url=$2; shift 2;;
  4066. -n|--gif_width) __gif_width=$2; shift 2;;
  4067. -h|--hide_url) __hide_url=$2; shift 2;;
  4068. -j|--last_name) __last_name=$2; shift 2;;
  4069. -e|--latitude) __latitude=$2; shift 2;;
  4070. -N|--live_period) __live_period=$2; shift 2;;
  4071. -R|--longitude) __longitude=$2; shift 2;;
  4072. -D|--mime_type) __mime_type=$2; shift 2;;
  4073. -A|--mpeg4_duration) __mpeg4_duration=$2; shift 2;;
  4074. -X|--mpeg4_file_id) __mpeg4_file_id=$2; shift 2;;
  4075. -G|--mpeg4_height) __mpeg4_height=$2; shift 2;;
  4076. -C|--mpeg4_url) __mpeg4_url=$2; shift 2;;
  4077. -Q|--mpeg4_width) __mpeg4_width=$2; shift 2;;
  4078. -L|--performer) __performer=$2; shift 2;;
  4079. -Y|--photo_file_id) __photo_file_id=$2; shift 2;;
  4080. -E|--photo_height) __photo_height=$2; shift 2;;
  4081. -V|--photo_url) __photo_url=$2; shift 2;;
  4082. -H|--photo_width) __photo_width=$2; shift 2;;
  4083. -Z|--sticker_file_id) __sticker_file_id=$2; shift 2;;
  4084. -T|--thumb_height) __thumb_height=$2; shift 2;;
  4085. -F|--thumb_url) __thumb_url=$2; shift 2;;
  4086. -U|--thumb_width) __thumb_width=$2; shift 2;;
  4087. -M|--url) __url=$2; shift 2;;
  4088. -S|--vcard) __vcard=$2; shift 2;;
  4089. -O|--video_duration) __video_duration=$2; shift 2;;
  4090. -I|--video_file_id) __video_file_id=$2; shift 2;;
  4091. -K|--video_height) __video_height=$2; shift 2;;
  4092. -B|--video_url) __video_url=$2; shift 2;;
  4093. -P|--video_width) __video_width=$2; shift 2;;
  4094. -J|--voice_duration) __voice_duration=$2; shift 2;;
  4095. -W|--voice_file_id) __voice_file_id=$2; shift 2;;
  4096. --phone_number) __phone_number=$2; shift 2;;
  4097. --voice_url) __voice_url=$2; shift 2;;
  4098. --) shift; break;;
  4099. esac
  4100. done
  4101. [[ $__input ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-i, --input]"
  4102. local -n __input=$__input
  4103. __input=${__input:+$__input,}{\"type\":\"$__type\"
  4104. __input+=${__title:+,\"title\":\"$__title\"}
  4105. __input+=${__caption:+,\"caption\":\"$__caption\"}
  4106. __input+=${__reply_markup:+,\"reply_markup\":\"$__reply_markup\"}
  4107. __input+=${__parse_mode:+,\"parse_mode\":\"$__parse_mode\"}
  4108. __input+=${__id:+,\"id\":\"$__id\"}
  4109. __input+=${__description:+,\"description\":\"$__description\"}
  4110. __input+=${__input_message_content:+,\"input_message_content\":$__input_message_content}
  4111. __input+=${__address:+,\"address\":\"$__address\"}
  4112. __input+=${__audio_duration:+,\"audio_duration\":$__audio_duration}
  4113. __input+=${__audio_file_id:+,\"audio_file_id\":\"$__audio_file_id\"}
  4114. __input+=${__audio_url:+,\"audio_url\":\"$__audio_url\"}
  4115. __input+=${__document_file_id:+,\"document_file_id\":\"$__document_file_id\"}
  4116. __input+=${__document_url:+,\"document_url\":\"$__document_url\"}
  4117. __input+=${__first_name:+,\"first_name\":\"$__first_name\"}
  4118. __input+=${__foursquare_id:+,\"foursquare_id\":\"$__foursquare_id\"}
  4119. __input+=${__foursquare_type:+,\"foursquare_type\":\"$__foursquare_type\"}
  4120. __input+=${__gif_duration:+,\"gif_duration\":$__gif_duration}
  4121. __input+=${__gif_file_id:+,\"gif_file_id\":\"$__gif_file_id\"}
  4122. __input+=${__gif_height:+,\"gif_height\":$__gif_height}
  4123. __input+=${__gif_url:+,\"gif_url\":\"$__gif_url\"}
  4124. __input+=${__gif_width:+,\"gif_width\":$__gif_width}
  4125. __input+=${__hide_url:+,\"hide_url\":\"$__hide_url\"}
  4126. __input+=${__last_name:+,\"last_name\":\"$__last_name\"}
  4127. __input+=${__latitude:+,\"latitude\":$__latitude}
  4128. __input+=${__live_period:+,\"live_period\":$__live_period}
  4129. __input+=${__longitude:+,\"longitude\":$__longitude}
  4130. __input+=${__mime_type:+,\"mime_type\":\"$__mime_type\"}
  4131. __input+=${__mpeg4_duration:+,\"mpeg4_duration\":$__mpeg4_duration}
  4132. __input+=${__mpeg4_file_id:+,\"mpeg4_file_id\":\"$__mpeg4_file_id\"}
  4133. __input+=${__mpeg4_height:+,\"mpeg4_height\":$__mpeg4_height}
  4134. __input+=${__mpeg4_url:+,\"mpeg4_url\":\"$__mpeg4_url\"}
  4135. __input+=${__mpeg4_width:+,\"mpeg4_width\":$__mpeg4_width}
  4136. __input+=${__performer:+,\"performer\":\"$__performer\"}
  4137. __input+=${__photo_file_id:+,\"photo_file_id\":\"$__photo_file_id\"}
  4138. __input+=${__photo_height:+,\"photo_height\":$__photo_height}
  4139. __input+=${__photo_url:+,\"photo_url\":\"$__photo_url\"}
  4140. __input+=${__photo_width:+,\"photo_width\":$__photo_width}
  4141. __input+=${__sticker_file_id:+,\"sticker_file_id\":\"$__sticker_file_id\"}
  4142. __input+=${__thumb_height:+,\"thumb_height\":$__thumb_height}
  4143. __input+=${__thumb_url:+,\"thumb_url\":\"$__thumb_url\"}
  4144. __input+=${__thumb_width:+,\"thumb_width\":$__thumb_width}
  4145. __input+=${__url:+,\"url\":\"$__url\"}
  4146. __input+=${__vcard:+,\"vcard\":\"$__vcard\"}
  4147. __input+=${__video_duration:+,\"video_duration\":$__video_duration}
  4148. __input+=${__video_file_id:+,\"video_file_id\":\"$__video_file_id\"}
  4149. __input+=${__video_height:+,\"video_height\":$__video_height}
  4150. __input+=${__video_url:+,\"video_url\":\"$__video_url\"}
  4151. __input+=${__video_width:+,\"video_width\":$__video_width}
  4152. __input+=${__voice_duration:+,\"voice_duration\":$__voice_duration}
  4153. __input+=${__voice_file_id:+,\"voice_file_id\":\"$__voice_file_id\"}
  4154. __input+=${__voice_url:+,\"voice_url\":\"$__voice_url\"}
  4155. __input+=${__phone_number:+,\"phone_number\":\"$__phone_number\"}}
  4156. return $?
  4157. }
  4158. ShellBot.InputMessageContent()
  4159. {
  4160. local message_text parse_mode disable_web_page_preview json
  4161. local latitude longitude live_period title address foursquare_id
  4162. local foursquare_type phone_number first_name last_name vcard
  4163. local param=$(getopt --name "$FUNCNAME" \
  4164. --options 't:p:w:l:v:e:a:f:q:n:m:s:d:' \
  4165. --longoptions 'message_text:,
  4166. parse_mode:,
  4167. disable_web_page_preview:,
  4168. latitude:,
  4169. longitude:,
  4170. live_period:,
  4171. title:,
  4172. address:,
  4173. foursquare_id:,
  4174. foursquare_type:,
  4175. phone_number:,
  4176. first_name:,
  4177. last_name:,
  4178. vcard:' \
  4179. -- "$@")
  4180. eval set -- "$param"
  4181. while :
  4182. do
  4183. case $1 in
  4184. -t|--message_text) message_text=$(echo -e "$2"); shift 2;;
  4185. -p|--parse_mode) parse_mode=$2; shift 2;;
  4186. -w|--disable_web_page_preview) disable_web_page_preview=$2; shift 2;;
  4187. -l|--latitude) latitude=$2; shift 2;;
  4188. -g|--longitude) longitude=$2; shift 2;;
  4189. -v|--live_period) live_period=$2; shift 2;;
  4190. -e|--title) title=$2; shift 2;;
  4191. -a|--address) address=$2; shift 2;;
  4192. -f|--foursquare_id) foursquare_id=$2; shift 2;;
  4193. -q|--foursquare_type) foursquare_type=$2; shift 2;;
  4194. -n|--phone_number) phone_number=$2; shift 2;;
  4195. -m|--first_name) first_name=$2; shift 2;;
  4196. -s|--last_name) last_name=$2; shift 2;;
  4197. -d|--vcard) vcard=$2; shift 2;;
  4198. --) shift; break;;
  4199. esac
  4200. done
  4201. json=${message_text:+\"message_text\":\"$message_text\"}
  4202. json+=${parse_mode:+,\"parse_mode\":\"$parse_mode\"}
  4203. json+=${disable_web_page_preview:+,\"disable_web_page_preview\":$disable_web_page_preview}
  4204. json+=${latitude:+,\"latitude\":$latitude}
  4205. json+=${longitude:+,\"longtitude\":$longitude}
  4206. json+=${live_period:+,\"live_period\":$live_period}
  4207. json+=${title:+,\"title\":\"$title\"}
  4208. json+=${address:+,\"address\":\"$address\"}
  4209. json+=${foursquare_id:+,\"foursquare_id\":\"$foursquare_id\"}
  4210. json+=${foursquare_type:+,\"foursquare_type\":\"$foursquare_type\"}
  4211. json+=${phone_number:+,\"phone_number\":\"$phone_number\"}
  4212. json+=${first_name:+,\"first_name\":\"$first_name\"}
  4213. json+=${last_name:+,\"last_name\":\"$last_name\"}
  4214. json+=${vcard:+,\"vcard\":\"$vcard\"}
  4215. echo "{${json#,}}"
  4216. return $?
  4217. }
  4218. ShellBot.ChatPermissions()
  4219. {
  4220. local can_send_messages can_send_media_messages can_send_polls
  4221. local can_send_other_messages can_add_web_page_previews json
  4222. local can_change_info can_invite_users can_pin_messages
  4223. local param=$(getopt --name "$FUNCNAME" \
  4224. --options 'm:d:l:o:w:c:i:p:' \
  4225. --longoptions 'can_send_messages:,
  4226. can_send_media_messages:,
  4227. can_send_polls:,
  4228. can_send_other_messages:,
  4229. can_add_web_page_previews:,
  4230. can_change_info:,
  4231. can_invite_users:,
  4232. can_pin_messages:' \
  4233. -- "$@")
  4234. eval set -- "$param"
  4235. while :
  4236. do
  4237. case $1 in
  4238. -m|--can_send_messages) can_send_messages=$2;;
  4239. -d|--can_send_media_messages) can_send_media_messages=$2;;
  4240. -l|--can_send_polls) can_send_polls=$2;;
  4241. -o|--can_send_other_messages) can_send_other_messages=$2;;
  4242. -w|--can_add_web_page_previews) can_add_web_page_previews=$2;;
  4243. -c|--can_change_info) can_change_info=$2;;
  4244. -i|--can_invite_users) can_invite_users=$2;;
  4245. -p|--can_pin_messages) can_pin_messages=$2;;
  4246. --) shift; break;;
  4247. esac
  4248. shift 2
  4249. done
  4250. json=${can_send_messages:+\"can_send_messages\":$can_send_messages,}
  4251. json+=${can_send_media_messages:+\"can_send_media_messages\":$can_send_media_messages,}
  4252. json+=${can_send_polls:+\"can_send_polls\":$can_send_polls,}
  4253. json+=${can_send_other_messages:+\"can_send_other_messages\":$can_send_other_messages,}
  4254. json+=${can_add_web_page_previews:+\"can_add_web_page_previews\":$can_add_web_page_previews,}
  4255. json+=${can_change_info:+\"can_change_info\":$can_change_info,}
  4256. json+=${can_invite_users:+\"can_invite_users\":$can_invite_users,}
  4257. json+=${can_pin_messages:+\"can_pin_messages\":$can_pin_messages,}
  4258. # Retorna o objeto de permissões.
  4259. echo "{${json%,}}"
  4260. # Status
  4261. return $?
  4262. }
  4263. ShellBot.setChatPermissions()
  4264. {
  4265. local chat_id permissions jq_obj
  4266. local param=$(getopt --name "$FUNCNAME" \
  4267. --options 'c:p:' \
  4268. --longoptions 'chat_id:,permissions:' \
  4269. -- "$@")
  4270. eval set -- "$param"
  4271. while :
  4272. do
  4273. case $1 in
  4274. -c|--chat_id) chat_id=$2;;
  4275. -p|--permissions) permissions=$2;;
  4276. --) shift; break;;
  4277. esac
  4278. shift 2
  4279. done
  4280. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  4281. [[ $permissions ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-p, --permissions]"
  4282. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  4283. ${chat_id:+-d chat_id="$chat_id"} \
  4284. ${permissions:+-d permissions="$permissions"})
  4285. # Retorno do método
  4286. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  4287. # Status
  4288. return $?
  4289. }
  4290. ShellBot.setChatAdministratorCustomTitle()
  4291. {
  4292. local chat_id user_id custom_title jq_obj
  4293. local param=$(getopt --name "$FUNCNAME" \
  4294. --options 'c:u:t:' \
  4295. --longoptions 'chat_id:,
  4296. user_id:,
  4297. custom_title:' \
  4298. -- "$@")
  4299. eval set -- "$param"
  4300. while :
  4301. do
  4302. case $1 in
  4303. -c|--chat_id) chat_id=$2;;
  4304. -u|--user_id) user_id=$2;;
  4305. -t|--custom_title) custom_title=$2;;
  4306. --) shift; break;;
  4307. esac
  4308. shift 2
  4309. done
  4310. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  4311. [[ $user_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --user_id]"
  4312. [[ $custom_title ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --custom_title]"
  4313. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  4314. ${chat_id:+-d chat_id="$chat_id"} \
  4315. ${user_id:+-d user_id="$user_id"} \
  4316. ${custom_tilte:+-d custom_title="$custom_title"})
  4317. # Retorno do método
  4318. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  4319. # Status
  4320. return $?
  4321. }
  4322. ShellBot.sendPoll()
  4323. {
  4324. local chat_id question options is_anonymous reply_markup
  4325. local type allows_multiple_answers correct_option_id jq_obj
  4326. local is_closed disable_notification reply_to_message_id
  4327. local explanation explanation_parse_mode open_period close_date
  4328. local param=$(getopt --name "$FUNCNAME" \
  4329. --options 'c:q:o:a:k:t:m:i:x:z:p:d:l:n:r:' \
  4330. --longoptions 'chat_id:,
  4331. question:,
  4332. options:,
  4333. is_anonymous:,
  4334. reply_markup:,
  4335. type:,
  4336. allows_multiple_answers:,
  4337. correct_option_id:,
  4338. explanation:,
  4339. explanation_parse_mode:,
  4340. open_period:,
  4341. close_date:,
  4342. is_closed:,
  4343. disable_notification:,
  4344. reply_to_message_id:' \
  4345. -- "$@")
  4346. eval set -- "$param"
  4347. while :
  4348. do
  4349. case $1 in
  4350. -c|--chat_id) chat_id=$2;;
  4351. -q|--question) question=$(echo -e "$2");;
  4352. -o|--options) options=$(echo -e "$2");;
  4353. -a|--is_anonymous) is_anonymous=$2;;
  4354. -k|--reply_markup) reply_markup=$2;;
  4355. -t|--type) type=$2;;
  4356. -m|--allows_multiple_answers) allows_multiple_answers=$2;;
  4357. -i|--correct_option_id) correct_option_id=$2;;
  4358. -x|--explanation) explanation=$2;;
  4359. -z|--explanation_parse_mode) explanation_parse_mode=$2;;
  4360. -p|--open_period) open_period=$2;;
  4361. -d|--close_date) close_date=$2;;
  4362. -l|--is_closed) is_closed=$2;;
  4363. -n|--disable_notification) disable_notification=$2;;
  4364. -r|--reply_to_message_id) reply_to_message_id=$2;;
  4365. --) shift; break;;
  4366. esac
  4367. shift 2
  4368. done
  4369. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  4370. [[ $question ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-q, --question]"
  4371. [[ $options ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-o, --options]"
  4372. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  4373. ${chat_id:+-d chat_id="$chat_id"} \
  4374. ${question:+-d question="$question"} \
  4375. ${options:+-d options="$options"} \
  4376. ${is_anonymous:+-d is_anonymous="$is_anonymous"} \
  4377. ${reply_markup:+-d reply_markup="$reply_markup"} \
  4378. ${type:+-d type="$type"} \
  4379. ${allows_multiple_answers:+-d allows_multiple_answers="$allows_multiple_answers"} \
  4380. ${correct_option_id:+-d correct_option_id="$correct_option_id"} \
  4381. ${explanation:+-d explanation="$explanation"} \
  4382. ${explanation_parse_mode:+-d explanation_parse_mode="$explanation_parse_mode"} \
  4383. ${open_period:+-d open_period="$open_period"} \
  4384. ${close_date:+-d close_date="$close_date"} \
  4385. ${is_closed:+-d is_closed="$is_closed"} \
  4386. ${disable_notification:+-d disable_notification="$disable_notification"} \
  4387. ${reply_to_message_id:+-d reply_to_message_id="$reply_to_message_id"})
  4388. # Retorno do método
  4389. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  4390. # Status
  4391. return $?
  4392. }
  4393. ShellBot.KeyboardButtonPollType()
  4394. {
  4395. local type
  4396. local param=$(getopt --name "$FUNCNAME" --options 't:' --longoptions 'type:' -- "$@")
  4397. eval set -- "$param"
  4398. while :
  4399. do
  4400. case $1 in
  4401. -t|--type) type=$2;;
  4402. --) shift; break;;
  4403. esac
  4404. shift 2
  4405. done
  4406. [[ $type ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-t, --type]"
  4407. printf '{"type": "%s"}' "$type"
  4408. return 0
  4409. }
  4410. ShellBot.sendDice()
  4411. {
  4412. local chat_id disable_notification reply_to_message_id
  4413. local reply_markup jq_obj emoji
  4414. local param=$(getopt --name "$FUNCNAME" \
  4415. --options 'c:e:n:r:k:' \
  4416. --longoptions 'chat_id:,
  4417. emoji:,
  4418. disable_notification:,
  4419. reply_to_message_id:,
  4420. reply_markup:' \
  4421. -- "$@")
  4422. eval set -- "$param"
  4423. while :
  4424. do
  4425. case $1 in
  4426. -c|--chat_id) chat_id=$2;;
  4427. -e|--emoji) emoji=$2;;
  4428. -n|--disable_notification) disable_notification=$2;;
  4429. -r|--reply_to_message_id) reply_to_message_id=$2;;
  4430. -k|--reply_markup) reply_markup=$2;;
  4431. --) shift; break;;
  4432. esac
  4433. shift 2
  4434. done
  4435. [[ $chat_id ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --chat_id]"
  4436. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  4437. ${chat_id:+-d chat_id="$chat_id"} \
  4438. ${emoji:+-d emoji="$emoji"} \
  4439. ${disable_notification:+-d disable_notification="$disable_notification"} \
  4440. ${reply_to_message_id:+-d reply_to_message_id="$reply_to_message_id"} \
  4441. ${reply_markup:+-d reply_markup="$reply_markup"})
  4442. # Retorno do método
  4443. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  4444. # Status
  4445. return $?
  4446. }
  4447. ShellBot.getMyCommands()
  4448. {
  4449. local jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.})
  4450. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  4451. return $?
  4452. }
  4453. ShellBot.setMyCommands()
  4454. {
  4455. local jq_obj commands
  4456. local param=$(getopt --name "$FUNCNAME" \
  4457. --options 'c:' \
  4458. --longoptions 'commands:' \
  4459. -- "$@")
  4460. eval set -- "$param"
  4461. while :
  4462. do
  4463. case $1 in
  4464. -c|--commands) commands=$2;;
  4465. --) break;;
  4466. esac
  4467. shift 2
  4468. done
  4469. [[ $commands ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --commands]"
  4470. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} ${commands:+-d commands="$commands"})
  4471. # Retorno do método
  4472. MethodReturn "$jq_obj" || MessageError TG "$jq_obj"
  4473. # Status
  4474. return $?
  4475. }
  4476. ShellBot.BotCommand()
  4477. {
  4478. local __command __description __list
  4479. local __param=$(getopt --name "$FUNCNAME" \
  4480. --options 'l:c:d:' \
  4481. --longoptions 'list:,
  4482. command:,
  4483. description:' \
  4484. -- "$@")
  4485. eval set -- "$__param"
  4486. while :
  4487. do
  4488. case $1 in
  4489. -l|--list) CheckArgType var "$1" "$2"; __list=$2;;
  4490. -c|--command) __command=$2;;
  4491. -d|--description) __description=$2;;
  4492. --) break;;
  4493. esac
  4494. shift 2
  4495. done
  4496. [[ $__list ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-l, --list]"
  4497. [[ $__command ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-c, --command]"
  4498. [[ $__description ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-d, --description]"
  4499. printf -v $__list '%s' "${!__list#[}"
  4500. printf -v $__list '%s' "${!__list%]}"
  4501. printf -v $__list '%s{"command": "%s", "description": "%s"}' \
  4502. "${!__list:+${!__list},}" \
  4503. "$__command" \
  4504. "$__description"
  4505. printf -v $__list '%s' "[${!__list}]"
  4506. return $?
  4507. }
  4508. ShellBot.setMessageRules()
  4509. {
  4510. local action command user_id username chat_id
  4511. local chat_type time date language message_id
  4512. local is_bot text entities_type file_type name
  4513. local query_data query_id query_text send_message
  4514. local chat_member mime_type num_args exec rule
  4515. local action_args weekday user_status chat_name
  4516. local message_status reply_message parse_mode
  4517. local forward_message reply_markup continue i
  4518. local author_signature bot_action auth_file
  4519. local param=$(getopt --name "$FUNCNAME" \
  4520. --options 's:a:z:c:i:u:h:v:y:l:m:b:t:n:f:p:q:r:g:o:e:d:w:j:x:R:S:F:K:P:E:A:C:B:T:' \
  4521. --longoptions 'name:,
  4522. action:,
  4523. action_args:,
  4524. command:,
  4525. user_id:,
  4526. username:,
  4527. chat_id:,
  4528. chat_name:,
  4529. chat_type:,
  4530. language_code:,
  4531. message_id:,
  4532. is_bot:,
  4533. text:,
  4534. entitie_type:,
  4535. file_type:,
  4536. mime_type:,
  4537. query_data:,
  4538. query_id:,
  4539. chat_member:,
  4540. num_args:,
  4541. time:,
  4542. date:,
  4543. weekday:,
  4544. user_status:,
  4545. message_status:,
  4546. exec:,
  4547. auth_file:,
  4548. bot_reply_message:,
  4549. bot_send_message:,
  4550. bot_forward_message:,
  4551. bot_reply_markup:,
  4552. bot_parse_mode:,
  4553. bot_action:,
  4554. author_signature:,
  4555. continue' \
  4556. -- "$@")
  4557. eval set -- "$param"
  4558. while :
  4559. do
  4560. case $1 in
  4561. -s|--name)
  4562. CheckArgType flag "$1" "$2"
  4563. name=$2
  4564. shift 2
  4565. ;;
  4566. -a|--action)
  4567. CheckArgType func "$1" "$2"
  4568. action=$2
  4569. shift 2
  4570. ;;
  4571. -z|--action_args)
  4572. action_args=$2
  4573. shift 2
  4574. ;;
  4575. -c|--command)
  4576. CheckArgType cmd "$1" "$2"
  4577. command=$2
  4578. shift 2
  4579. ;;
  4580. -i|--user_id)
  4581. user_id=${user_id:+$user_id|}${2//[,$'\n']/|}
  4582. shift 2
  4583. ;;
  4584. -u|--username)
  4585. username=${username:+$username|}${2//[,$'\n']/|}
  4586. shift 2
  4587. ;;
  4588. -h|--chat_id)
  4589. chat_id=${chat_id:+$chat_id|}${2//[,$'\n']/|}
  4590. shift 2
  4591. ;;
  4592. -v|--chat_name)
  4593. chat_name=${chat_name:+$chat_name|}${2//[,$'\n']/|}
  4594. shift 2
  4595. ;;
  4596. -y|--chat_type)
  4597. chat_type=${chat_type:+$chat_type|}${2//[,$'\n']/|}
  4598. shift 2
  4599. ;;
  4600. -e|--time)
  4601. time=${time:+$time|}${2//[,$'\n']/|}
  4602. shift 2
  4603. ;;
  4604. -d|--date)
  4605. date=${date:+$date|}${2//[,$'\n']/|}
  4606. shift 2
  4607. ;;
  4608. -l|--laguage_code)
  4609. language=${language:+$language|}${2//[,$'\n']/|}
  4610. shift 2
  4611. ;;
  4612. -m|--message_id)
  4613. message_id=${message_id:+$message_id|}${2//[,$'\n']/|}
  4614. shift 2
  4615. ;;
  4616. -b|--is_bot)
  4617. is_bot=${is_bot:+$is_bot|}${2//[,$'\n']/|}
  4618. shift 2
  4619. ;;
  4620. -t|--text)
  4621. text=${2//$'\n'/|}
  4622. shift 2
  4623. ;;
  4624. -n|--entitie_type)
  4625. entities_type=${entities_type:+$entities_type|}${2//[,$'\n']/|}
  4626. shift 2
  4627. ;;
  4628. -f|--file_type)
  4629. file_type=${file_type:+$file_type|}${2//[,$'\n']/|}
  4630. shift 2
  4631. ;;
  4632. -p|--mime_type)
  4633. mime_type=${mime_type:+$mime_type|}${2//[,$'\n']/|}
  4634. shift 2
  4635. ;;
  4636. -q|--query_data)
  4637. query_data=${query_data:+$query_data|}${2//[,$'\n']/|}
  4638. shift 2
  4639. ;;
  4640. -r|--query_id)
  4641. query_id=${query_id:+$query_id|}${2//[,$'\n']/|}
  4642. shift 2
  4643. ;;
  4644. -g|--chat_member)
  4645. chat_member=${chat_member:+$chat_member|}${2//[,$'\n']/|}
  4646. shift 2
  4647. ;;
  4648. -o|--num_args)
  4649. num_args=${num_args:+$num_args|}${2//[,$'\n']/|}
  4650. shift 2
  4651. ;;
  4652. -w|--weekday)
  4653. weekday=${weekday:+$weekday|}${2//[,$'\n']/|}
  4654. shift 2
  4655. ;;
  4656. -j|--user_status)
  4657. user_status=${user_status:+$user_status|}${2//[,$'\n']/|}
  4658. shift 2
  4659. ;;
  4660. -x|--message_status)
  4661. message_status=${message_status:+$message_status|}${2//[,$'\n']/|}
  4662. shift 2
  4663. ;;
  4664. -T|--auth_file)
  4665. auth_file=${auth_file:+$auth_file|}${2//[,$'\n']/|}
  4666. shift 2
  4667. ;;
  4668. -R|--bot_reply_message)
  4669. reply_message=$2
  4670. shift 2
  4671. ;;
  4672. -S|--bot_send_message)
  4673. send_message=$2
  4674. shift 2
  4675. ;;
  4676. -F|--bot_forward_message)
  4677. forward_message=${forward_message:+$forward_message|}${2//[,$'\n']/|}
  4678. shift 2
  4679. ;;
  4680. -K|--bot_reply_markup)
  4681. reply_markup=$2
  4682. shift 2
  4683. ;;
  4684. -P|--bot_parse_mode)
  4685. parse_mode=$2
  4686. shift 2
  4687. ;;
  4688. -B|--bot_action)
  4689. bot_action=$2
  4690. shift 2
  4691. ;;
  4692. -E|--exec)
  4693. exec=$2
  4694. shift 2
  4695. ;;
  4696. -A|--author_signature)
  4697. author_signature=${author_signature:+$author_signature|}${2//[,$'\n']/|}
  4698. shift 2
  4699. ;;
  4700. -C|--continue)
  4701. continue=true
  4702. shift
  4703. ;;
  4704. --)
  4705. shift
  4706. break
  4707. ;;
  4708. esac
  4709. done
  4710. [[ $name ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-s, --name]"
  4711. [[ ${_BOT_RULES_[$name]} ]] && MessageError API "$_ERR_RULE_ALREADY_EXISTS_" "[-s, --name]" "$name"
  4712. i=${_BOT_RULES_INDEX_:=0}
  4713. _BOT_RULES_[$i:source]=${BASH_SOURCE[1]##*/}
  4714. _BOT_RULES_[$i:line]=${BASH_LINENO}
  4715. _BOT_RULES_[$i:name]=${name}
  4716. _BOT_RULES_[$i:action]=${action}
  4717. _BOT_RULES_[$i:action_args]=${action_args}
  4718. _BOT_RULES_[$i:user_id]=${user_id}
  4719. _BOT_RULES_[$i:username]=${username}
  4720. _BOT_RULES_[$i:chat_id]=${chat_id}
  4721. _BOT_RULES_[$i:chat_name]=${chat_name}
  4722. _BOT_RULES_[$i:chat_type]=${chat_type}
  4723. _BOT_RULES_[$i:language_code]=${language}
  4724. _BOT_RULES_[$i:message_id]=${message_id}
  4725. _BOT_RULES_[$i:is_bot]=${is_bot}
  4726. _BOT_RULES_[$i:command]=${command}
  4727. _BOT_RULES_[$i:text]=${text}
  4728. _BOT_RULES_[$i:entities_type]=${entities_type}
  4729. _BOT_RULES_[$i:file_type]=${file_type}
  4730. _BOT_RULES_[$i:mime_type]=${mime_type}
  4731. _BOT_RULES_[$i:query_data]=${query_data}
  4732. _BOT_RULES_[$i:query_id]=${query_id}
  4733. _BOT_RULES_[$i:chat_member]=${chat_member}
  4734. _BOT_RULES_[$i:num_args]=${num_args}
  4735. _BOT_RULES_[$i:time]=${time}
  4736. _BOT_RULES_[$i:date]=${date}
  4737. _BOT_RULES_[$i:weekday]=${weekday}
  4738. _BOT_RULES_[$i:user_status]=${user_status}
  4739. _BOT_RULES_[$i:message_status]=${message_status}
  4740. _BOT_RULES_[$i:author_signature]=${author_signature}
  4741. _BOT_RULES_[$i:auth_file]=${auth_file}
  4742. _BOT_RULES_[$i:bot_reply_message]=${reply_message}
  4743. _BOT_RULES_[$i:bot_send_message]=${send_message}
  4744. _BOT_RULES_[$i:bot_forward_message]=${forward_message}
  4745. _BOT_RULES_[$i:bot_reply_markup]=${reply_markup}
  4746. _BOT_RULES_[$i:bot_parse_mode]=${parse_mode}
  4747. _BOT_RULES_[$i:bot_action]=${bot_action}
  4748. _BOT_RULES_[$i:exec]=${exec}
  4749. _BOT_RULES_[$i:continue]=${continue}
  4750. _BOT_RULES_[$name]=true
  4751. # Incrementa índice.
  4752. ((_BOT_RULES_INDEX_++))
  4753. return $?
  4754. }
  4755. ShellBot.manageRules()
  4756. {
  4757. local uid rule botcmd err tm stime etime ctime mime_type weekday
  4758. local dt sdate edate cdate mem ent type args status out fwid
  4759. local stdout i re match file user line
  4760. local u_message_text u_message_id u_message_from_is_bot
  4761. local u_message_from_id u_message_from_username msgstatus argpos
  4762. local u_message_from_language_code u_message_chat_id message_status
  4763. local u_message_chat_type u_message_date u_message_entities_type
  4764. local u_message_mime_type u_message_author_signature
  4765. local param=$(getopt --name "$FUNCNAME" \
  4766. --options 'u:' \
  4767. --longoptions 'update_id:' \
  4768. -- "$@")
  4769. eval set -- "$param"
  4770. while :
  4771. do
  4772. case $1 in
  4773. -u|--update_id)
  4774. CheckArgType int "$1" "$2"
  4775. uid=$2
  4776. shift 2
  4777. ;;
  4778. --)
  4779. shift
  4780. break
  4781. ;;
  4782. esac
  4783. done
  4784. [[ $uid ]] || MessageError API "$_ERR_PARAM_REQUIRED_" "[-u, --update_id]"
  4785. # Regras (somente-leitura)
  4786. readonly _BOT_RULES_ _BOT_RULES_INDEX_
  4787. [[ ${u_message_text:=${message_text[$uid]}} ]] ||
  4788. [[ ${u_message_text:=${edited_message_text[$uid]}} ]] ||
  4789. [[ ${u_message_text:=${callback_query_message_text[$uid]}} ]] ||
  4790. [[ ${u_message_text:=${inline_query_query[$uid]}} ]] ||
  4791. [[ ${u_message_text:=${chosen_inline_result_query[$uid]}} ]] ||
  4792. [[ ${u_message_text:=${channel_post_text[$uid]}} ]] ||
  4793. [[ ${u_message_text:=${edited_channel_post_text[$uid]}} ]]
  4794. [[ ${u_message_id:=${message_message_id[$uid]}} ]] ||
  4795. [[ ${u_message_id:=${edited_message_message_id[$uid]}} ]] ||
  4796. [[ ${u_message_id:=${callback_query_message_message_id[$uid]}} ]] ||
  4797. [[ ${u_message_id:=${inline_query_id[$uid]}} ]] ||
  4798. [[ ${u_message_id:=${chosen_inline_result_result_id[$uid]}} ]] ||
  4799. [[ ${u_message_id:=${channel_post_message_id[$uid]}} ]] ||
  4800. [[ ${u_message_id:=${edited_channel_post_message_id[$uid]}} ]] ||
  4801. [[ ${u_message_id:=${poll_answer_poll_id[$uid]}} ]]
  4802. [[ ${u_message_from_is_bot:=${message_from_is_bot[$uid]}} ]] ||
  4803. [[ ${u_message_from_is_bot:=${edited_message_from_is_bot[$uid]}} ]] ||
  4804. [[ ${u_message_from_is_bot:=${callback_query_from_is_bot[$uid]}} ]] ||
  4805. [[ ${u_message_from_is_bot:=${inline_query_from_is_bot[$uid]}} ]] ||
  4806. [[ ${u_message_from_is_bot:=${chosen_inline_result_from_is_bot[$uid]}} ]] ||
  4807. [[ ${u_message_from_is_bot:=${poll_answer_user_is_bot[$uid]}} ]]
  4808. [[ ${u_message_from_id:=${message_from_id[$uid]}} ]] ||
  4809. [[ ${u_message_from_id:=${edited_message_from_id[$uid]}} ]] ||
  4810. [[ ${u_message_from_id:=${callback_query_from_id[$uid]}} ]] ||
  4811. [[ ${u_message_from_id:=${inline_query_from_id[$uid]}} ]] ||
  4812. [[ ${u_message_from_id:=${chosen_inline_result_from_id[$uid]}} ]] ||
  4813. [[ ${u_message_from_id:=${poll_answer_user_id[$uid]}} ]]
  4814. [[ ${u_message_from_username:=${message_from_username[$uid]}} ]] ||
  4815. [[ ${u_message_from_username:=${edited_message_from_username[$uid]}} ]] ||
  4816. [[ ${u_message_from_username:=${callback_query_from_username[$uid]}} ]] ||
  4817. [[ ${u_message_from_username:=${inline_query_from_username[$uid]}} ]] ||
  4818. [[ ${u_message_from_username:=${chosen_inline_result_from_username[$uid]}} ]] ||
  4819. [[ ${u_message_from_username:=${poll_answer_user_username[$uid]}} ]]
  4820. [[ ${u_message_from_language_code:=${message_from_language_code[$uid]}} ]] ||
  4821. [[ ${u_message_from_language_code:=${edited_message_from_language_code[$uid]}} ]] ||
  4822. [[ ${u_message_from_language_code:=${callback_query_from_language_code[$uid]}} ]] ||
  4823. [[ ${u_message_from_language_code:=${inline_query_from_language_code[$uid]}} ]] ||
  4824. [[ ${u_message_from_language_code:=${chosen_inline_result_from_language_code[$uid]}} ]]
  4825. [[ ${u_message_chat_id:=${message_chat_id[$uid]}} ]] ||
  4826. [[ ${u_message_chat_id:=${edited_message_chat_id[$uid]}} ]] ||
  4827. [[ ${u_message_chat_id:=${callback_query_message_chat_id[$uid]}} ]] ||
  4828. [[ ${u_message_chat_id:=${channel_post_chat_id[$uid]}} ]] ||
  4829. [[ ${u_message_chat_id:=${edited_channel_post_chat_id[$uid]}} ]]
  4830. [[ ${u_message_chat_username:=${message_chat_username[$uid]}} ]] ||
  4831. [[ ${u_message_chat_username:=${edited_message_chat_username[$uid]}} ]] ||
  4832. [[ ${u_message_chat_username:=${callback_query_message_chat_username[$uid]}} ]]
  4833. [[ ${u_message_chat_type:=${message_chat_type[$uid]}} ]] ||
  4834. [[ ${u_message_chat_type:=${edited_message_chat_type[$uid]}} ]] ||
  4835. [[ ${u_message_chat_type:=${callback_query_message_chat_type[$uid]}} ]] ||
  4836. [[ ${u_message_chat_type:=${channel_post_chat_type[$uid]}} ]] ||
  4837. [[ ${u_message_chat_type:=${edited_channel_post_chat_type[$uid]}} ]]
  4838. [[ ${u_message_date:=${message_date[$uid]}} ]] ||
  4839. [[ ${u_message_date:=${edited_message_edit_date[$uid]}} ]] ||
  4840. [[ ${u_message_date:=${callback_query_message_date[$uid]}} ]] ||
  4841. [[ ${u_message_date:=${channel_post_date[$uid]}} ]] ||
  4842. [[ ${u_message_date:=${edited_channel_post_date[$uid]}} ]]
  4843. [[ ${u_message_entities_type:=${message_entities_type[$uid]}} ]] ||
  4844. [[ ${u_message_entities_type:=${edited_message_entities_type[$uid]}} ]] ||
  4845. [[ ${u_message_entities_type:=${callback_query_message_entities_type[$uid]}} ]] ||
  4846. [[ ${u_message_entities_type:=${channel_post_entities_type[$uid]}} ]] ||
  4847. [[ ${u_message_entities_type:=${edited_channel_post_entities_type[$uid]}} ]]
  4848. [[ ${u_message_mime_type:=${message_document_mime_type[$uid]}} ]] ||
  4849. [[ ${u_message_mime_type:=${message_video_mime_type[$uid]}} ]] ||
  4850. [[ ${u_message_mime_type:=${message_audio_mime_type[$uid]}} ]] ||
  4851. [[ ${u_message_mime_type:=${message_voice_mime_type[$uid]}} ]] ||
  4852. [[ ${u_message_mime_type:=${channel_post_document_mime_type[$uid]}} ]]
  4853. [[ ${u_message_author_signature:=${channel_post_author_signature[$uid]}} ]] ||
  4854. [[ ${u_message_author_signature:=${edited_channel_post_author_signature[$uid]}} ]]
  4855. # Regras
  4856. for ((i=0; i < _BOT_RULES_INDEX_; i++)); do
  4857. IFS=' ' read -ra args <<< $u_message_text
  4858. [[ ! ${_BOT_RULES_[$i:num_args]} || ${#args[@]} == @(${_BOT_RULES_[$i:num_args]}) ]] &&
  4859. [[ ! ${_BOT_RULES_[$i:command]} || ${u_message_text%% *} == @(${_BOT_RULES_[$i:command]})?(@${_BOT_INFO_[3]}) ]] &&
  4860. [[ ! ${_BOT_RULES_[$i:message_id]} || $u_message_id == @(${_BOT_RULES_[$i:message_id]}) ]] &&
  4861. [[ ! ${_BOT_RULES_[$i:is_bot]} || $u_message_from_is_bot == @(${_BOT_RULES_[$i:is_bot]}) ]] &&
  4862. [[ ! ${_BOT_RULES_[$i:user_id]} || $u_message_from_id == @(${_BOT_RULES_[$i:user_id]}) ]] &&
  4863. [[ ! ${_BOT_RULES_[$i:username]} || $u_message_from_username == @(${_BOT_RULES_[$i:username]}) ]] &&
  4864. [[ ! ${_BOT_RULES_[$i:language]} || $u_message_from_language_code == @(${_BOT_RULES_[$i:language]}) ]] &&
  4865. [[ ! ${_BOT_RULES_[$i:chat_id]} || $u_message_chat_id == @(${_BOT_RULES_[$i:chat_id]}) ]] &&
  4866. [[ ! ${_BOT_RULES_[$i:chat_name]} || $u_message_chat_username == @(${_BOT_RULES_[$i:chat_name]}) ]] &&
  4867. [[ ! ${_BOT_RULES_[$i:chat_type]} || $u_message_chat_type == @(${_BOT_RULES_[$i:chat_type]}) ]] &&
  4868. [[ ! ${_BOT_RULES_[$i:author_signature]} || $u_message_author_signature == @(${_BOT_RULES_[$i:author_signature]}) ]] &&
  4869. [[ ! ${_BOT_RULES_[$i:mime_type]} || $u_message_mime_type == @(${_BOT_RULES_[$i:mime_type]}) ]] &&
  4870. [[ ! ${_BOT_RULES_[$i:query_id]} || ${callback_query_id[$uid]} == @(${_BOT_RULES_[$i:query_id]}) ]] &&
  4871. [[ ! ${_BOT_RULES_[$i:query_data]} || ${callback_query_data[$uid]} == @(${_BOT_RULES_[$i:query_data]}) ]] &&
  4872. [[ ! ${_BOT_RULES_[$i:weekday]} || $(printf '%(%u)T' $u_message_date) == @(${_BOT_RULES_[$i:weekday]}) ]] &&
  4873. [[ ! ${_BOT_RULES_[$i:text]} || $u_message_text =~ ${_BOT_RULES_[$i:text]} ]] || continue
  4874. # Extrai os arquivos do conjunto negado. Caso esteja ausente
  4875. # define a expressão padrão.
  4876. # Captura os grupos contidos no padrão, separando o
  4877. # operador de negação '!' (se presente) para determinar o
  4878. # tratamento de valição do intervalo.
  4879. #
  4880. # Exemplo 1:
  4881. #
  4882. # BASH_REMATCH[2]
  4883. # __________|__________
  4884. # | |
  4885. # !(12:00-13:00,15:00-17:00)
  4886. # |
  4887. # |_ BASH_REMATCH[1]
  4888. #
  4889. re='^(!)\(([^)]+)\)$'
  4890. [[ ${_BOT_RULES_[$i:auth_file]} =~ $re ]]
  4891. match=${BASH_REMATCH[2]:-${_BOT_RULES_[$i:auth_file]}}
  4892. for file in ${match//|/ }; do
  4893. # Testa acesso ao arquivo.
  4894. if ! [[ -f "$file" && -r "$file" ]]; then
  4895. MessageError API "'$file' $_ERR_FILE_NOT_FOUND_" "${_BOT_RULES_[$i:name]}" '[-T, --auth_file]'
  4896. fi
  4897. # Lê os usuários removendo os comentários complementares
  4898. # e ignora a linha prefixada com hashtag '#'.
  4899. while read -r line; do
  4900. user=${line%%*( )#*}
  4901. [[ $user != *( )#* ]] &&
  4902. [[ $user == $u_message_from_id || $user == $u_message_from_username ]] && break 2
  4903. done < "$file"
  4904. done
  4905. ((${BASH_REMATCH[1]} $?)) && continue
  4906. for msgstatus in ${_BOT_RULES_[$i:message_status]//|/ }; do
  4907. [[ $msgstatus == pinned && ${message_pinned_message_message_id[$uid]:-${channel_post_pinned_message_message_id[$uid]}} ]] ||
  4908. [[ $msgstatus == edited && ${edited_message_message_id[$uid]:-${edited_channel_post_message_id[$uid]}} ]] ||
  4909. [[ $msgstatus == forwarded && ${message_forward_from_id[$uid]:-${channel_post_forward_from_chat_id[$uid]}} ]] ||
  4910. [[ $msgstatus == reply && ${message_reply_to_message_message_id[$uid]:-${channel_post_reply_to_message_message_id[$uid]}} ]] ||
  4911. [[ $msgstatus == callback && ${callback_query_message_message_id[$uid]} ]] ||
  4912. [[ $msgstatus == inline && ${inline_query_id[$uid]} ]] ||
  4913. [[ $msgstatus == chosen && ${chosen_inline_result_result_id[$uid]} ]] ||
  4914. [[ $msgstatus == poll && ${poll_answer_poll_id[$uid]} ]] && break
  4915. done
  4916. (($?)) && continue
  4917. for ent in ${_BOT_RULES_[$i:entities_type]//|/ }; do
  4918. [[ $ent == @(${u_message_entities_type//$_BOT_DELM_/|}) ]] && break
  4919. done
  4920. (($?)) && continue
  4921. for mem in ${_BOT_RULES_[$i:chat_member]//|/ }; do
  4922. [[ $mem == new && ${message_new_chat_member_id[$uid]} ]] ||
  4923. [[ $mem == left && ${message_left_chat_member_id[$uid]} ]] && break
  4924. done
  4925. (($?)) && continue
  4926. for type in ${_BOT_RULES_[$i:file_type]//|/ }; do
  4927. [[ $type == document && ${message_document_file_id[$uid]:-${channel_post_document_file_id[$uid]}} &&
  4928. ! ${message_document_thumb_file_id[$uid]:-${channel_post_document_thumb_file_id[$uid]}} ]] ||
  4929. [[ $type == gif && ${message_document_file_id[$uid]:-${channel_post_document_file_id[$uid]}} &&
  4930. ${message_document_thumb_file_id[$uid]:-${channel_post_document_thumb_file_id[$uid]}} ]] ||
  4931. [[ $type == photo && ${message_photo_file_id[$uid]:-${channel_post_photo_file_id[$uid]}} ]] ||
  4932. [[ $type == sticker && ${message_sticker_file_id[$uid]:-${channel_post_sticker_file_id[$uid]}} ]] ||
  4933. [[ $type == video && ${message_video_file_id[$uid]:-${channel_post_video_file_id[$uid]}} ]] ||
  4934. [[ $type == audio && ${message_audio_file_id[$uid]:-${channel_post_audio_file_id[$uid]}} ]] ||
  4935. [[ $type == voice && ${message_voice_file_id[$uid]:-${channel_post_voice_file_id[$uid]}} ]] ||
  4936. [[ $type == contact && ${message_contact_user_id[$uid]:-${channel_post_contact_user_id[$uid]}} ]] ||
  4937. [[ $type == location && ${message_location_latitude[$uid]:-${channel_post_location_latitude[$uid]}} ]] && break
  4938. done
  4939. (($?)) && continue
  4940. [[ ${_BOT_RULES_[$i:time]} =~ $re ]]
  4941. match=${BASH_REMATCH[2]:-${_BOT_RULES_[$i:time]}}
  4942. for tm in ${match//|/ }; do
  4943. IFS='-' read stime etime <<< $tm
  4944. printf -v ctime '%(%H:%M)T' $u_message_date
  4945. [[ $ctime == @($stime|$etime) ]] ||
  4946. [[ $ctime > $stime && $ctime < $etime ]] && break
  4947. done
  4948. ((${BASH_REMATCH[1]} $?)) && continue
  4949. [[ ${_BOT_RULES_[$i:date]} =~ $re ]]
  4950. match=${BASH_REMATCH[2]:-${_BOT_RULES_[$i:date]}}
  4951. for dt in ${match//|/ }; do
  4952. IFS='-' read sdate edate <<< $dt
  4953. IFS='/' read -a sdate <<< $sdate
  4954. IFS='/' read -a edate <<< $edate
  4955. sdate=${sdate[2]}/${sdate[1]}/${sdate[0]}
  4956. edate=${edate[2]}/${edate[1]}/${edate[0]}
  4957. printf -v cdate '%(%Y/%m/%d)T' $u_message_date
  4958. [[ $cdate == @($sdate|$edate) ]] ||
  4959. [[ $cdate > $sdate && $cdate < $edate ]] && break
  4960. done
  4961. ((${BASH_REMATCH[1]} $?)) && continue
  4962. if [[ ${_BOT_RULES_[$i:user_status]} ]]; then
  4963. case $_BOT_TYPE_RETURN_ in
  4964. value)
  4965. out=$(ShellBot.getChatMember --chat_id $u_message_chat_id \
  4966. --user_id $u_message_from_id 2>/dev/null)
  4967. IFS=$_BOT_DELM_ read -a out <<< $out
  4968. [[ ${out[2]} == true ]]
  4969. status=${out[$(($? ? 6 : 5))]}
  4970. ;;
  4971. json)
  4972. out=$(ShellBot.getChatMember --chat_id $u_message_chat_id \
  4973. --user_id $u_message_from_id 2>/dev/null)
  4974. status=$(Json '.result.status' $out)
  4975. ;;
  4976. map)
  4977. ShellBot.getChatMember --chat_id $u_message_chat_id \
  4978. --user_id $u_message_from_id &>/dev/null
  4979. status=${return[status]}
  4980. ;;
  4981. esac
  4982. [[ $status == @(${_BOT_RULES_[$i:user_status]}) ]] || continue
  4983. fi
  4984. # Monitor
  4985. [[ $_BOT_MONITOR_ ]] && printf '[%s]: %s: %s: %s: %s: %s: %s: %s: %s: %s: %s\n' \
  4986. "${FUNCNAME}" \
  4987. "$((uid+1))" \
  4988. "$(printf '%(%d/%m/%Y %H:%M:%S)T' ${u_message_date})" \
  4989. "${u_message_chat_type}" \
  4990. "${u_message_chat_username:--}" \
  4991. "${u_message_from_username:--}" \
  4992. "${_BOT_RULES_[$i:source]}" \
  4993. "${_BOT_RULES_[$i:line]}" \
  4994. "${_BOT_RULES_[$i:name]}" \
  4995. "${_BOT_RULES_[$i:action]:--}" \
  4996. "${_BOT_RULES_[$i:exec]:--}"
  4997. # Log
  4998. [[ $_BOT_LOG_FILE_ ]] && printf '%s: %s: %s: %s: %s: %s: %s\n' \
  4999. "$(printf '%(%d/%m/%Y %H:%M:%S)T')" \
  5000. "${FUNCNAME}" \
  5001. "${_BOT_RULES_[$i:source]}" \
  5002. "${_BOT_RULES_[$i:line]}" \
  5003. "${_BOT_RULES_[$i:name]}" \
  5004. "${_BOT_RULES_[$i:action]:--}" \
  5005. "${_BOT_RULES_[$i:exec]:--}" >> "$_BOT_LOG_FILE_"
  5006. # Anexa tipo da ação. (se presente)
  5007. if [[ ${_BOT_RULES_[$i:bot_action]} ]]; then
  5008. ShellBot.sendChatAction --chat_id $u_message_chat_id --action ${_BOT_RULES_[$i:bot_action]} &>/dev/null
  5009. fi
  5010. if [[ ${_BOT_RULES_[$i:bot_reply_message]} ]]; then
  5011. ShellBot.sendMessage --chat_id $u_message_chat_id \
  5012. --reply_to_message_id $u_message_id \
  5013. --text "$(FlagConv $uid "${_BOT_RULES_[$i:bot_reply_message]}")" \
  5014. ${_BOT_RULES_[$i:bot_reply_markup]:+--reply_markup "${_BOT_RULES_[$i:bot_reply_markup]}"} \
  5015. ${_BOT_RULES_[$i:bot_parse_mode]:+--parse_mode ${_BOT_RULES_[$i:bot_parse_mode]}} &>/dev/null
  5016. fi
  5017. if [[ ${_BOT_RULES_[$i:bot_send_message]} ]]; then
  5018. ShellBot.sendMessage --chat_id $u_message_chat_id \
  5019. --text "$(FlagConv $uid "${_BOT_RULES_[$i:bot_send_message]}")" \
  5020. ${_BOT_RULES_[$i:bot_reply_markup]:+--reply_markup "${_BOT_RULES_[$i:bot_reply_markup]}"} \
  5021. ${_BOT_RULES_[$i:bot_parse_mode]:+--parse_mode ${_BOT_RULES_[$i:bot_parse_mode]}} &>/dev/null
  5022. fi
  5023. for fwid in ${_BOT_RULES_[$i:bot_forward_message]//|/ }; do
  5024. ShellBot.forwardMessage --chat_id $fwid \
  5025. --from_chat_id $u_message_chat_id \
  5026. --message_id $u_message_id &>/dev/null
  5027. done
  5028. # Chama a função passando os argumentos posicionais. (se existir)
  5029. ${_BOT_RULES_[$i:action]:+${_BOT_RULES_[$i:action]} ${_BOT_RULES_[$i:action_args]:-${args[*]}}}
  5030. # Executa a linha de comando e salva o retorno.
  5031. stdout=${_BOT_RULES_[$i:exec]:+$(set -- ${args[*]}; eval $(FlagConv $uid "${_BOT_RULES_[$i:exec]}") 2>&1)}
  5032. while [[ $stdout ]]; do
  5033. ShellBot.sendMessage --chat_id $u_message_chat_id \
  5034. --reply_to_message_id $u_message_id \
  5035. --text "${stdout:0:4096}" &>/dev/null
  5036. # Atualiza o buffer de saída.
  5037. stdout=${stdout:4096}
  5038. # Reenvia ação se ainda houver dados.
  5039. if [[ ${_BOT_RULES_[$i:bot_action]} && $stdout ]]; then
  5040. ShellBot.sendChatAction --chat_id $u_message_chat_id --action ${_BOT_RULES_[$i:bot_action]} &>/dev/null
  5041. fi
  5042. done
  5043. [[ ${_BOT_RULES_[$i:continue]} ]] || return 0
  5044. done
  5045. return 1
  5046. }
  5047. ShellBot.getUpdates()
  5048. {
  5049. local total_keys offset limit timeout allowed_updates jq_obj
  5050. local vet val var obj oldv bar vars vals i
  5051. # Define os parâmetros da função
  5052. local param=$(getopt --name "$FUNCNAME" \
  5053. --options 'o:l:t:a:' \
  5054. --longoptions 'offset:,
  5055. limit:,
  5056. timeout:,
  5057. allowed_updates:' \
  5058. -- "$@")
  5059. eval set -- "$param"
  5060. while :
  5061. do
  5062. case $1 in
  5063. -o|--offset)
  5064. CheckArgType int "$1" "$2"
  5065. offset=$2
  5066. shift 2
  5067. ;;
  5068. -l|--limit)
  5069. CheckArgType int "$1" "$2"
  5070. limit=$2
  5071. shift 2
  5072. ;;
  5073. -t|--timeout)
  5074. CheckArgType int "$1" "$2"
  5075. timeout=$2
  5076. shift 2
  5077. ;;
  5078. -a|--allowed_updates)
  5079. allowed_updates=$2
  5080. shift 2
  5081. ;;
  5082. --)
  5083. # Se não houver mais parâmetros
  5084. shift
  5085. break
  5086. ;;
  5087. esac
  5088. done
  5089. # Seta os parâmetros
  5090. jq_obj=$(curl $_CURL_OPT_ POST $_API_TELEGRAM_/${FUNCNAME#*.} \
  5091. ${offset:+-d offset="$offset"} \
  5092. ${limit:+-d limit="$limit"} \
  5093. ${timeout:+-d timeout="$timeout"} \
  5094. ${allowed_updates:+-d allowed_updates="$allowed_updates"})
  5095. # Limpa as variáveis inicializadas.
  5096. unset $_VAR_INIT_; _VAR_INIT_=
  5097. # Se há atualizações.
  5098. [[ $(jq -r '.result|length' <<< $jq_obj) -eq 0 ]] && return 0
  5099. # Se o método 'ShellBot.getUpdates' for invocado a partir de um subshell,
  5100. # as atualizações são retornadas em um estrutura de dados json, o método
  5101. # é finalizado e variáveis não são inicializadas.
  5102. [[ $BASH_SUBSHELL -gt 0 ]] && { echo "$jq_obj"; return 0; }
  5103. if [[ $_BOT_MONITOR_ ]]; then
  5104. printf -v bar '=%.s' {1..50}
  5105. printf "$bar\nData: %(%d/%m/%Y %T)T\n"
  5106. printf 'Script: %s\nBot (nome): %s\nBot (usuario): %s\nBot (id): %s\n' \
  5107. "${_BOT_SCRIPT_}" \
  5108. "${_BOT_INFO_[2]}" \
  5109. "${_BOT_INFO_[3]}" \
  5110. "${_BOT_INFO_[1]}"
  5111. fi
  5112. mapfile -t vars <<< $(GetAllKeys "$jq_obj")
  5113. mapfile -t vals <<< $(GetAllValues "$jq_obj")
  5114. for i in ${!vars[@]}; do
  5115. [[ ${vars[$i]} =~ [0-9]+ ]]
  5116. vet=${BASH_REMATCH:-0}
  5117. var=${vars[$i]//[0-9\[\]]/}
  5118. var=${var#result.}
  5119. var=${var//./_}
  5120. declare -g $var
  5121. local -n byref=$var # ponteiro
  5122. val=${vals[$i]}
  5123. val=${val#\"}
  5124. val=${val%\"}
  5125. byref[$vet]+=${byref[$vet]:+$_BOT_DELM_}${val}
  5126. if [[ $_BOT_MONITOR_ ]]; then
  5127. [[ $vet -ne ${oldv:--1} ]] && printf "$bar\nMensagem: %d\n$bar\n" $((vet+1))
  5128. printf "[%s]: %s = '%s'\n" "$FUNCNAME" "$var" "$val"
  5129. oldv=$vet
  5130. fi
  5131. unset -n byref
  5132. [[ $var != @(${_VAR_INIT_// /|}) ]] && _VAR_INIT_=${_VAR_INIT_:+$_VAR_INIT_ }${var}
  5133. done
  5134. # Log (thread)
  5135. [[ $_BOT_LOG_FILE_ ]] && CreateLog "${#update_id[@]}" "$jq_obj"
  5136. # Status
  5137. return $?
  5138. }
  5139. # Bot métodos (somente leitura)
  5140. readonly -f ShellBot.token \
  5141. ShellBot.id \
  5142. ShellBot.username \
  5143. ShellBot.first_name \
  5144. ShellBot.getConfig \
  5145. ShellBot.regHandleFunction \
  5146. ShellBot.regHandleExec \
  5147. ShellBot.watchHandle \
  5148. ShellBot.ListUpdates \
  5149. ShellBot.TotalUpdates \
  5150. ShellBot.OffsetEnd \
  5151. ShellBot.OffsetNext \
  5152. ShellBot.getMe \
  5153. ShellBot.getWebhookInfo \
  5154. ShellBot.deleteWebhook \
  5155. ShellBot.setWebhook \
  5156. ShellBot.init \
  5157. ShellBot.ReplyKeyboardMarkup \
  5158. ShellBot.ForceReply \
  5159. ShellBot.ReplyKeyboardRemove \
  5160. ShellBot.KeyboardButton \
  5161. ShellBot.sendMessage \
  5162. ShellBot.forwardMessage \
  5163. ShellBot.sendPhoto \
  5164. ShellBot.sendAudio \
  5165. ShellBot.sendDocument \
  5166. ShellBot.sendSticker \
  5167. ShellBot.sendVideo \
  5168. ShellBot.sendVideoNote \
  5169. ShellBot.sendVoice \
  5170. ShellBot.sendLocation \
  5171. ShellBot.sendVenue \
  5172. ShellBot.sendContact \
  5173. ShellBot.sendChatAction \
  5174. ShellBot.getUserProfilePhotos \
  5175. ShellBot.getFile \
  5176. ShellBot.kickChatMember \
  5177. ShellBot.leaveChat \
  5178. ShellBot.unbanChatMember \
  5179. ShellBot.getChat \
  5180. ShellBot.getChatAdministrators \
  5181. ShellBot.getChatMembersCount \
  5182. ShellBot.getChatMember \
  5183. ShellBot.editMessageText \
  5184. ShellBot.editMessageCaption \
  5185. ShellBot.editMessageReplyMarkup \
  5186. ShellBot.InlineKeyboardMarkup \
  5187. ShellBot.InlineKeyboardButton \
  5188. ShellBot.answerCallbackQuery \
  5189. ShellBot.deleteMessage \
  5190. ShellBot.exportChatInviteLink \
  5191. ShellBot.setChatPhoto \
  5192. ShellBot.deleteChatPhoto \
  5193. ShellBot.setChatTitle \
  5194. ShellBot.setChatDescription \
  5195. ShellBot.pinChatMessage \
  5196. ShellBot.unpinChatMessage \
  5197. ShellBot.promoteChatMember \
  5198. ShellBot.restrictChatMember \
  5199. ShellBot.getStickerSet \
  5200. ShellBot.uploadStickerFile \
  5201. ShellBot.createNewStickerSet \
  5202. ShellBot.addStickerToSet \
  5203. ShellBot.setStickerPositionInSet \
  5204. ShellBot.deleteStickerFromSet \
  5205. ShellBot.stickerMaskPosition \
  5206. ShellBot.downloadFile \
  5207. ShellBot.editMessageLiveLocation \
  5208. ShellBot.stopMessageLiveLocation \
  5209. ShellBot.setChatStickerSet \
  5210. ShellBot.deleteChatStickerSet \
  5211. ShellBot.sendMediaGroup \
  5212. ShellBot.editMessageMedia \
  5213. ShellBot.inputMedia \
  5214. ShellBot.sendAnimation \
  5215. ShellBot.answerInlineQuery \
  5216. ShellBot.InlineQueryResult \
  5217. ShellBot.InputMessageContent \
  5218. ShellBot.ChatPermissions \
  5219. ShellBot.setChatPermissions \
  5220. ShellBot.setChatAdministratorCustomTitle \
  5221. ShellBot.sendPoll \
  5222. ShellBot.KeyboardButtonPollType \
  5223. ShellBot.sendDice \
  5224. ShellBot.getMyCommands \
  5225. ShellBot.setMyCommands \
  5226. ShellBot.BotCommand \
  5227. ShellBot.setMessageRules \
  5228. ShellBot.manageRules \
  5229. ShellBot.getUpdates
  5230. offset=${_BOT_FLUSH_:+$(FlushOffset)} # flush
  5231. printf -v jq_obj '{"token":"%s","id":%d,"first_name":"%s","username":"%s","offset_start":%d,"offset_end":%d}' \
  5232. "${_BOT_INFO_[0]}" \
  5233. "${_BOT_INFO_[1]}" \
  5234. "${_BOT_INFO_[2]}" \
  5235. "${_BOT_INFO_[3]}" \
  5236. "${offset%|*}" \
  5237. "${offset#*|}"
  5238. # Retorna informações do bot.
  5239. MethodReturn "$jq_obj"
  5240. return $?
  5241. }
  5242. # Funções (somente leitura)
  5243. readonly -f MessageError \
  5244. Json \
  5245. FlushOffset \
  5246. CreateUnitService \
  5247. GetAllKeys \
  5248. GetAllValues \
  5249. SetDelmValues \
  5250. MethodReturn \
  5251. CheckArgType \
  5252. CreateLog \
  5253. FlagConv
  5254. # /* SHELLBOT */