ShellBot.sh 169 KB

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