50_TelegramBot.pm 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297
  1. ##############################################################################
  2. #
  3. # 50_TelegramBot.pm
  4. #
  5. # This file is part of Fhem.
  6. #
  7. # Fhem is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Fhem is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Fhem. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. #
  22. # TelegramBot (c) Johannes Viegener / https://github.com/viegener/Telegram-fhem
  23. #
  24. # This module handles receiving and sending messages to the messaging service telegram (see https://telegram.org/)
  25. # TelegramBot is making use of the Telegrom Bot API (see https://core.telegram.org/bots and https://core.telegram.org/bots/api)
  26. # For using it with fhem an telegram BOT API key is needed! --> see https://core.telegram.org/bots/api#authorizing-your-bot
  27. #
  28. # Discussed in FHEM Forum: https://forum.fhem.de/index.php/topic,38328.0.html
  29. #
  30. # $Id: 50_TelegramBot.pm 12874 2016-12-25 14:15:59Z viegener $
  31. #
  32. ##############################################################################
  33. # 0.0 2015-09-16 Started
  34. # 1.0 2015-10-17 Initial SVN-Version
  35. #
  36. # INTERNAL: sendIt allows providing a keyboard json
  37. # Favorites sent as keyboard
  38. # allow sending to contacts not in the contacts list (by giving id of user)
  39. # added comment on save (statefile) for correct operation in documentation
  40. # contacts changed on new contacts found
  41. # saveStateOnContactChange attribute to disaloow statefile save on contact change
  42. # writeStatefile on contact change
  43. # make contact restore simpler --> whenever new contact found write all contacts into log with loglevel 1
  44. # Do not allow shutdown as command for execution
  45. # ret from command handlings logged
  46. # maxReturnSize for command results
  47. # limit sentMsgTxt internal to 1000 chars (even if longer texts are sent)
  48. # contact reading now written in contactsupdate before statefile written
  49. # documentation corrected - forum#msg350873
  50. # cleanup on comments
  51. # removed old version changes to history.txt
  52. # add digest readings for error
  53. # attribute to reduce logging on updatepoll errors - pollingVerbose:0_None,1_Digest,2_Log - (no log, default digest log daily, log every issue)
  54. # documentation for pollingverbose
  55. # reset / log polling status also in case of no error
  56. # removed remark on timeout of 20sec
  57. # LastCommands returns keyboard with commands
  58. # added send / image command for compatibility with yowsup
  59. # image not in cmd list to avoid this being first option
  60. # FIX: Keyboard removed after fac execution
  61. # Do not use contacts from msg since this might be NON-Telegram contact
  62. # cmdReturnEmptyResult - to suppress empty results from command execution
  63. # prev... Readings do not trigger events (to reduce log content)
  64. # Contacts reading only changed if string is not equal
  65. # Need to replace \n again with Chr10 - linefeed due to a telegram change - FORUM #msg363825
  66. # 1.1 2015-11-24 keyboards added, log changes and multiple smaller enhancements
  67. #
  68. # Prepared for allowing multiple contacts being given for msg/image commands
  69. # Prepare for defaultpeer specifying multiple peers as a list
  70. # Allow multiple peers specified for send/msg/image etc
  71. # Remove deprecated commands messageTo sendImageTo sendPhotoTo
  72. # Minor fixes on lineendings for cmd results and log messages
  73. # pollingVerbose attribute checked on set
  74. # allowUnknownContacts attribute added default 1
  75. # 1.2 2015-12-20 multiple contacts for send etc/removed depreacted messageTo,sendImageTo,sendPhotoTo/allowunknowncontacts
  76. #
  77. # modified cmd handling in preparation for alias (and more efficient)
  78. # allow alias to be defined for favorites: /aliasx=cmdx;
  79. # docu for alias
  80. # correction for keyboard (no abbruch)
  81. # added sentMsgId on sentMsgs
  82. # Also set sentMsg Id and result in Readings (when finished)
  83. # add docu for new readings on sentMsg
  84. # fix for checkCmdKeyword to not sent unauthorized on every message
  85. # avoid unauthorized messages to be sent multiple times
  86. # added sendVoice for Voice messages
  87. # added sendMedia / sendDocument for arbitrary media files
  88. # specified a longer description in the doc for gaining telegramBot tokens
  89. # fix: allowunknowncontacts for known contacts
  90. # 1.3 2016-01-02 alias for commands, new readings, support for sending media files plus fixes
  91. #
  92. # receiving media files is possible --> file id is stored in msgFileId / msgText starting with "received..."
  93. # additional info from message (type, name, etc) is contained in msgText
  94. # added get function to return url for file ids on media messages "urlForFile"
  95. # writes returned url into internal: fileUrl
  96. # INT: switch command result sending to direct _sendIt call
  97. # forum msg396189
  98. # favorite commands can be used also to send images back if the result of the command is an image
  99. # e.g. { plotAsPng('SVG_FileLog_something') } --> returns PNG if used in favorite the result will be send as photo
  100. # Forbid all commands starting with shutdown
  101. # Recognize MP3 also with ID3v2 tag (2.2 / 2.3 / 2.4)
  102. # 1.4 2016-02-07 receive media files, send media files directly from parameter (PNG, JPG, MP3, PDF, etc)
  103. # Retry-Preparation: store arsg in param array / delete in case of error before sending
  104. # added maxRetries for Retry send with wait time 1=10s / 2=100s / 3=1000s ~ 17min / 4=10000s ~ 3h / 5=100000s ~ 30h
  105. # tested Retry of send in case of errors (after finalizing message)
  106. # attr returns text to avoid automatic attr setting in fhem.pl
  107. # documented maxRetries
  108. # fixed attributehandling to normalize and correct attribute values
  109. # fix for perl "keys on reference is experimental" forum#msg417968
  110. # allow confirmation for favorite commands by prefixing with question ark (?)
  111. # fix contact update
  112. # 1.5 2016-03-19 retry for send / confirmation
  113. # supergroups added now also to contacts
  114. # fix for first name of contacts undefined
  115. # remove stale/duplicate contacts (based on username) on update of contacts (supergroups get new ids)
  116. # Allow localization outward facing messages -> templates with replacements (German as default)
  117. # New attributes for visible telegram responses:
  118. # textResponseConfirm, textResponseFavorites, textResponseCommands, textResponseResult, textResponseUnauthorized
  119. # descriptions for favorites can be specified (enclosed in [])
  120. # descriptions are shown in favorite list and confirmation dialogue
  121. # texts are converted to UTF8 also for keyboards
  122. # favorite list corrected
  123. # 1.6 2016-04-08 text customization for replies/messages and favorite descriptions
  124. # Fix: contact handling failed (/ in contact names ??)
  125. # Reply keyboards also for sendVoice/sendDocument ect
  126. # reply msg id in sendit - new set cmd reply
  127. # Fix: reset also removes retry timer
  128. # TEMP: SNAME in hash is needed for allowed (SNAME reflects if the TCPServer device name)
  129. # Remove unnecessary attribute setters
  130. # added allowedCommands and doc (with modification of allowed_... device)
  131. # allowedCommands only modified on the allowed_... device
  132. # 1.7 2016-05-05 reply set command / allowedCommands as restriction
  133. # fix for addPar (Caption) on photos in SendIt
  134. # fix for contact list UTF8 encoding on restart
  135. # fix: encoding problem in some environments leading to wrong length calc in httputils (msg457443)
  136. #
  137. # fix: Favorite description without alias name was not parsed correctly
  138. # fix: Favorite alias only handled if really contains more than the /
  139. #
  140. # Complete rework of JSON/UTF8 code to solve timeout and encoding issues
  141. # Add \t for messages texts - will be a single space in the message
  142. # 1.8 2016-05-05 UNicode / Umlaute handling changed, \t added
  143. # Add unescaping of filenames for send - this allows also spaces (%20)
  144. # Attribut filenameUrlEscape allows switching on urlescaping for filenames
  145. # Caption also for documents
  146. # Location and venue received as message type
  147. # sendLocation command
  148. # add attribute for timeout on do execution (similar to polling) --> cmdTimeout - timeout in do_params / Forum msg480844
  149. # fix for timeout on sent and addtl log - forum msg497239
  150. # change log levels for deep encoding
  151. # add summary for fhem commandref
  152. # 1.9 2016-10-06 urlescaped filenames / location send-receive / timeout for send
  153. # fix: multibot environment - localize global hashes
  154. # markup - per Attribute "parseModeSend" - None / InMsg / Markdown / HTML
  155. # Log unnknown contacts and messages - msg505210
  156. # replykeyboardhide - test - msg505012 - not possible to remove keyboard
  157. # edit_message - msg504659 - new command msgEdit
  158. # msgEdit documented
  159. # 2.0 2016-10-19 multibot support / markup on send text / msgEdit
  160. # disable_web_page_preview - attribut webPagePreview - msg506924
  161. # log other messages in getupdate
  162. # add new get command "update" for single update poll
  163. # new cmd for forcing a msg reply - msgForceReply
  164. # add readings for reply msg id: msgReplyMsgId
  165. # documemt: msgForceReply, msgReplyMsgId
  166. # diable attribute to stop polling
  167. # keyboards through [] in message command(s) -> no changed to () to avoid issues
  168. # send incomplete keyboards as message instead of error
  169. # Add | as separator for keys
  170. # documentation alignment - more consistent usage of peer (instead of user)
  171. # Keyboards in () istead of []
  172. # added callback being retrieved in updates
  173. # allow inline keyboards sent - new command inline keys titel:data
  174. # allow answer to callback (id must be given / text is optional)
  175. # document inline / answer
  176. # cleaned up recommendations for cmdKeyword etc
  177. # corrections to doc and code - msg540802
  178. # command names for answer / inline -changed to-> queryInline, queryAnswer - msg540802
  179. # attribute for automatic answer - eval set logic - queryAnswerText
  180. # FIX: trim $ret avoiding empty msg error from telegram in command response
  181. # FIX: trim $ret avoiding empty msg error from telegram also with control characters in 2 chars
  182. # rename callback... readings to query... for consistency
  183. # value 0 for queryAnswerText means no text sent but still answer
  184. # FIX: corrected documentation - unbalanced li
  185. # Run set magic on all comands before execution
  186. # add new reading sentMsgPeerId
  187. # add edit message for inline keyboards?
  188. # document queryEditInline
  189. # "0" message still sent on queryanswer
  190. # 2.1 2016-12-25 msgForceReply, disable, keyboards in messages, inline keyboards and dialogs
  191. #
  192. #
  193. #
  194. #
  195. #
  196. ##############################################################################
  197. # TASKS
  198. #
  199. # allow setting one time keyboard through set - how to connect to set?
  200. #
  201. #
  202. #
  203. #
  204. ##############################################################################
  205. # Ideas / Future
  206. #
  207. # Idea: allow literals in msges: U+27F2 - \xe2\x9f\xb2 / Forum msg458794
  208. #
  209. ##############################################################################
  210. package main;
  211. use strict;
  212. use warnings;
  213. #use HttpUtils;
  214. use utf8;
  215. use Encode;
  216. # JSON:XS is used here normally
  217. use JSON;
  218. use File::Basename;
  219. use URI::Escape;
  220. use Scalar::Util qw(reftype looks_like_number);
  221. #########################
  222. # Forward declaration
  223. sub TelegramBot_Define($$);
  224. sub TelegramBot_Undef($$);
  225. sub TelegramBot_Set($@);
  226. sub TelegramBot_Get($@);
  227. sub TelegramBot_Callback($$$);
  228. sub TelegramBot_SendIt($$$$$;$$);
  229. sub TelegramBot_checkAllowedPeer($$$);
  230. sub TelegramBot_SplitFavoriteDef($$);
  231. sub TelegramBot_GetUTF8Back( $ );
  232. sub TelegramBot_PutToUTF8( $ );
  233. sub TelegramBot_AttrNum($$$);
  234. sub TelegramBot_MakeKeyboard($$$@);
  235. #########################
  236. # Globals
  237. my %sets = (
  238. "_msg" => "textField",
  239. "message" => "textField",
  240. "msg" => "textField",
  241. "send" => "textField",
  242. "msgEdit" => "textField",
  243. "msgForceReply" => "textField",
  244. "queryAnswer" => "textField",
  245. "queryInline" => "textField",
  246. "queryEditInline" => "textField",
  247. "sendImage" => "textField",
  248. "sendPhoto" => "textField",
  249. "sendDocument" => "textField",
  250. "sendMedia" => "textField",
  251. "sendVoice" => "textField",
  252. "sendLocation" => "textField",
  253. "replaceContacts" => "textField",
  254. "reset" => undef,
  255. "reply" => "textField",
  256. "zDebug" => "textField"
  257. );
  258. my %deprecatedsets = (
  259. "image" => "textField",
  260. "sendPhoto" => "textField",
  261. );
  262. my %gets = (
  263. "urlForFile" => "textField",
  264. "update" => undef
  265. );
  266. my $TelegramBot_header = "agent: TelegramBot/1.0\r\nUser-Agent: TelegramBot/1.0\r\nAccept: application/json\r\nAccept-Charset: utf-8";
  267. ##############################################################################
  268. ##############################################################################
  269. ##
  270. ## Module operation
  271. ##
  272. ##############################################################################
  273. ##############################################################################
  274. #####################################
  275. # Initialize is called from fhem.pl after loading the module
  276. # define functions and attributed for the module and corresponding devices
  277. sub TelegramBot_Initialize($) {
  278. my ($hash) = @_;
  279. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  280. $hash->{DefFn} = "TelegramBot_Define";
  281. $hash->{UndefFn} = "TelegramBot_Undef";
  282. $hash->{StateFn} = "TelegramBot_State";
  283. $hash->{GetFn} = "TelegramBot_Get";
  284. $hash->{SetFn} = "TelegramBot_Set";
  285. $hash->{AttrFn} = "TelegramBot_Attr";
  286. $hash->{AttrList} = "defaultPeer defaultPeerCopy:0,1 cmdKeyword cmdSentCommands favorites:textField-long cmdFavorites cmdRestrictedPeer ". "cmdTriggerOnly:0,1 saveStateOnContactChange:1,0 maxFileSize maxReturnSize cmdReturnEmptyResult:1,0 pollingVerbose:1_Digest,2_Log,0_None ".
  287. "cmdTimeout pollingTimeout disable queryAnswerText:textField ".
  288. "allowUnknownContacts:1,0 textResponseConfirm:textField textResponseCommands:textField allowedCommands filenameUrlEscape:1,0 ".
  289. "textResponseFavorites:textField textResponseResult:textField textResponseUnauthorized:textField ".
  290. "parseModeSend:0_None,1_Markdown,2_HTML,3_InMsg webPagePreview:1,0 ".
  291. " maxRetries:0,1,2,3,4,5 ".$readingFnAttributes;
  292. }
  293. ######################################
  294. # Define function is called for actually defining a device of the corresponding module
  295. # For TelegramBot this is mainly API id for the bot
  296. # data will be stored in the hash of the device as internals
  297. #
  298. sub TelegramBot_Define($$) {
  299. my ($hash, $def) = @_;
  300. my @a = split("[ \t]+", $def);
  301. my $name = $hash->{NAME};
  302. Log3 $name, 3, "TelegramBot_Define $name: called ";
  303. my $errmsg = '';
  304. # Check parameter(s)
  305. if( int(@a) != 3 ) {
  306. $errmsg = "syntax error: define <name> TelegramBot <APIid> ";
  307. Log3 $name, 1, "TelegramBot $name: " . $errmsg;
  308. return $errmsg;
  309. }
  310. if ( $a[2] =~ /^([[:alnum:]]|[-:_])+[[:alnum:]]+([[:alnum:]]|[-:_])+$/ ) {
  311. $hash->{Token} = $a[2];
  312. } else {
  313. $errmsg = "specify valid API token containing only alphanumeric characters and -: characters: define <name> TelegramBot <APItoken> ";
  314. Log3 $name, 1, "TelegramBot $name: " . $errmsg;
  315. return $errmsg;
  316. }
  317. my $ret;
  318. $hash->{TYPE} = "TelegramBot";
  319. $hash->{STATE} = "Undefined";
  320. $hash->{WAIT} = 0;
  321. $hash->{FAILS} = 0;
  322. $hash->{UPDATER} = 0;
  323. $hash->{POLLING} = -1;
  324. my %hu_upd_params = (
  325. url => "",
  326. timeout => 5,
  327. method => "GET",
  328. header => $TelegramBot_header,
  329. isPolling => "update",
  330. hideurl => 1,
  331. callback => \&TelegramBot_Callback
  332. );
  333. my %hu_do_params = (
  334. url => "",
  335. timeout => 30,
  336. method => "GET",
  337. header => $TelegramBot_header,
  338. hideurl => 1,
  339. callback => \&TelegramBot_Callback
  340. );
  341. $hash->{HU_UPD_PARAMS} = \%hu_upd_params;
  342. $hash->{HU_DO_PARAMS} = \%hu_do_params;
  343. TelegramBot_Setup( $hash );
  344. return $ret;
  345. }
  346. #####################################
  347. # Undef function is corresponding to the delete command the opposite to the define function
  348. # Cleanup the device specifically for external ressources like connections, open files,
  349. # external memory outside of hash, sub processes and timers
  350. sub TelegramBot_Undef($$)
  351. {
  352. my ($hash, $arg) = @_;
  353. my $name = $hash->{NAME};
  354. Log3 $name, 3, "TelegramBot_Undef $name: called ";
  355. HttpUtils_Close($hash->{HU_UPD_PARAMS});
  356. HttpUtils_Close($hash->{HU_DO_PARAMS});
  357. RemoveInternalTimer($hash);
  358. RemoveInternalTimer($hash->{HU_DO_PARAMS});
  359. Log3 $name, 4, "TelegramBot_Undef $name: done ";
  360. return undef;
  361. }
  362. ##############################################################################
  363. ##############################################################################
  364. ##
  365. ## Instance operational methods
  366. ##
  367. ##############################################################################
  368. ##############################################################################
  369. ####################################
  370. # State function to ensure contacts internal hash being reset on Contacts Readings Set
  371. sub TelegramBot_State($$$$) {
  372. my ($hash, $time, $name, $value) = @_;
  373. # Log3 $hash->{NAME}, 4, "TelegramBot_State called with :$name: value :$value:";
  374. if ($name eq 'Contacts') {
  375. TelegramBot_CalcContactsHash( $hash, $value );
  376. Log3 $hash->{NAME}, 4, "TelegramBot_State Contacts hash has now :".scalar(keys %{$hash->{Contacts}}).":";
  377. }
  378. return undef;
  379. }
  380. ####################################
  381. # set function for executing set operations on device
  382. sub TelegramBot_Set($@)
  383. {
  384. my ( $hash, $name, @args ) = @_;
  385. Log3 $name, 4, "TelegramBot_Set $name: called ";
  386. ### Check Args
  387. my $numberOfArgs = int(@args);
  388. return "TelegramBot_Set: No cmd specified for set" if ( $numberOfArgs < 1 );
  389. my $cmd = shift @args;
  390. Log3 $name, 4, "TelegramBot_Set $name: Processing TelegramBot_Set( $cmd )";
  391. if (!exists($sets{$cmd})) {
  392. my @cList;
  393. foreach my $k (keys %sets) {
  394. my $opts = undef;
  395. $opts = $sets{$k};
  396. if (defined($opts)) {
  397. push(@cList,$k . ':' . $opts);
  398. } else {
  399. push (@cList,$k);
  400. }
  401. } # end foreach
  402. return "TelegramBot_Set: Unknown argument $cmd, choose one of " . join(" ", @cList);
  403. } # error unknown cmd handling
  404. my $ret = undef;
  405. if( ($cmd eq 'message') || ($cmd eq 'queryInline') || ($cmd eq 'queryEditInline') || ($cmd eq 'queryAnswer') || ($cmd eq 'msg') || ($cmd eq '_msg') || ($cmd eq 'reply') || ($cmd eq 'msgEdit') || ($cmd eq 'msgForceReply') || ($cmd =~ /^send.*/ ) ) {
  406. my $msgid;
  407. my $msg;
  408. my $addPar;
  409. my $sendType = 0;
  410. my $peers;
  411. my $inline = 0;
  412. if ( ($cmd eq 'reply') || ($cmd eq 'msgEdit' ) || ($cmd eq 'queryEditInline' ) ) {
  413. return "TelegramBot_Set: Command $cmd, no peer, msgid and no text/file specified" if ( $numberOfArgs < 3 );
  414. $msgid = shift @args;
  415. $numberOfArgs--;
  416. $inline = 1 if ($cmd eq 'queryEditInline');
  417. } elsif ($cmd eq 'msgForceReply') {
  418. $addPar = "{\"force_reply\":true}";
  419. } elsif ($cmd eq 'queryInline') {
  420. $inline = 1;
  421. }
  422. return "TelegramBot_Set: Command $cmd, no peers and no text/file specified" if ( $numberOfArgs < 2 );
  423. # numberOfArgs might not be correct beyond this point
  424. while ( $args[0] =~ /^@(..+)$/ ) {
  425. my $ppart = $1;
  426. return "TelegramBot_Set: Command $cmd, need exactly one peer" if ( ($cmd eq 'reply') && ( defined( $peers ) ) );
  427. $peers .= " " if ( defined( $peers ) );
  428. $peers = "" if ( ! defined( $peers ) );
  429. $peers .= $ppart;
  430. shift @args;
  431. last if ( int(@args) == 0 );
  432. }
  433. return "TelegramBot_Set: Command $cmd, no msg content specified" if ( int(@args) < 1 );
  434. if ( ! defined( $peers ) ) {
  435. $peers = AttrVal($name,'defaultPeer',undef);
  436. return "TelegramBot_Set: Command $cmd, without explicit peer requires defaultPeer being set" if ( ! defined($peers) );
  437. }
  438. if ( ($cmd eq 'sendPhoto') || ($cmd eq 'sendImage') || ($cmd eq 'image') ) {
  439. $sendType = 1;
  440. } elsif ($cmd eq 'sendVoice') {
  441. $sendType = 2;
  442. } elsif ( ($cmd eq 'sendDocument') || ($cmd eq 'sendMedia') ) {
  443. $sendType = 3;
  444. } elsif ( ($cmd eq 'msgEdit') || ($cmd eq 'queryEditInline') ) {
  445. $sendType = 10;
  446. } elsif ($cmd eq 'sendLocation') {
  447. $sendType = 11;
  448. } elsif ($cmd eq 'queryAnswer') {
  449. $sendType = 12;
  450. }
  451. if ( $sendType == 11 ) {
  452. # location
  453. return "TelegramBot_Set: Command $cmd, 2 parameters latitude / longitude need to be specified" if ( int(@args) != 2 );
  454. # first latitude
  455. $msg = shift @args;
  456. # first longitude
  457. $addPar = shift @args;
  458. } elsif ( $sendType == 12 ) {
  459. # inline query
  460. return "TelegramBot_Set: Command $cmd, no inline query id given" if ( int(@args) < 1 );
  461. # first inline query id
  462. $addPar = shift @args;
  463. # remaining msg
  464. $msg = "";
  465. $msg = join(" ", @args ) if ( int(@args) > 0 );
  466. } elsif ( ( $sendType > 0 ) && ( $sendType < 10 ) ) {
  467. # should return undef if succesful
  468. $msg = shift @args;
  469. $msg = $1 if ( $msg =~ /^\"(.*)\"$/ );
  470. if ( $sendType == 1 ) {
  471. # for Photos a caption can be given
  472. $addPar = join(" ", @args ) if ( int(@args) > 0 );
  473. } else {
  474. return "TelegramBot_Set: Command $cmd, extra parameter specified after filename" if ( int(@args) > 0 );
  475. }
  476. } else {
  477. if ( ! defined( $addPar ) ) {
  478. # check for Keyboard given (only if not forcing reply) and parse it to keys / jsonkb
  479. my @keys;
  480. while ( $args[0] =~ /^\s*\(.*$/ ) {
  481. my $aKey = "";
  482. while ( $aKey !~ /^\s*\((.*)\)\s*$/ ) {
  483. $aKey .= " ".$args[0];
  484. shift @args;
  485. last if ( int(@args) == 0 );
  486. }
  487. # trim key
  488. $aKey =~ s/^\s+|\s+$//g;
  489. if ( $aKey =~ /^\((.*)\)$/ ) {
  490. my @tmparr = split( /\|/, $1 );
  491. push( @keys, \@tmparr );
  492. } else {
  493. # incomplete key handle as message
  494. unshift( @args, $aKey ) if ( length( $aKey ) > 0 );
  495. last;
  496. }
  497. }
  498. $addPar = TelegramBot_MakeKeyboard( $hash, 1, $inline, @keys ) if ( scalar( @keys ) );
  499. }
  500. return "TelegramBot_Set: Command $cmd, no text for msg specified " if ( int(@args) == 0 );
  501. $msg = join(" ", @args );
  502. }
  503. Log3 $name, 5, "TelegramBot_Set $name: start send for cmd :$cmd: and sendType :$sendType:";
  504. $ret = TelegramBot_SendIt( $hash, $peers, $msg, $addPar, $sendType, $msgid );
  505. } elsif($cmd eq 'zDebug') {
  506. # for internal testing only
  507. Log3 $name, 5, "TelegramBot_Set $name: start debug option ";
  508. # delete $hash->{sentMsgPeer};
  509. $ret = TelegramBot_SendIt( $hash, AttrVal($name,'defaultPeer',undef), "abc def\n def ghi", undef, 0, undef );
  510. # BOTONLY
  511. } elsif($cmd eq 'reset') {
  512. Log3 $name, 5, "TelegramBot_Set $name: reset requested ";
  513. TelegramBot_Setup( $hash );
  514. } elsif($cmd eq 'replaceContacts') {
  515. if ( $numberOfArgs < 2 ) {
  516. return "TelegramBot_Set: Command $cmd, need to specify contacts string separate by space and contacts in the form of <id>:<full_name>:[@<username>|#<groupname>] ";
  517. }
  518. my $arg = join(" ", @args );
  519. Log3 $name, 3, "TelegramBot_Set $name: set new contacts to :$arg: ";
  520. # first set the hash accordingly
  521. TelegramBot_CalcContactsHash($hash, $arg);
  522. # then calculate correct string reading and put this into the reading
  523. my @dumarr;
  524. TelegramBot_ContactUpdate($hash, @dumarr);
  525. Log3 $name, 5, "TelegramBot_Set $name: contacts newly set ";
  526. }
  527. if ( ! defined( $ret ) ) {
  528. Log3 $name, 5, "TelegramBot_Set $name: $cmd done succesful: ";
  529. } else {
  530. Log3 $name, 5, "TelegramBot_Set $name: $cmd failed with :$ret: ";
  531. }
  532. return $ret
  533. }
  534. #####################################
  535. # get function for gaining information from device
  536. sub TelegramBot_Get($@)
  537. {
  538. my ( $hash, $name, @args ) = @_;
  539. Log3 $name, 5, "TelegramBot_Get $name: called ";
  540. ### Check Args
  541. my $numberOfArgs = int(@args);
  542. return "TelegramBot_Get: No value specified for get" if ( $numberOfArgs < 1 );
  543. my $cmd = $args[0];
  544. my $arg = ($args[1] ? $args[1] : "");
  545. Log3 $name, 5, "TelegramBot_Get $name: Processing TelegramBot_Get( $cmd )";
  546. if(!exists($gets{$cmd})) {
  547. my @cList;
  548. foreach my $k (sort keys %gets) {
  549. my $opts = undef;
  550. $opts = $sets{$k};
  551. if (defined($opts)) {
  552. push(@cList,$k . ':' . $opts);
  553. } else {
  554. push (@cList,$k);
  555. }
  556. } # end foreach
  557. return "TelegramBot_Get: Unknown argument $cmd, choose one of " . join(" ", @cList);
  558. } # error unknown cmd handling
  559. my $ret = undef;
  560. if($cmd eq 'urlForFile') {
  561. if ( $numberOfArgs != 2 ) {
  562. return "TelegramBot_Get: Command $cmd, no file id specified";
  563. }
  564. $hash->{fileUrl} = "";
  565. # return URL for file id
  566. my $url = $hash->{URL}."getFile?file_id=".urlEncode($arg);
  567. my $guret = TelegramBot_DoUrlCommand( $hash, $url );
  568. if ( ( defined($guret) ) && ( ref($guret) eq "HASH" ) ) {
  569. if ( defined($guret->{file_path} ) ) {
  570. # URL is https://api.telegram.org/file/bot<token>/<file_path>
  571. my $filePath = $guret->{file_path};
  572. $hash->{fileUrl} = "https://api.telegram.org/file/bot".$hash->{Token}."/".$filePath;
  573. $ret = $hash->{fileUrl};
  574. } else {
  575. $ret = "urlForFile failed: no file path found";
  576. $hash->{fileUrl} = $ret;
  577. }
  578. } else {
  579. $ret = "urlForFile failed: ".(defined($guret)?$guret:"<undef>");
  580. $hash->{fileUrl} = $ret;
  581. }
  582. } elsif ( $cmd eq "update" ) {
  583. $ret = TelegramBot_UpdatePoll( $hash, "doOnce" );
  584. }
  585. Log3 $name, 5, "TelegramBot_Get $name: done with ".( defined($ret)?$ret:"<undef>").": ";
  586. return $ret
  587. }
  588. ##############################
  589. # attr function for setting fhem attributes for the device
  590. sub TelegramBot_Attr(@) {
  591. my ($cmd,$name,$aName,$aVal) = @_;
  592. my $hash = $defs{$name};
  593. Log3 $name, 5, "TelegramBot_Attr $name: called ";
  594. return "\"TelegramBot_Attr: \" $name does not exist" if (!defined($hash));
  595. if (defined($aVal)) {
  596. Log3 $name, 5, "TelegramBot_Attr $name: $cmd on $aName to $aVal";
  597. } else {
  598. Log3 $name, 5, "TelegramBot_Attr $name: $cmd on $aName to <undef>";
  599. }
  600. # $cmd can be "del" or "set"
  601. # $name is device name
  602. # aName and aVal are Attribute name and value
  603. if ($cmd eq "set") {
  604. if ($aName eq 'favorites') {
  605. $attr{$name}{'favorites'} = $aVal;
  606. # Empty current alias list in hash
  607. if ( defined( $hash->{AliasCmds} ) ) {
  608. foreach my $key (keys %{$hash->{AliasCmds}} )
  609. {
  610. delete $hash->{AliasCmds}{$key};
  611. }
  612. } else {
  613. $hash->{AliasCmds} = {};
  614. }
  615. my @clist = split( /;/, $aVal);
  616. foreach my $cs ( @clist ) {
  617. my ( $alias, $desc, $parsecmd, $needsConfirm ) = TelegramBot_SplitFavoriteDef( $hash, $cs );
  618. if ( $alias ) {
  619. my $alx = $alias;
  620. my $alcmd = $parsecmd;
  621. Log3 $name, 2, "TelegramBot_Attr $name: Alias $alcmd defined multiple times" if ( defined( $hash->{AliasCmds}{$alx} ) );
  622. $hash->{AliasCmds}{$alx} = $alcmd;
  623. }
  624. }
  625. } elsif ($aName eq 'cmdRestrictedPeer') {
  626. $aVal =~ s/^\s+|\s+$//g;
  627. } elsif ( ($aName eq 'defaultPeerCopy') ||
  628. ($aName eq 'saveStateOnContactChange') ||
  629. ($aName eq 'cmdReturnEmptyResult') ||
  630. ($aName eq 'cmdTriggerOnly') ||
  631. ($aName eq 'allowUnknownContacts') ) {
  632. $aVal = ($aVal eq "1")? "1": "0";
  633. } elsif ( ($aName eq 'maxFileSize') ||
  634. ($aName eq 'maxReturnSize') ||
  635. ($aName eq 'maxRetries') ) {
  636. return "\"TelegramBot_Attr: \" $aName needs to be given in digits only" if ( $aVal !~ /^[[:digit:]]+$/ );
  637. } elsif ($aName eq 'pollingTimeout') {
  638. return "\"TelegramBot_Attr: \" $aName needs to be given in digits only" if ( $aVal !~ /^[[:digit:]]+$/ );
  639. # let all existing methods run into block
  640. RemoveInternalTimer($hash);
  641. $hash->{POLLING} = -1;
  642. # wait some time before next polling is starting
  643. TelegramBot_ResetPolling( $hash );
  644. } elsif ($aName eq 'pollingVerbose') {
  645. return "\"TelegramBot_Attr: \" Incorrect value given for pollingVerbose" if ( $aVal !~ /^((1_Digest)|(2_Log)|(0_None))$/ );
  646. } elsif ($aName eq 'allowedCommands') {
  647. my $allowedName = "allowed_$name";
  648. my $exists = ($defs{$allowedName} ? 1 : 0);
  649. AnalyzeCommand(undef, "defmod $allowedName allowed");
  650. AnalyzeCommand(undef, "attr $allowedName validFor $name");
  651. AnalyzeCommand(undef, "attr $allowedName $aName ".$aVal);
  652. Log3 $name, 3, "TelegramBot_Attr $name: ".($exists ? "modified":"created")." $allowedName with commands :$aVal:";
  653. # allowedCommands only set on the corresponding allowed_device
  654. return "\"TelegramBot_Attr: \" $aName ".($exists ? "modified":"created")." $allowedName with commands :$aVal:"
  655. }
  656. $_[3] = $aVal;
  657. }
  658. return undef;
  659. }
  660. ##############################################################################
  661. ##############################################################################
  662. ##
  663. ## Command handling
  664. ##
  665. ##############################################################################
  666. ##############################################################################
  667. #####################################
  668. #####################################
  669. # INTERNAL: Check against cmdkeyword given (no auth check !!!!)
  670. sub TelegramBot_checkCmdKeyword($$$$$) {
  671. my ($hash, $mpeernorm, $mtext, $cmdKey, $needsSep ) = @_;
  672. my $name = $hash->{NAME};
  673. my $cmd;
  674. my $doRet = 0;
  675. # Log3 $name, 3, "TelegramBot_checkCmdKeyword $name: check :".$mtext.": against defined :".$ck.": results in ".index($mtext,$ck);
  676. return ( undef, 0 ) if ( ! defined( $cmdKey ) );
  677. # Trim and then if requested add a space to the cmdKeyword
  678. $cmdKey =~ s/^\s+|\s+$//g;
  679. my $ck = $cmdKey;
  680. # Check special case end of messages considered separator
  681. if ( $mtext ne $ck ) {
  682. $ck .= " " if ( $needsSep );
  683. return ( undef, 0 ) if ( index($mtext,$ck) != 0 );
  684. }
  685. $cmd = substr( $mtext, length($ck) );
  686. $cmd =~ s/^\s+|\s+$//g;
  687. # validate security criteria for commands and return cmd only if succesful
  688. return ( undef, 1 ) if ( ! TelegramBot_checkAllowedPeer( $hash, $mpeernorm, $mtext ) );
  689. return ( $cmd, 1 );
  690. }
  691. #####################################
  692. #####################################
  693. # INTERNAL: Split Favorite def in alias(optional), description (optional), parsecmd, needsConfirm
  694. sub TelegramBot_SplitFavoriteDef($$) {
  695. my ($hash, $cmd ) = @_;
  696. my $name = $hash->{NAME};
  697. # Valid favoritedef
  698. # list TYPE=SOMFY
  699. # ?set TYPE=CUL_WM getconfig
  700. # /rolladen=list TYPE=SOMFY
  701. # /rolladen=?list TYPE=SOMFY
  702. # /[Liste Rolladen]=list TYPE=SOMFY
  703. # /[Liste Rolladen]=?list TYPE=SOMFY
  704. # /rolladen[Liste Rolladen]=list TYPE=SOMFY
  705. # /rolladen[Liste Rolladen]=list TYPE=SOMFY
  706. my ( $alias, $desc, $parsecmd, $confirm );
  707. if ( $cmd =~ /^\s*((\/[^\[=]*)?(\[([^\]]+)\])?=)?(\??)(.*?)$/ ) {
  708. $alias = $2;
  709. $alias = undef if ( $alias && ( $alias eq "/" ) );
  710. $desc = $4;
  711. $confirm = $5;
  712. $parsecmd = $6;
  713. # Debug "Parse 1 a:".$alias.": d:".$desc.": c:".$parsecmd.":";
  714. } else {
  715. Log3 $name, 1, "TelegramBot_SplitFavoriteDef invalid favorite definition :$cmd: ";
  716. }
  717. return ( $alias, $desc, $parsecmd, (($confirm eq "?")?1:0) );
  718. }
  719. #####################################
  720. #####################################
  721. # INTERNAL: handle sentlast and favorites
  722. sub TelegramBot_SentFavorites($$$$) {
  723. my ($hash, $mpeernorm, $cmd, $mid ) = @_;
  724. my $name = $hash->{NAME};
  725. my $ret;
  726. Log3 $name, 4, "TelegramBot_SentFavorites cmd correct peer ";
  727. my $slc = AttrVal($name,'favorites',"");
  728. # Log3 $name, 5, "TelegramBot_SentFavorites Favorites :$slc: ";
  729. my @clist = split( /;/, $slc);
  730. my $isConfirm;
  731. if ( $cmd =~ /^\s*([0-9]+)(\??)\s*=.*$/ ) {
  732. $cmd = $1;
  733. $isConfirm = ($2 eq "?")?1:0;
  734. }
  735. # if given a number execute the numbered favorite as a command
  736. if ( looks_like_number( $cmd ) ) {
  737. return $ret if ( $cmd == 0 );
  738. my $cmdId = ($cmd-1);
  739. Log3 $name, 4, "TelegramBot_SentFavorites exec cmd :$cmdId: ";
  740. if ( ( $cmdId >= 0 ) && ( $cmdId < scalar( @clist ) ) ) {
  741. my $ecmd = $clist[$cmdId];
  742. my ( $alias, $desc, $parsecmd, $needsConfirm ) = TelegramBot_SplitFavoriteDef( $hash, $ecmd );
  743. return "Alias could not be parsed :$ecmd:" if ( ! $parsecmd );
  744. $ecmd = $parsecmd;
  745. # Debug "Needsconfirm: ". $needsConfirm;
  746. if ( ( ! $isConfirm ) && ( $needsConfirm ) ) {
  747. # ask first for confirmation
  748. my $fcmd = AttrVal($name,'cmdFavorites',undef);
  749. my @tmparr;
  750. my @keys = ();
  751. # my @tmparr1 = ( TelegramBot_PutToUTF8( $fcmd.$cmd."? = ".(($desc)?$desc:$parsecmd)." ausführen?" ) );
  752. # my @tmparr1 = ( $fcmd.$cmd."? = ".(($desc)?$desc:$parsecmd)." ausführen?" );
  753. # my $tmptxt = encode_utf8( $fcmd.$cmd."? = ".(($desc)?$desc:$parsecmd)." ausführen?" );
  754. my $tmptxt = $fcmd.$cmd."? = ".(($desc)?$desc:$parsecmd).encode_utf8( " ausführen?" );
  755. # my $tmptxt = $fcmd.$cmd."? = ".(($desc)?$desc:$parsecmd);
  756. # Debug "tmptxt :$tmptxt:";
  757. # utf8::upgrade($tmptxt);
  758. # $tmptxt = TelegramBot_PutToUTF8($tmptxt);
  759. # $tmptxt = decode_utf8($tmptxt);
  760. my @tmparr1 = ( $tmptxt );
  761. push( @keys, \@tmparr1 );
  762. my @tmparr2 = ( "Abbruch" );
  763. push( @keys, \@tmparr2 );
  764. my $jsonkb = TelegramBot_MakeKeyboard( $hash, 1, 0, @keys );
  765. # LOCAL: External message
  766. $ret = encode_utf8( AttrVal( $name, 'textResponseConfirm', 'TelegramBot FHEM : $peer\n Bestätigung \n') );
  767. $ret =~ s/\$peer/$mpeernorm/g;
  768. # $ret = "TelegramBot FHEM : ($mpeernorm)\n Bestätigung \n";
  769. return TelegramBot_SendIt( $hash, $mpeernorm, $ret, $jsonkb, 0 );
  770. } else {
  771. $ecmd = $1 if ( $ecmd =~ /^\s*\?(.*)$/ );
  772. return TelegramBot_ExecuteCommand( $hash, $mpeernorm, $ecmd );
  773. }
  774. } else {
  775. Log3 $name, 3, "TelegramBot_SentFavorites cmd id not defined :($cmdId+1): ";
  776. }
  777. }
  778. # ret not defined means no favorite found that matches cmd or no fav given in cmd
  779. if ( ! defined( $ret ) ) {
  780. my $cnt = 0;
  781. my @keys = ();
  782. my $fcmd = AttrVal($name,'cmdFavorites',undef);
  783. foreach my $cs ( @clist ) {
  784. $cnt += 1;
  785. my ( $alias, $desc, $parsecmd, $needsConfirm ) = TelegramBot_SplitFavoriteDef( $hash, $cs );
  786. if ( defined($parsecmd) ) {
  787. my @tmparr = ( $fcmd.$cnt." = ".($alias?$alias." = ":"").(($desc)?$desc:$parsecmd) );
  788. push( @keys, \@tmparr );
  789. }
  790. }
  791. # my @tmparr = ( $fcmd."0 = Abbruch" );
  792. # push( @keys, \@tmparr );
  793. my $jsonkb = TelegramBot_MakeKeyboard( $hash, 1, 0, @keys );
  794. Log3 $name, 5, "TelegramBot_SentFavorites keyboard:".$jsonkb.": ";
  795. # LOCAL: External message
  796. $ret = AttrVal( $name, 'textResponseFavorites', 'TelegramBot FHEM : $peer\n Favoriten \n');
  797. $ret =~ s/\$peer/$mpeernorm/g;
  798. # $ret = "TelegramBot FHEM : ($mpeernorm)\n Favorites \n";
  799. $ret = TelegramBot_SendIt( $hash, $mpeernorm, $ret, $jsonkb, 0 );
  800. }
  801. return $ret;
  802. }
  803. #####################################
  804. #####################################
  805. # INTERNAL: handle sentlast and favorites
  806. sub TelegramBot_SentLastCommand($$$) {
  807. my ($hash, $mpeernorm, $cmd ) = @_;
  808. my $name = $hash->{NAME};
  809. my $ret;
  810. Log3 $name, 5, "TelegramBot_SentLastCommand cmd correct peer ";
  811. my $slc = ReadingsVal($name ,"StoredCommands","");
  812. my @cmds = split( "\n", $slc );
  813. # create keyboard
  814. my @keys = ();
  815. foreach my $cs ( @cmds ) {
  816. my @tmparr = ( $cs );
  817. push( @keys, \@tmparr );
  818. }
  819. # my @tmparr = ( $fcmd."0 = Abbruch" );
  820. # push( @keys, \@tmparr );
  821. my $jsonkb = TelegramBot_MakeKeyboard( $hash, 1, 0, @keys );
  822. # LOCAL: External message
  823. $ret = AttrVal( $name, 'textResponseCommands', 'TelegramBot FHEM : $peer\n Letzte Befehle \n');
  824. $ret =~ s/\$peer/$mpeernorm/g;
  825. # $ret = "TelegramBot FHEM : $mpeernorm \n Last Commands \n";
  826. # overwrite ret with result from SendIt --> send response
  827. $ret = TelegramBot_SendIt( $hash, $mpeernorm, $ret, $jsonkb, 0 );
  828. ############ OLD SentLastCommands sent as message
  829. # $ret = "TelegramBot fhem : $mpeernorm \nLast Commands \n\n".$slc;
  830. # # overwrite ret with result from Analyzecommand --> send response
  831. # $ret = AnalyzeCommand( undef, "set $name message \@$mpeernorm $ret", "" );
  832. return $ret;
  833. }
  834. #####################################
  835. #####################################
  836. # INTERNAL: execute command and sent return value
  837. sub TelegramBot_ReadHandleCommand($$$$) {
  838. my ($hash, $mpeernorm, $cmd, $mtext ) = @_;
  839. my $name = $hash->{NAME};
  840. my $ret;
  841. Log3 $name, 3, "TelegramBot_ReadHandleCommand $name: cmd found :".$cmd.": ";
  842. # get human readble name for peer
  843. my $pname = TelegramBot_GetFullnameForContact( $hash, $mpeernorm );
  844. Log3 $name, 5, "TelegramBot_ReadHandleCommand cmd correct peer ";
  845. # Either no peer defined or cmdpeer matches peer for message -> good to execute
  846. my $cto = AttrVal($name,'cmdTriggerOnly',"0");
  847. if ( $cto eq '1' ) {
  848. $cmd = "trigger ".$cmd;
  849. }
  850. Log3 $name, 5, "TelegramBot_ReadHandleCommand final cmd for analyze :".$cmd.": ";
  851. # store last commands (original text)
  852. TelegramBot_AddStoredCommands( $hash, $mtext );
  853. $ret = TelegramBot_ExecuteCommand( $hash, $mpeernorm, $cmd );
  854. return $ret;
  855. }
  856. #####################################
  857. #####################################
  858. # INTERNAL: execute command and sent return value
  859. sub TelegramBot_ExecuteCommand($$$) {
  860. my ($hash, $mpeernorm, $cmd ) = @_;
  861. my $name = $hash->{NAME};
  862. my $ret;
  863. # get human readble name for peer
  864. my $pname = TelegramBot_GetFullnameForContact( $hash, $mpeernorm );
  865. Log3 $name, 5, "TelegramBot_ExecuteCommand final cmd for analyze :".$cmd.": ";
  866. # special case shutdown caught here to avoid endless loop
  867. $ret = "shutdown command can not be executed" if ( $cmd =~ /^shutdown/ );
  868. # Execute command
  869. my $isMediaStream = 0;
  870. if ( ! defined( $ret ) ) {
  871. # run replace set magic on command - first
  872. my %dummy;
  873. my ($err, @a) = ReplaceSetMagic(\%dummy, 0, ( $cmd ) );
  874. if ( $err ) {
  875. Log3 $name, 1, "TelegramBot_ExecuteCommand $name: parse cmd failed on ReplaceSetmagic with :$err: on :$cmd:";
  876. } else {
  877. $cmd = join(" ", @a);
  878. Log3 $name, 4, "TelegramBot_ExecuteCommand $name: parse cmd returned :$cmd:";
  879. }
  880. $ret = AnalyzeCommand( $hash, $cmd );
  881. # Check for image/doc/audio stream in return (-1 image
  882. ( $isMediaStream ) = TelegramBot_IdentifyStream( $hash, $ret ) if ( defined( $ret ) );
  883. }
  884. Log3 $name, 4, "TelegramBot_ExecuteCommand result for analyze :".TelegramBot_MsgForLog($ret, $isMediaStream ).": ";
  885. my $defpeer = AttrVal($name,'defaultPeer',undef);
  886. $defpeer = TelegramBot_GetIdForPeer( $hash, $defpeer ) if ( defined( $defpeer ) );
  887. $defpeer = AttrVal($name,'defaultPeer',undef) if ( ! defined( $defpeer ) );
  888. $defpeer = undef if ( $defpeer eq $mpeernorm );
  889. # LOCAL: External message
  890. my $retMsg = AttrVal( $name, 'textResponseResult', 'TelegramBot FHEM : $peer\n Befehl:$cmd:\n Ergebnis:\n$result \n ');
  891. $retMsg =~ s/\$cmd/$cmd/g;
  892. if ( defined( $defpeer ) ) {
  893. $retMsg =~ s/\$peer/$pname/g;
  894. } else {
  895. $retMsg =~ s/\$peer//g;
  896. }
  897. if ( ( ! defined( $ret ) ) || ( length( $ret) == 0 ) ) {
  898. $retMsg =~ s/\$result/OK/g;
  899. $ret = $retMsg if ( AttrVal($name,'cmdReturnEmptyResult',1) );
  900. } elsif ( ! $isMediaStream ) {
  901. $retMsg =~ s/\$result/$ret/g;
  902. $ret = $retMsg;
  903. }
  904. # trim $ret avoiding empty msg error from telegram
  905. # Debug "Length before :".length($ret)." :$ret:";
  906. $ret =~ s/^(\s|(\\[rfnt]))+|(\s|(\\[rfnt]))+$//g if ( defined($ret) );
  907. # Debug "Length after :".length($ret);
  908. # my $retstart = "TelegramBot FHEM";
  909. # $retstart .= " from $pname ($mpeernorm)" if ( defined( $defpeer ) );
  910. # my $retempty = AttrVal($name,'cmdReturnEmptyResult',1);
  911. # undef is considered ok
  912. # if ( ( ! defined( $ret ) ) || ( length( $ret) == 0 ) ) {
  913. # # : External message
  914. # $ret = "$retstart cmd :$cmd: result OK" if ( $retempty );
  915. # } elsif ( ! $isMediaStream ) {
  916. # $ret = "$retstart cmd :$cmd: result :$ret:";
  917. # }
  918. Log3 $name, 5, "TelegramBot_ExecuteCommand $name: ".TelegramBot_MsgForLog($ret, $isMediaStream ).": ";
  919. if ( ( defined( $ret ) ) && ( length( $ret) != 0 ) ) {
  920. if ( ! $isMediaStream ) {
  921. # replace line ends with spaces
  922. $ret =~ s/\r//gm;
  923. # shorten to maxReturnSize if set
  924. my $limit = AttrVal($name,'maxReturnSize',4000);
  925. if ( ( length($ret) > $limit ) && ( $limit != 0 ) ) {
  926. $ret = substr( $ret, 0, $limit )."\n \n ...";
  927. }
  928. $ret =~ s/\n/\\n/gm;
  929. }
  930. my $peers = $mpeernorm;
  931. my $dpc = AttrVal($name,'defaultPeerCopy',1);
  932. $peers .= " ".$defpeer if ( ( $dpc ) && ( defined( $defpeer ) ) );
  933. # Ignore result from sendIt here
  934. my $retsend = TelegramBot_SendIt( $hash, $peers, $ret, undef, $isMediaStream );
  935. # ensure return is not a stream (due to log handling)
  936. $ret = TelegramBot_MsgForLog($ret, $isMediaStream )
  937. }
  938. return $ret;
  939. }
  940. ######################################
  941. # add a command to the StoredCommands reading
  942. # hash, cmd
  943. sub TelegramBot_AddStoredCommands($$) {
  944. my ($hash, $cmd) = @_;
  945. my $stcmds = ReadingsVal($hash->{NAME},"StoredCommands","");
  946. $stcmds = $stcmds;
  947. if ( $stcmds !~ /^\Q$cmd\E$/m ) {
  948. # add new cmd
  949. $stcmds .= $cmd."\n";
  950. # check number lines
  951. my $num = ( $stcmds =~ tr/\n// );
  952. if ( $num > 10 ) {
  953. $stcmds =~ /^[^\n]+\n(.*)$/s;
  954. $stcmds = $1;
  955. }
  956. # change reading
  957. readingsSingleUpdate($hash, "StoredCommands", $stcmds , 1);
  958. Log3 $hash->{NAME}, 4, "TelegramBot_AddStoredCommands :$stcmds: ";
  959. }
  960. }
  961. #####################################
  962. # INTERNAL: Function to check for commands in messages
  963. # Always executes and returns on first match also in case of error
  964. sub Telegram_HandleCommandInMessages($$$$)
  965. {
  966. my ( $hash, $mpeernorm, $mtext, $mid ) = @_;
  967. my $name = $hash->{NAME};
  968. my $cmdRet;
  969. my $cmd;
  970. my $doRet;
  971. # trim whitespace from message text
  972. $mtext =~ s/^\s+|\s+$//g;
  973. #### Check authorization for cmd execution is done inside checkCmdKeyword
  974. # Check for cmdKeyword in msg
  975. ( $cmd, $doRet ) = TelegramBot_checkCmdKeyword( $hash, $mpeernorm, $mtext, AttrVal($name,'cmdKeyword',undef), 1 );
  976. if ( defined( $cmd ) ) {
  977. $cmdRet = TelegramBot_ReadHandleCommand( $hash, $mpeernorm, $cmd, $mtext );
  978. Log3 $name, 4, "TelegramBot_ParseMsg $name: ReadHandleCommand returned :$cmdRet:" if ( defined($cmdRet) );
  979. return;
  980. } elsif ( $doRet ) {
  981. return;
  982. }
  983. # Check for sentCommands Keyword in msg
  984. ( $cmd, $doRet ) = TelegramBot_checkCmdKeyword( $hash, $mpeernorm, $mtext, AttrVal($name,'cmdSentCommands',undef), 1 );
  985. if ( defined( $cmd ) ) {
  986. $cmdRet = TelegramBot_SentLastCommand( $hash, $mpeernorm, $cmd );
  987. Log3 $name, 4, "TelegramBot_ParseMsg $name: SentLastCommand returned :$cmdRet:" if ( defined($cmdRet) );
  988. return;
  989. } elsif ( $doRet ) {
  990. return;
  991. }
  992. # Check for favorites Keyword in msg
  993. ( $cmd, $doRet ) = TelegramBot_checkCmdKeyword( $hash, $mpeernorm, $mtext, AttrVal($name,'cmdFavorites',undef), 0 );
  994. if ( defined( $cmd ) ) {
  995. $cmdRet = TelegramBot_SentFavorites( $hash, $mpeernorm, $cmd, $mid );
  996. Log3 $name, 4, "TelegramBot_ParseMsg $name: SentFavorites returned :$cmdRet:" if ( defined($cmdRet) );
  997. return;
  998. } elsif ( $doRet ) {
  999. return;
  1000. }
  1001. # Check for favorite aliase in msg - execute command then
  1002. if ( defined( $hash->{AliasCmds} ) ) {
  1003. foreach my $aliasKey (keys %{$hash->{AliasCmds}} ) {
  1004. ( $cmd, $doRet ) = TelegramBot_checkCmdKeyword( $hash, $mpeernorm, $mtext, $aliasKey, 1 );
  1005. if ( defined( $cmd ) ) {
  1006. # Build the final command from the the alias and the remainder of the message
  1007. Log3 $name, 5, "TelegramBot_ParseMsg $name: Alias Match :$aliasKey:";
  1008. $cmd = $hash->{AliasCmds}{$aliasKey}." ".$cmd;
  1009. $cmdRet = TelegramBot_ExecuteCommand( $hash, $mpeernorm, $cmd );
  1010. Log3 $name, 4, "TelegramBot_ParseMsg $name: ExecuteFavoriteCmd returned :$cmdRet:" if ( defined($cmdRet) );
  1011. return;
  1012. } elsif ( $doRet ) {
  1013. return;
  1014. }
  1015. }
  1016. }
  1017. # ignore result of readhandlecommand since it leads to endless loop
  1018. }
  1019. #####################################
  1020. # INTERNAL: Function to send a command handle result
  1021. # Parameter
  1022. # hash
  1023. # url - url including parameters
  1024. # > returns string in case of error or the content of the result object if ok
  1025. sub TelegramBot_DoUrlCommand($$)
  1026. {
  1027. my ( $hash, $url ) = @_;
  1028. my $name = $hash->{NAME};
  1029. my $ret;
  1030. Log3 $name, 5, "TelegramBot_DoUrlCommand $name: called ";
  1031. my $param = {
  1032. url => $url,
  1033. timeout => 1,
  1034. hash => $hash,
  1035. method => "GET",
  1036. header => $TelegramBot_header
  1037. };
  1038. my ($err, $data) = HttpUtils_BlockingGet( $param );
  1039. if ( $err ne "" ) {
  1040. # http returned error
  1041. $ret = "FAILED http access returned error :$err:";
  1042. Log3 $name, 2, "TelegramBot_DoUrlCommand $name: ".$ret;
  1043. } else {
  1044. my $jo;
  1045. eval {
  1046. $jo = decode_json( $data );
  1047. };
  1048. if ( ! defined( $jo ) ) {
  1049. $ret = "FAILED invalid JSON returned";
  1050. Log3 $name, 2, "TelegramBot_DoUrlCommand $name: ".$ret;
  1051. } elsif ( $jo->{ok} ) {
  1052. $ret = $jo->{result};
  1053. Log3 $name, 4, "TelegramBot_DoUrlCommand OK with result";
  1054. } else {
  1055. my $ret = "FAILED Telegram returned error: ".$jo->{description};
  1056. Log3 $name, 2, "TelegramBot_DoUrlCommand $name: ".$ret;
  1057. }
  1058. }
  1059. return $ret;
  1060. }
  1061. ##############################################################################
  1062. ##############################################################################
  1063. ##
  1064. ## Communication - Send - receive - Parse
  1065. ##
  1066. ##############################################################################
  1067. ##############################################################################
  1068. #####################################
  1069. # INTERNAL: Function to send a photo (and text message) to a peer and handle result
  1070. # addPar is caption for images / keyboard for text / longituted for location (isMedia 10)
  1071. # isMedia - 0 (text)
  1072. sub TelegramBot_SendIt($$$$$;$$)
  1073. {
  1074. my ( $hash, @args) = @_;
  1075. my ( $peers, $msg, $addPar, $isMedia, $replyid, $retryCount) = @args;
  1076. my $name = $hash->{NAME};
  1077. if ( ! defined( $retryCount ) ) {
  1078. $retryCount = 0;
  1079. }
  1080. # increase retrycount for next try
  1081. $args[5] = $retryCount+1;
  1082. Log3 $name, 5, "TelegramBot_SendIt $name: called ";
  1083. # ensure sentQueue exists
  1084. $hash->{sentQueue} = [] if ( ! defined( $hash->{sentQueue} ) );
  1085. if ( ( defined( $hash->{sentMsgResult} ) ) && ( $hash->{sentMsgResult} =~ /^WAITING/ ) && ( $retryCount == 0 ) ){
  1086. # add to queue
  1087. Log3 $name, 4, "TelegramBot_SendIt $name: add send to queue :$peers: -:".
  1088. TelegramBot_MsgForLog($msg, ($isMedia<0) ).": - :".(defined($addPar)?$addPar:"<undef>").":";
  1089. push( @{ $hash->{sentQueue} }, \@args );
  1090. return;
  1091. }
  1092. my $ret;
  1093. $hash->{sentMsgResult} = "WAITING";
  1094. $hash->{sentMsgResult} .= " retry $retryCount" if ( $retryCount > 0 );
  1095. $hash->{sentMsgId} = "";
  1096. my $peer;
  1097. ( $peer, $peers ) = split( " ", $peers, 2 );
  1098. # handle addtl peers specified (will be queued since WAITING is set already)
  1099. if ( defined( $peers ) ) {
  1100. # ignore return, since it is only queued
  1101. TelegramBot_SendIt( $hash, $peers, $msg, $addPar, $isMedia );
  1102. }
  1103. Log3 $name, 5, "TelegramBot_SendIt $name: try to send message to :$peer: -:".
  1104. TelegramBot_MsgForLog($msg, ($isMedia<0) ).": - :".(defined($addPar)?$addPar:"<undef>").":";
  1105. # trim and convert spaces in peer to underline
  1106. my $peer2 = TelegramBot_GetIdForPeer( $hash, $peer );
  1107. if ( ! defined( $peer2 ) ) {
  1108. $ret = "FAILED peer not found :$peer:";
  1109. # Log3 $name, 2, "TelegramBot_SendIt $name: failed with :".$ret.":";
  1110. $peer2 = "";
  1111. }
  1112. $hash->{sentMsgPeer} = TelegramBot_GetFullnameForContact( $hash, $peer2 );
  1113. $hash->{sentMsgPeerId} = $peer2;
  1114. # init param hash
  1115. $hash->{HU_DO_PARAMS}->{hash} = $hash;
  1116. $hash->{HU_DO_PARAMS}->{header} = $TelegramBot_header;
  1117. delete( $hash->{HU_DO_PARAMS}->{args} );
  1118. delete( $hash->{HU_DO_PARAMS}->{boundary} );
  1119. my $timeout = AttrVal($name,'cmdTimeout',30);
  1120. $hash->{HU_DO_PARAMS}->{timeout} = $timeout;
  1121. # only for test / debug
  1122. # $hash->{HU_DO_PARAMS}->{loglevel} = 3;
  1123. # handle data creation only if no error so far
  1124. if ( ! defined( $ret ) ) {
  1125. # add chat / user id (no file) --> this will also do init
  1126. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "chat_id", undef, $peer2, 0 );
  1127. if ( ( $isMedia == 0 ) || ( $isMedia == 10 ) ) {
  1128. if ( $isMedia == 0 ) {
  1129. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."sendMessage";
  1130. } else {
  1131. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."editMessageText";
  1132. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "message_id", undef, $replyid, 0 ) if ( ! defined( $ret ) );
  1133. $replyid = undef;
  1134. }
  1135. # $hash->{HU_DO_PARAMS}->{url} = "http://requestb.in";
  1136. ## JVI
  1137. # Debug "send org msg :".$msg.":";
  1138. my $parseMode = TelegramBot_AttrNum($name,"parseModeSend","0" );
  1139. if ( $parseMode == 1 ) {
  1140. $parseMode = "Markdown";
  1141. } elsif ( $parseMode == 2 ) {
  1142. $parseMode = "HTML";
  1143. } elsif ( $parseMode == 3 ) {
  1144. $parseMode = 0;
  1145. if ( $msg =~ /^markdown(.*)$/i ) {
  1146. $msg = $1;
  1147. $parseMode = "Markdown";
  1148. } elsif ( $msg =~ /^HTML(.*)$/i ) {
  1149. $msg = $1;
  1150. $parseMode = "HTML";
  1151. }
  1152. } else {
  1153. $parseMode = 0;
  1154. }
  1155. Log3 $name, 4, "TelegramBot_SendIt parseMode $parseMode";
  1156. if ( length($msg) > 1000 ) {
  1157. $hash->{sentMsgText} = substr($msg,0, 1000)."...";
  1158. } else {
  1159. $hash->{sentMsgText} = $msg;
  1160. }
  1161. $msg =~ s/(?<![\\])\\n/\x0A/g;
  1162. $msg =~ s/(?<![\\])\\t/\x09/g;
  1163. ## JVI
  1164. # Debug "send conv msg :".$msg.":";
  1165. # add msg (no file)
  1166. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "text", undef, $msg, 0 ) if ( ! defined( $ret ) );
  1167. # add parseMode
  1168. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "parse_mode", undef, $parseMode, 0 ) if ( ( ! defined( $ret ) ) && ( $parseMode ) );
  1169. # add disable_web_page_preview
  1170. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "disable_web_page_preview", undef, JSON::true, 0 )
  1171. if ( ( ! defined( $ret ) ) && ( ! AttrVal($name,'webPagePreview',1) ) );
  1172. } elsif ( $isMedia == 11 ) {
  1173. # Location send
  1174. $hash->{sentMsgText} = "Location: ".TelegramBot_MsgForLog($msg, ($isMedia<0) ).
  1175. (( defined( $addPar ) )?" - ".$addPar:"");
  1176. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."sendLocation";
  1177. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "latitude", undef, $msg, 0 ) if ( ! defined( $ret ) );
  1178. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "longitude", undef, $addPar, 0 ) if ( ! defined( $ret ) );
  1179. $addPar = undef;
  1180. } elsif ( $isMedia == 12 ) {
  1181. # answer Inline query
  1182. $hash->{sentMsgText} = "AnswerInline: ".TelegramBot_MsgForLog($msg, ($isMedia<0) ).
  1183. (( defined( $addPar ) )?" - ".$addPar:"");
  1184. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."answerCallbackQuery";
  1185. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "callback_query_id", undef, $addPar, 0 ) if ( ! defined( $ret ) );
  1186. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "text", undef, $msg, 0 ) if ( ( ! defined( $ret ) ) && ( $msg ) );
  1187. } elsif ( abs($isMedia) == 1 ) {
  1188. # Photo send
  1189. $hash->{sentMsgText} = "Image: ".TelegramBot_MsgForLog($msg, ($isMedia<0) ).
  1190. (( defined( $addPar ) )?" - ".$addPar:"");
  1191. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."sendPhoto";
  1192. # add caption
  1193. if ( defined( $addPar ) ) {
  1194. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "caption", undef, $addPar, 0 ) if ( ! defined( $ret ) );
  1195. $addPar = undef;
  1196. }
  1197. # add msg or file or stream
  1198. Log3 $name, 4, "TelegramBot_SendIt $name: Filename for image file :".
  1199. TelegramBot_MsgForLog($msg, ($isMedia<0) ).":";
  1200. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "photo", undef, $msg, $isMedia ) if ( ! defined( $ret ) );
  1201. } elsif ( $isMedia == 2 ) {
  1202. # Voicemsg send == 2
  1203. $hash->{sentMsgText} = "Voice: $msg";
  1204. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."sendVoice";
  1205. # add msg or file or stream
  1206. Log3 $name, 4, "TelegramBot_SendIt $name: Filename for document file :".
  1207. TelegramBot_MsgForLog($msg, ($isMedia<0) ).":";
  1208. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "voice", undef, $msg, 1 ) if ( ! defined( $ret ) );
  1209. } else {
  1210. # Media send == 3
  1211. $hash->{sentMsgText} = "Document: ".TelegramBot_MsgForLog($msg, ($isMedia<0) );
  1212. $hash->{HU_DO_PARAMS}->{url} = $hash->{URL}."sendDocument";
  1213. # add msg (no file)
  1214. Log3 $name, 4, "TelegramBot_SendIt $name: Filename for document file :$msg:";
  1215. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "document", undef, $msg, $isMedia ) if ( ! defined( $ret ) );
  1216. }
  1217. if ( defined( $replyid ) ) {
  1218. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "reply_to_message_id", undef, $replyid, 0 ) if ( ! defined( $ret ) );
  1219. }
  1220. if ( defined( $addPar ) ) {
  1221. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "reply_markup", undef, $addPar, 0 ) if ( ! defined( $ret ) );
  1222. }
  1223. # finalize multipart
  1224. $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, undef, undef, undef, 0 ) if ( ! defined( $ret ) );
  1225. }
  1226. ## JVI
  1227. # Debug "send command :".$hash->{HU_DO_PARAMS}->{data}.":";
  1228. if ( defined( $ret ) ) {
  1229. Log3 $name, 3, "TelegramBot_SendIt $name: Failed with :$ret:";
  1230. TelegramBot_Callback( $hash->{HU_DO_PARAMS}, $ret, "");
  1231. } else {
  1232. $hash->{HU_DO_PARAMS}->{args} = \@args;
  1233. # reset UTF8 flag for ensuring length in httputils is correctly handling lenght (as bytes)
  1234. # Debug "send a command :".$hash->{HU_DO_PARAMS}->{data}.":";
  1235. # $hash->{HU_DO_PARAMS}->{data} = encode_utf8(decode_utf8($hash->{HU_DO_PARAMS}->{data}));
  1236. # Debug "send b command :".$hash->{HU_DO_PARAMS}->{data}.":";
  1237. Log3 $name, 4, "TelegramBot_SendIt $name: timeout for sent :".$hash->{HU_DO_PARAMS}->{timeout}.": ";
  1238. HttpUtils_NonblockingGet( $hash->{HU_DO_PARAMS} );
  1239. }
  1240. return $ret;
  1241. }
  1242. #####################################
  1243. # INTERNAL: Build a multipart form data in a given hash
  1244. # Parameter
  1245. # hash (device hash)
  1246. # params (hash for building up the data)
  1247. # paramname --> if not sepecifed / undef - multipart will be finished
  1248. # header for multipart
  1249. # content
  1250. # isFile to specify if content is providing a file to be read as content
  1251. #
  1252. # > returns string in case of error or undef
  1253. sub TelegramBot_AddMultipart($$$$$$)
  1254. {
  1255. my ( $hash, $params, $parname, $parheader, $parcontent, $isMedia ) = @_;
  1256. my $name = $hash->{NAME};
  1257. my $ret;
  1258. # Check if boundary is defined
  1259. if ( ! defined( $params->{boundary} ) ) {
  1260. $params->{boundary} = "TelegramBot_boundary-x0123";
  1261. $params->{header} .= "\r\nContent-Type: multipart/form-data; boundary=".$params->{boundary};
  1262. $params->{method} = "POST";
  1263. $params->{data} = "";
  1264. }
  1265. # ensure parheader is defined and add final header new lines
  1266. $parheader = "" if ( ! defined( $parheader ) );
  1267. $parheader .= "\r\n" if ( ( length($parheader) > 0 ) && ( $parheader !~ /\r\n$/ ) );
  1268. # add content
  1269. my $finalcontent;
  1270. if ( defined( $parname ) ) {
  1271. $params->{data} .= "--".$params->{boundary}."\r\n";
  1272. if ( $isMedia > 0) {
  1273. # url decode filename
  1274. $parcontent = uri_unescape($parcontent) if ( AttrVal($name,'filenameUrlEscape',0) );
  1275. my $baseFilename = basename($parcontent);
  1276. $parheader = "Content-Disposition: form-data; name=\"".$parname."\"; filename=\"".$baseFilename."\"\r\n".$parheader."\r\n";
  1277. return( "FAILED file :$parcontent: not found or empty" ) if ( ! -e $parcontent ) ;
  1278. my $size = -s $parcontent;
  1279. my $limit = AttrVal($name,'maxFileSize',10485760);
  1280. return( "FAILED file :$parcontent: is too large for transfer (current limit: ".$limit."B)" ) if ( $size > $limit ) ;
  1281. $finalcontent = TelegramBot_BinaryFileRead( $hash, $parcontent );
  1282. if ( $finalcontent eq "" ) {
  1283. return( "FAILED file :$parcontent: not found or empty" );
  1284. }
  1285. } elsif ( $isMedia < 0) {
  1286. my ( $im, $ext ) = TelegramBot_IdentifyStream( $hash, $parcontent );
  1287. my $baseFilename = "fhem.".$ext;
  1288. $parheader = "Content-Disposition: form-data; name=\"".$parname."\"; filename=\"".$baseFilename."\"\r\n".$parheader."\r\n";
  1289. $finalcontent = $parcontent;
  1290. } else {
  1291. $parheader = "Content-Disposition: form-data; name=\"".$parname."\"\r\n".$parheader."\r\n";
  1292. $finalcontent = $parcontent;
  1293. }
  1294. $params->{data} .= $parheader.$finalcontent."\r\n";
  1295. } else {
  1296. return( "No content defined for multipart" ) if ( length( $params->{data} ) == 0 );
  1297. $params->{data} .= "--".$params->{boundary}."--";
  1298. }
  1299. return undef;
  1300. }
  1301. #####################################
  1302. # INTERNAL: Build a keyboard string for sendMessage
  1303. # Parameter
  1304. # hash (device hash)
  1305. # onetime/hide --> true means onetime / false means hide / undef means nothing
  1306. # inline --> true/false
  1307. # keys array of arrays for keyboard
  1308. # > returns string in case of error or undef
  1309. sub TelegramBot_MakeKeyboard($$$@)
  1310. {
  1311. my ( $hash, $onetime_hide, $inlinekb, @keys ) = @_;
  1312. my $name = $hash->{NAME};
  1313. my $ret;
  1314. my %par;
  1315. if ( ( defined( $inlinekb ) ) && ( $inlinekb ) ) {
  1316. # inline kb
  1317. my @parKeys = ( );
  1318. foreach my $aKeyRow ( @keys ) {
  1319. my @parRow = ();
  1320. foreach my $aKey ( @$aKeyRow ) {
  1321. my ( $keytext, $keydata ) = split( /:/, $aKey, 2);
  1322. $keydata = $keytext if ( ! defined( $keydata ) );
  1323. my %oneKey = ( "text" => $keytext, "callback_data" => $keydata );
  1324. push( @parRow, \%oneKey );
  1325. }
  1326. push( @parKeys, \@parRow );
  1327. }
  1328. %par = ( "inline_keyboard" => \@parKeys );
  1329. } elsif ( ( defined( $onetime_hide ) ) && ( ! $onetime_hide ) ) {
  1330. %par = ( "hide_keyboard" => JSON::true );
  1331. } else {
  1332. return $ret if ( ! @keys );
  1333. %par = ( "one_time_keyboard" => (( ( defined( $onetime_hide ) ) && ( $onetime_hide ) )?JSON::true:JSON::true ) );
  1334. $par{keyboard} = \@keys;
  1335. }
  1336. my $refkb = \%par;
  1337. # $refkb = TelegramBot_Deepencode( $name, $refkb );
  1338. # $ret = encode_json( $refkb );
  1339. my $json = JSON->new->utf8;
  1340. $ret = $json->utf8(0)->encode( $refkb );
  1341. Log3 $name, 4, "TelegramBot_MakeKeyboard $name: json :$ret: is utf8? ".(utf8::is_utf8($ret)?"yes":"no");
  1342. if ( utf8::is_utf8($ret) ) {
  1343. utf8::downgrade($ret);
  1344. Log3 $name, 4, "TelegramBot_MakeKeyboard $name: json downgraded :$ret: is utf8? ".(utf8::is_utf8($ret)?"yes":"no");
  1345. }
  1346. # Debug "json_keyboard :$ret:";
  1347. return $ret;
  1348. }
  1349. #####################################
  1350. # INTERNAL: _PollUpdate is called to set out a nonblocking http call for updates
  1351. # if still polling return
  1352. # if more than one fails happened --> wait instead of poll
  1353. #
  1354. # 2nd parameter set means do it once only not by regular update
  1355. sub TelegramBot_UpdatePoll($;$)
  1356. {
  1357. my ($hash, $doOnce) = @_;
  1358. my $name = $hash->{NAME};
  1359. Log3 $name, 5, "TelegramBot_UpdatePoll $name: called ";
  1360. if ( $hash->{POLLING} ) {
  1361. Log3 $name, 4, "TelegramBot_UpdatePoll $name: polling still running ";
  1362. return ( ( $doOnce ) ? "Update polling still running" : undef );
  1363. }
  1364. # Get timeout from attribute
  1365. my $timeout = AttrVal($name,'pollingTimeout',0);
  1366. $timeout = 0 if ( AttrVal($name,'disable',0) );
  1367. if ( $doOnce ) {
  1368. $timeout = 0;
  1369. } else {
  1370. if ( $timeout == 0 ) {
  1371. $hash->{STATE} = "Static";
  1372. Log3 $name, 4, "TelegramBot_UpdatePoll $name: Polling timeout 0 - no polling ";
  1373. return;
  1374. }
  1375. if ( $hash->{FAILS} > 1 ) {
  1376. # more than one fail in a row wait until next poll
  1377. $hash->{OLDFAILS} = $hash->{FAILS};
  1378. $hash->{FAILS} = 0;
  1379. my $wait = $hash->{OLDFAILS}+2;
  1380. Log3 $name, 5, "TelegramBot_UpdatePoll $name: got fails :".$hash->{OLDFAILS}.": wait ".$wait." seconds";
  1381. InternalTimer(gettimeofday()+$wait, "TelegramBot_UpdatePoll", $hash,0);
  1382. return;
  1383. } elsif ( defined($hash->{OLDFAILS}) ) {
  1384. # oldfails defined means
  1385. $hash->{FAILS} = $hash->{OLDFAILS};
  1386. delete $hash->{OLDFAILS};
  1387. }
  1388. }
  1389. # get next offset id
  1390. my $offset = $hash->{offset_id};
  1391. $offset = 0 if ( ! defined($offset) );
  1392. # build url
  1393. my $url = $hash->{URL}."getUpdates?offset=".$offset. ( ($timeout!=0)? "&limit=5&timeout=".$timeout : "" );
  1394. $hash->{HU_UPD_PARAMS}->{url} = $url;
  1395. $hash->{HU_UPD_PARAMS}->{timeout} = $timeout+$timeout+5;
  1396. $hash->{HU_UPD_PARAMS}->{hash} = $hash;
  1397. $hash->{HU_UPD_PARAMS}->{offset} = $offset;
  1398. $hash->{STATE} = "Polling";
  1399. $hash->{POLLING} = ( ( defined( $hash->{OLD_POLLING} ) )?$hash->{OLD_POLLING}:1 );
  1400. Log3 $name, 4, "TelegramBot_UpdatePoll $name: initiate polling with nonblockingGet with ".$timeout."s";
  1401. HttpUtils_NonblockingGet( $hash->{HU_UPD_PARAMS} );
  1402. }
  1403. #####################################
  1404. # INTERNAL: Called to retry a send operation after wait time
  1405. # Gets the do params
  1406. sub TelegramBot_RetrySend($)
  1407. {
  1408. my ( $param ) = @_;
  1409. my $hash= $param->{hash};
  1410. my $name = $hash->{NAME};
  1411. my $ref = $param->{args};
  1412. Log3 $name, 4, "TelegramBot_Retrysend $name: reply ".(defined( @$ref[4] )?@$ref[4]:"<undef>")." retry @$ref[5] :@$ref[0]: -:@$ref[1]: ";
  1413. TelegramBot_SendIt( $hash, @$ref[0], @$ref[1], @$ref[2], @$ref[3], @$ref[4], @$ref[5] );
  1414. }
  1415. sub TelegramBot_Deepencode
  1416. {
  1417. my @result;
  1418. my $name = shift( @_ );
  1419. # Debug "TelegramBot_Deepencode with :".(@_).":";
  1420. for (@_) {
  1421. my $reftype= ref $_;
  1422. if( $reftype eq "ARRAY" ) {
  1423. Log3 $name, 5, "TelegramBot_Deepencode $name: found an ARRAY";
  1424. push @result, [ TelegramBot_Deepencode($name, @$_) ];
  1425. }
  1426. elsif( $reftype eq "HASH" ) {
  1427. my %h;
  1428. @h{keys %$_}= TelegramBot_Deepencode($name, values %$_);
  1429. Log3 $name, 5, "TelegramBot_Deepencode $name: found a HASH";
  1430. push @result, \%h;
  1431. }
  1432. else {
  1433. my $us = $_ ;
  1434. if ( utf8::is_utf8($us) ) {
  1435. $us = encode_utf8( $_ );
  1436. }
  1437. Log3 $name, 5, "TelegramBot_Deepencode $name: encoded a String from :".$_.": to :".$us.":";
  1438. push @result, $us;
  1439. }
  1440. }
  1441. return @_ == 1 ? $result[0] : @result;
  1442. }
  1443. #####################################
  1444. # INTERNAL: Callback is the callback for any nonblocking call to the bot api (e.g. the long poll on update call)
  1445. # 3 params are defined for callbacks
  1446. # param-hash
  1447. # err
  1448. # data (returned from url call)
  1449. # empty string used instead of undef for no return/err value
  1450. sub TelegramBot_Callback($$$)
  1451. {
  1452. my ( $param, $err, $data ) = @_;
  1453. my $hash= $param->{hash};
  1454. my $name = $hash->{NAME};
  1455. my $ret;
  1456. my $result;
  1457. my $msgId;
  1458. my $ll = 5;
  1459. if ( defined( $param->{isPolling} ) ) {
  1460. $hash->{OLD_POLLING} = ( ( defined( $hash->{POLLING} ) )?$hash->{POLLING}:0 ) + 1;
  1461. $hash->{OLD_POLLING} = 1 if ( $hash->{OLD_POLLING} > 255 );
  1462. $hash->{POLLING} = 0 if ( $hash->{POLLING} != -1 ) ;
  1463. }
  1464. Log3 $name, 5, "TelegramBot_Callback $name: called from ".(( defined( $param->{isPolling} ) )?"Polling":"SendIt");
  1465. # Check for timeout "read from $hash->{addr} timed out"
  1466. if ( $err =~ /^read from.*timed out$/ ) {
  1467. $ret = "NonBlockingGet timed out on read from ".($param->{hideurl}?"<hidden>":$param->{url})." after ".$param->{timeout}."s";
  1468. } elsif ( $err ne "" ) {
  1469. $ret = "NonBlockingGet: returned $err";
  1470. } elsif ( $data ne "" ) {
  1471. # assuming empty data without err means timeout
  1472. Log3 $name, 5, "TelegramBot_Callback $name: data returned :$data:";
  1473. my $jo;
  1474. # Debug "jjj: ".$data;
  1475. ### mark as latin1 to ensure no conversion is happening (this works surprisingly)
  1476. eval {
  1477. # $data = encode( 'latin1', $data );
  1478. $data = encode_utf8( $data );
  1479. # $data = decode_utf8( $data );
  1480. # Debug "-----AFTER------\n".$data."\n-------UC=".${^UNICODE} ."-----\n";
  1481. $jo = decode_json( $data );
  1482. $jo = TelegramBot_Deepencode( $name, $jo );
  1483. };
  1484. Log3 $name, 5, "TelegramBot_Callback $name: after encoding";
  1485. ######################
  1486. if ( $@ ) {
  1487. $ret = "Callback returned no valid JSON: $@ ";
  1488. } elsif ( ! defined( $jo ) ) {
  1489. $ret = "Callback returned no valid JSON !";
  1490. } elsif ( ! $jo->{ok} ) {
  1491. if ( defined( $jo->{description} ) ) {
  1492. $ret = "Callback returned error:".$jo->{description}.":";
  1493. } else {
  1494. $ret = "Callback returned error without description";
  1495. }
  1496. } else {
  1497. if ( defined( $jo->{result} ) ) {
  1498. $result = $jo->{result};
  1499. } else {
  1500. $ret = "Callback returned no result";
  1501. }
  1502. }
  1503. }
  1504. if ( defined( $param->{isPolling} ) ) {
  1505. Log3 $name, 5, "TelegramBot_Callback $name: polling returned result? ".((defined($result))?scalar(@$result):"<undef>");
  1506. # Polling means result must be analyzed
  1507. if ( defined($result) ) {
  1508. # handle result
  1509. $hash->{FAILS} = 0; # succesful UpdatePoll reset fails
  1510. Log3 $name, 5, "UpdatePoll $name: number of results ".scalar(@$result) ;
  1511. foreach my $update ( @$result ) {
  1512. Log3 $name, 5, "UpdatePoll $name: parse result ";
  1513. if ( defined( $update->{message} ) ) {
  1514. $ret = TelegramBot_ParseMsg( $hash, $update->{update_id}, $update->{message} );
  1515. } elsif ( defined( $update->{callback_query} ) ) {
  1516. $ret = TelegramBot_ParseCallbackQuery( $hash, $update->{update_id}, $update->{callback_query} );
  1517. } else {
  1518. Log3 $name, 3, "UpdatePoll $name: inline_query id:".$update->{inline_query}->{id}.
  1519. ": query:".$update->{inline_query}->{query}.":" if ( defined( $update->{inline_query} ) );
  1520. Log3 $name, 3, "UpdatePoll $name: chosen_inline_result id:".$update->{chosen_inline_result}->{result_id}.":".
  1521. " inline id:".$update->{chosen_inline_result}->{inline_message_id}.":".
  1522. " query:".$update->{chosen_inline_result}->{query}.":"
  1523. if ( defined( $update->{chosen_inline_result} ) );
  1524. Log3 $name, 3, "UpdatePoll $name: callback_query id:".$update->{callback_query}->{id}.":".
  1525. " inline id:".$update->{callback_query}->{inline_message_id}.":".
  1526. " data:".$update->{callback_query}->{data}.":"
  1527. if ( defined( $update->{callback_query} ) );
  1528. }
  1529. if ( defined( $ret ) ) {
  1530. last;
  1531. } else {
  1532. $hash->{offset_id} = $update->{update_id}+1;
  1533. }
  1534. }
  1535. }
  1536. # get timestamps and verbose
  1537. my $now = FmtDateTime( gettimeofday() );
  1538. my $tst = ReadingsTimestamp( $name, "PollingErrCount", "1970-01-01 01:00:00" );
  1539. my $pv = AttrVal( $name, "pollingVerbose", "1_Digest" );
  1540. # get current error cnt
  1541. my $cnt = ReadingsVal( $name, "PollingErrCount", "0" );
  1542. # flag if log needs to be written
  1543. my $doLog = 0;
  1544. # Error to be converted to Reading for Poll
  1545. if ( defined( $ret ) ) {
  1546. # something went wrong increase fails
  1547. $hash->{FAILS} += 1;
  1548. # Put last error into reading
  1549. readingsSingleUpdate($hash, "PollingLastError", $ret , 1);
  1550. if ( substr($now,0,10) eq substr($tst,0,10) ) {
  1551. # Still same date just increment
  1552. $cnt += 1;
  1553. readingsSingleUpdate($hash, "PollingErrCount", $cnt, 1);
  1554. } else {
  1555. # Write digest in log on next date
  1556. $doLog = ( $pv ne "3_None" );
  1557. readingsSingleUpdate($hash, "PollingErrCount", 1, 1);
  1558. }
  1559. } elsif ( substr($now,0,10) ne substr($tst,0,10) ) {
  1560. readingsSingleUpdate($hash, "PollingErrCount", 0, 1);
  1561. $doLog = ( $pv ne "3_None" );
  1562. }
  1563. # log level is 2 on error if not digest is selected
  1564. $ll =( ( $pv eq "2_Log" )?2:4 );
  1565. # log digest if flag set
  1566. Log3 $name, 3, "TelegramBot_Callback $name: Digest: Number of poll failures on ".substr($tst,0,10)." is :$cnt:" if ( $doLog );
  1567. # start next poll or wait
  1568. TelegramBot_UpdatePoll($hash);
  1569. } else {
  1570. # Non Polling means: get msgid, reset the params and set loglevel
  1571. $hash->{HU_DO_PARAMS}->{data} = "";
  1572. $ll = 3 if ( defined( $ret ) );
  1573. $msgId = $result->{message_id} if ( ( defined($result) ) && ( ref($result) eq "HASH" ) );
  1574. }
  1575. $ret = "SUCCESS" if ( ! defined( $ret ) );
  1576. Log3 $name, $ll, "TelegramBot_Callback $name: resulted in :$ret: from ".(( defined( $param->{isPolling} ) )?"Polling":"SendIt");
  1577. if ( ! defined( $param->{isPolling} ) ) {
  1578. $hash->{sentLastResult} = $ret;
  1579. # handle retry
  1580. # ret defined / args defined in params
  1581. if ( ( $ret ne "SUCCESS" ) && ( defined( $param->{args} ) ) ) {
  1582. my $wait = $param->{args}[5];
  1583. my $maxRetries = AttrVal($name,'maxRetries',0);
  1584. if ( $wait <= $maxRetries ) {
  1585. # calculate wait time 10s / 100s / 1000s ~ 17min / 10000s ~ 3h / 100000s ~ 30h
  1586. $wait = 10**$wait;
  1587. Log3 $name, 4, "TelegramBot_Callback $name: do retry ".$param->{args}[5]." timer: $wait (ret: $ret) for msg ".
  1588. $param->{args}[0]." : ".$param->{args}[1];
  1589. # set timer
  1590. InternalTimer(gettimeofday()+$wait, "TelegramBot_RetrySend", $param,0);
  1591. # finish
  1592. return;
  1593. }
  1594. Log3 $name, 3, "TelegramBot_Callback $name: Reached max retries (ret: $ret) for msg ".$param->{args}[0]." : ".$param->{args}[1];
  1595. }
  1596. $hash->{sentMsgResult} = $ret;
  1597. $hash->{sentMsgId} = ((defined($msgId))?$msgId:"");
  1598. # Also set sentMsg Id and result in Readings
  1599. readingsBeginUpdate($hash);
  1600. readingsBulkUpdate($hash, "sentMsgResult", $ret);
  1601. readingsBulkUpdate($hash, "sentMsgId", ((defined($msgId))?$msgId:"") );
  1602. readingsBulkUpdate($hash, "sentMsgPeerId", ((defined($hash->{sentMsgPeerId}))?$hash->{sentMsgPeerId}:"") );
  1603. readingsEndUpdate($hash, 1);
  1604. if ( scalar( @{ $hash->{sentQueue} } ) ) {
  1605. my $ref = shift @{ $hash->{sentQueue} };
  1606. Log3 $name, 5, "TelegramBot_Callback $name: handle queued send with :@$ref[0]: -:@$ref[1]: ";
  1607. TelegramBot_SendIt( $hash, @$ref[0], @$ref[1], @$ref[2], @$ref[3], @$ref[4], @$ref[5] );
  1608. }
  1609. }
  1610. }
  1611. #####################################
  1612. # INTERNAL: _ParseMsg handle a message from the update call
  1613. # params are the hash, the updateid and the actual message
  1614. sub TelegramBot_ParseMsg($$$)
  1615. {
  1616. my ( $hash, $uid, $message ) = @_;
  1617. my $name = $hash->{NAME};
  1618. my @contacts;
  1619. my $ret;
  1620. my $mid = $message->{message_id};
  1621. my $from = $message->{from};
  1622. my $mpeer = $from->{id};
  1623. # ignore if unknown contacts shall be accepter
  1624. if ( ( AttrVal($name,'allowUnknownContacts',1) == 0 ) && ( ! TelegramBot_IsKnownContact( $hash, $mpeer ) ) ) {
  1625. my $mName = $from->{first_name};
  1626. $mName .= " ".$from->{last_name} if ( defined($from->{last_name}) );
  1627. Log3 $name, 3, "TelegramBot $name: Message from unknown Contact (id:$mpeer: name:$mName:) blocked";
  1628. return $ret;
  1629. }
  1630. # check peers beside from only contact (shared contact) and new_chat_participant are checked
  1631. push( @contacts, $from );
  1632. my $chatId = "";
  1633. my $chat = $message->{chat};
  1634. if ( ( defined( $chat ) ) && ( $chat->{type} ne "private" ) ) {
  1635. push( @contacts, $chat );
  1636. $chatId = $chat->{id};
  1637. }
  1638. # my $user = $message->{contact};
  1639. # if ( defined( $user ) ) {
  1640. # push( @contacts, $user );
  1641. # }
  1642. my $user = $message->{new_chat_participant};
  1643. if ( defined( $user ) ) {
  1644. push( @contacts, $user );
  1645. }
  1646. # get reply message id
  1647. my $replyId;
  1648. my $replyPart = $message->{reply_to_message};
  1649. if ( defined( $replyPart ) ) {
  1650. $replyId = $replyPart->{message_id};
  1651. }
  1652. # mtext contains the text of the message (if empty no further handling)
  1653. my ( $mtext, $mfileid );
  1654. if ( defined( $message->{text} ) ) {
  1655. # handle text message
  1656. $mtext = $message->{text};
  1657. Log3 $name, 4, "TelegramBot_ParseMsg $name: Textmessage";
  1658. } elsif ( defined( $message->{audio} ) ) {
  1659. # handle audio message
  1660. my $subtype = $message->{audio};
  1661. $mtext = "received audio ";
  1662. $mfileid = $subtype->{file_id};
  1663. $mtext .= " # Performer: ".$subtype->{performer} if ( defined( $subtype->{performer} ) );
  1664. $mtext .= " # Title: ".$subtype->{title} if ( defined( $subtype->{title} ) );
  1665. $mtext .= " # Mime: ".$subtype->{mime_type} if ( defined( $subtype->{mime_type} ) );
  1666. $mtext .= " # Size: ".$subtype->{file_size} if ( defined( $subtype->{file_size} ) );
  1667. Log3 $name, 4, "TelegramBot_ParseMsg $name: audio fileid: $mfileid";
  1668. } elsif ( defined( $message->{document} ) ) {
  1669. # handle document message
  1670. my $subtype = $message->{document};
  1671. $mtext = "received document ";
  1672. $mfileid = $subtype->{file_id};
  1673. $mtext .= " # Caption: ".$message->{caption} if ( defined( $message->{caption} ) );
  1674. $mtext .= " # Name: ".$subtype->{file_name} if ( defined( $subtype->{file_name} ) );
  1675. $mtext .= " # Mime: ".$subtype->{mime_type} if ( defined( $subtype->{mime_type} ) );
  1676. $mtext .= " # Size: ".$subtype->{file_size} if ( defined( $subtype->{file_size} ) );
  1677. Log3 $name, 4, "TelegramBot_ParseMsg $name: document fileid: $mfileid ";
  1678. } elsif ( defined( $message->{voice} ) ) {
  1679. # handle voice message
  1680. my $subtype = $message->{voice};
  1681. $mtext = "received voice ";
  1682. $mfileid = $subtype->{file_id};
  1683. $mtext .= " # Mime: ".$subtype->{mime_type} if ( defined( $subtype->{mime_type} ) );
  1684. $mtext .= " # Size: ".$subtype->{file_size} if ( defined( $subtype->{file_size} ) );
  1685. Log3 $name, 4, "TelegramBot_ParseMsg $name: voice fileid: $mfileid";
  1686. } elsif ( defined( $message->{video} ) ) {
  1687. # handle video message
  1688. my $subtype = $message->{video};
  1689. $mtext = "received video ";
  1690. $mfileid = $subtype->{file_id};
  1691. $mtext .= " # Caption: ".$message->{caption} if ( defined( $message->{caption} ) );
  1692. $mtext .= " # Mime: ".$subtype->{mime_type} if ( defined( $subtype->{mime_type} ) );
  1693. $mtext .= " # Size: ".$subtype->{file_size} if ( defined( $subtype->{file_size} ) );
  1694. Log3 $name, 4, "TelegramBot_ParseMsg $name: video fileid: $mfileid";
  1695. } elsif ( defined( $message->{photo} ) ) {
  1696. # handle photo message
  1697. # photos are always an array with (hopefully) the biggest size last in the array
  1698. my $photolist = $message->{photo};
  1699. if ( scalar(@$photolist) > 0 ) {
  1700. my $subtype = $$photolist[scalar(@$photolist)-1] ;
  1701. $mtext = "received photo ";
  1702. $mfileid = $subtype->{file_id};
  1703. $mtext .= " # Caption: ".$message->{caption} if ( defined( $message->{caption} ) );
  1704. $mtext .= " # Mime: ".$subtype->{mime_type} if ( defined( $subtype->{mime_type} ) );
  1705. $mtext .= " # Size: ".$subtype->{file_size} if ( defined( $subtype->{file_size} ) );
  1706. Log3 $name, 4, "TelegramBot_ParseMsg $name: photo fileid: $mfileid";
  1707. }
  1708. } elsif ( defined( $message->{venue} ) ) {
  1709. # handle location type message
  1710. my $ven = $message->{venue};
  1711. my $loc = $ven->{location};
  1712. $mtext = "received venue ";
  1713. $mtext .= " # latitude: ".$loc->{latitude}." # longitude: ".$loc->{longitude};
  1714. $mtext .= " # title: ".$ven->{title}." # address: ".$ven->{address};
  1715. # urls will be discarded in fhemweb $mtext .= "\n# url: <a href=\"http://maps.google.com/?q=loc:".$loc->{latitude}.",".$loc->{longitude}."\">maplink</a>";
  1716. Log3 $name, 4, "TelegramBot_ParseMsg $name: location received: latitude: ".$loc->{latitude}." longitude: ".$loc->{longitude};;
  1717. } elsif ( defined( $message->{location} ) ) {
  1718. # handle location type message
  1719. my $loc = $message->{location};
  1720. $mtext = "received location ";
  1721. $mtext .= " # latitude: ".$loc->{latitude}." # longitude: ".$loc->{longitude};
  1722. # urls will be discarded in fhemweb $mtext .= "\n# url: <a href=\"http://maps.google.com/?q=loc:".$loc->{latitude}.",".$loc->{longitude}."\">maplink</a>";
  1723. Log3 $name, 4, "TelegramBot_ParseMsg $name: location received: latitude: ".$loc->{latitude}." longitude: ".$loc->{longitude};;
  1724. }
  1725. if ( defined( $mtext ) ) {
  1726. Log3 $name, 4, "TelegramBot_ParseMsg $name: text :$mtext:";
  1727. my $mpeernorm = $mpeer;
  1728. $mpeernorm =~ s/^\s+|\s+$//g;
  1729. $mpeernorm =~ s/ /_/g;
  1730. # Log3 $name, 5, "TelegramBot_ParseMsg $name: Found message $mid from $mpeer :$mtext:";
  1731. # contacts handled separately since readings are updated in here
  1732. TelegramBot_ContactUpdate($hash, @contacts) if ( scalar(@contacts) > 0 );
  1733. readingsBeginUpdate($hash);
  1734. readingsBulkUpdate($hash, "prevMsgId", $hash->{READINGS}{msgId}{VAL});
  1735. readingsBulkUpdate($hash, "prevMsgPeer", $hash->{READINGS}{msgPeer}{VAL});
  1736. readingsBulkUpdate($hash, "prevMsgPeerId", $hash->{READINGS}{msgPeerId}{VAL});
  1737. readingsBulkUpdate($hash, "prevMsgChat", $hash->{READINGS}{msgChat}{VAL});
  1738. readingsBulkUpdate($hash, "prevMsgText", $hash->{READINGS}{msgText}{VAL});
  1739. readingsBulkUpdate($hash, "prevMsgFileId", $hash->{READINGS}{msgFileId}{VAL});
  1740. readingsBulkUpdate($hash, "prevMsgReplyMsgId", $hash->{READINGS}{msgReplyMsgId}{VAL});
  1741. readingsEndUpdate($hash, 0);
  1742. readingsBeginUpdate($hash);
  1743. readingsBulkUpdate($hash, "msgId", $mid);
  1744. readingsBulkUpdate($hash, "msgPeer", TelegramBot_GetFullnameForContact( $hash, $mpeernorm ));
  1745. readingsBulkUpdate($hash, "msgChat", TelegramBot_GetFullnameForChat( $hash, $chatId ) );
  1746. readingsBulkUpdate($hash, "msgPeerId", $mpeernorm);
  1747. readingsBulkUpdate($hash, "msgText", $mtext);
  1748. readingsBulkUpdate($hash, "msgReplyMsgId", $replyId);
  1749. readingsBulkUpdate($hash, "msgFileId", ( ( defined( $mfileid ) ) ? $mfileid : "" ) );
  1750. readingsEndUpdate($hash, 1);
  1751. # COMMAND Handling (only if no fileid found
  1752. Telegram_HandleCommandInMessages( $hash, $mpeernorm, $mtext, $mid ) if ( ! defined( $mfileid ) );
  1753. } elsif ( scalar(@contacts) > 0 ) {
  1754. # will also update reading
  1755. TelegramBot_ContactUpdate( $hash, @contacts );
  1756. Log3 $name, 5, "TelegramBot_ParseMsg $name: Found message $mid from $mpeer without text/media but with contacts";
  1757. } else {
  1758. Log3 $name, 5, "TelegramBot_ParseMsg $name: Found message $mid from $mpeer without text/media";
  1759. }
  1760. return $ret;
  1761. }
  1762. #####################################
  1763. # INTERNAL: _ParseCallbackQuery handle the callback of a query provide as
  1764. # params are the hash, the updateid and the actual message
  1765. sub TelegramBot_ParseCallbackQuery($$$)
  1766. {
  1767. my ( $hash, $uid, $callback ) = @_;
  1768. my $name = $hash->{NAME};
  1769. my @contacts;
  1770. my $ret;
  1771. my $qid = $callback->{id};
  1772. my $from = $callback->{from};
  1773. my $mpeer = $from->{id};
  1774. # get reply message id
  1775. my $replyId;
  1776. my $replyPart = $callback->{message};
  1777. if ( defined( $replyPart ) ) {
  1778. $replyId = $replyPart->{message_id};
  1779. }
  1780. my $imid = $callback->{inline_message_id};
  1781. my $chat= $callback->{chat_instance};
  1782. my $data = $callback->{data};
  1783. my $mtext = "Callback for inline query id: $qid from : $mpeer : data : ".(defined($data)?$data:"<undef>");
  1784. # ignore if unknown contacts shall be accepter
  1785. if ( ( AttrVal($name,'allowUnknownContacts',1) == 0 ) && ( ! TelegramBot_IsKnownContact( $hash, $mpeer ) ) ) {
  1786. my $mName = $from->{first_name};
  1787. $mName .= " ".$from->{last_name} if ( defined($from->{last_name}) );
  1788. Log3 $name, 3, "TelegramBot $name: Message from unknown Contact (id:$mpeer: name:$mName:) blocked";
  1789. return $ret;
  1790. }
  1791. my $mpeernorm = $mpeer;
  1792. $mpeernorm =~ s/^\s+|\s+$//g;
  1793. $mpeernorm =~ s/ /_/g;
  1794. # check peers beside from only contact (shared contact) and new_chat_participant are checked
  1795. push( @contacts, $from );
  1796. my $answerData = "";
  1797. if ( TelegramBot_checkAllowedPeer( $hash, $mpeernorm, $mtext ) ) {
  1798. Log3 $name, 4, "TelegramBot_ParseCallback $name: ".$mtext;
  1799. # contacts handled separately since readings are updated in here
  1800. TelegramBot_ContactUpdate($hash, @contacts) if ( scalar(@contacts) > 0 );
  1801. readingsBeginUpdate($hash);
  1802. readingsBulkUpdate($hash, "queryID", $qid);
  1803. readingsBulkUpdate($hash, "queryPeer", TelegramBot_GetFullnameForContact( $hash, $mpeernorm ));
  1804. readingsBulkUpdate($hash, "queryPeerId", $mpeernorm);
  1805. readingsBulkUpdate($hash, "queryData", ( ( defined( $data ) ) ? $data : "" ) );
  1806. readingsBulkUpdate($hash, "queryReplyMsgId", ( ( defined( $replyId ) ) ? $replyId : "" ) );
  1807. readingsEndUpdate($hash, 1);
  1808. $answerData = AttrVal($name,'queryAnswerText',undef);
  1809. }
  1810. # sent answer if not undef
  1811. if ( defined( $answerData ) ) {
  1812. $answerData = "" if ( ! $answerData );
  1813. if ( length( $answerData ) > 0 ) {
  1814. my %dummy;
  1815. my ($err, @a) = ReplaceSetMagic(\%dummy, 0, ( $answerData ) );
  1816. if ( $err ) {
  1817. Log3 $name, 1, "TelegramBot_ParseCallback $name: parse answerData failed on ReplaceSetmagic with :$err: on :$answerData:";
  1818. $answerData = "";
  1819. } else {
  1820. $answerData = join(" ", @a);
  1821. Log3 $name, 4, "TelegramBot_ParseCallback $name: parse answerData returned :$answerData:";
  1822. }
  1823. }
  1824. my $tmpRet = TelegramBot_SendIt( $hash, $mpeernorm, $answerData, $qid, 12, undef );
  1825. Log3 $name, 1, "TelegramBot_ParseCallback $name: send answer failed with :$tmpRet: " if ( $tmpRet );
  1826. }
  1827. return $ret;
  1828. }
  1829. ##############################################################################
  1830. ##############################################################################
  1831. ##
  1832. ## Polling / Setup
  1833. ##
  1834. ##############################################################################
  1835. ##############################################################################
  1836. ######################################
  1837. # make sure a reinitialization is triggered on next update
  1838. #
  1839. sub TelegramBot_ResetPolling($) {
  1840. my ($hash) = @_;
  1841. my $name = $hash->{NAME};
  1842. Log3 $name, 4, "TelegramBot_ResetPolling $name: called ";
  1843. RemoveInternalTimer($hash);
  1844. HttpUtils_Close( $hash->{HU_UPD_PARAMS} );
  1845. HttpUtils_Close( $hash->{HU_DO_PARAMS} );
  1846. $hash->{WAIT} = 0;
  1847. $hash->{FAILS} = 0;
  1848. # let all existing methods first run into block
  1849. $hash->{POLLING} = -1;
  1850. # wait some time before next polling is starting
  1851. InternalTimer(gettimeofday()+30, "TelegramBot_RestartPolling", $hash,0);
  1852. Log3 $name, 4, "TelegramBot_ResetPolling $name: finished ";
  1853. }
  1854. ######################################
  1855. # make sure a reinitialization is triggered on next update
  1856. #
  1857. sub TelegramBot_RestartPolling($) {
  1858. my ($hash) = @_;
  1859. my $name = $hash->{NAME};
  1860. Log3 $name, 4, "TelegramBot_RestartPolling $name: called ";
  1861. # Now polling can start
  1862. $hash->{POLLING} = 0;
  1863. # wait some time before next polling is starting
  1864. TelegramBot_UpdatePoll($hash);
  1865. Log3 $name, 4, "TelegramBot_RestartPolling $name: finished ";
  1866. }
  1867. ######################################
  1868. # make sure a reinitialization is triggered on next update
  1869. #
  1870. sub TelegramBot_Setup($) {
  1871. my ($hash) = @_;
  1872. my $name = $hash->{NAME};
  1873. Log3 $name, 4, "TelegramBot_Setup $name: called ";
  1874. $hash->{me} = "<unknown>";
  1875. $hash->{STATE} = "Undefined";
  1876. $hash->{POLLING} = -1;
  1877. $hash->{HU_UPD_PARAMS}->{callback} = \&TelegramBot_Callback;
  1878. $hash->{HU_DO_PARAMS}->{callback} = \&TelegramBot_Callback;
  1879. # Temp?? SNAME is required for allowed (normally set in TCPServerUtils)
  1880. $hash->{SNAME} = $name;
  1881. # Ensure queueing is not happening
  1882. delete( $hash->{sentQueue} );
  1883. delete( $hash->{sentMsgResult} );
  1884. # remove timer for retry
  1885. RemoveInternalTimer($hash->{HU_DO_PARAMS});
  1886. $hash->{URL} = "https://api.telegram.org/bot".$hash->{Token}."/";
  1887. $hash->{STATE} = "Defined";
  1888. # getMe as connectivity check and set internals accordingly
  1889. my $url = $hash->{URL}."getMe";
  1890. my $meret = TelegramBot_DoUrlCommand( $hash, $url );
  1891. if ( ( ! defined($meret) ) || ( ref($meret) ne "HASH" ) ) {
  1892. # retry on first failure
  1893. $meret = TelegramBot_DoUrlCommand( $hash, $url );
  1894. }
  1895. if ( ( defined($meret) ) && ( ref($meret) eq "HASH" ) ) {
  1896. $hash->{me} = TelegramBot_userObjectToString( $meret );
  1897. $hash->{STATE} = "Setup";
  1898. } else {
  1899. $hash->{me} = "Failed - see log file for details";
  1900. $hash->{STATE} = "Failed";
  1901. $hash->{FAILS} = 1;
  1902. }
  1903. TelegramBot_InternalContactsFromReading( $hash);
  1904. TelegramBot_ResetPolling($hash);
  1905. Log3 $name, 4, "TelegramBot_Setup $name: ended ";
  1906. }
  1907. ##############################################################################
  1908. ##############################################################################
  1909. ##
  1910. ## CONTACT handling
  1911. ##
  1912. ##############################################################################
  1913. ##############################################################################
  1914. #####################################
  1915. # INTERNAL: get id for a peer
  1916. # if only digits --> assume id
  1917. # if start with @ --> assume username
  1918. # if start with # --> assume groupname
  1919. # else --> assume full name
  1920. sub TelegramBot_GetIdForPeer($$)
  1921. {
  1922. my ($hash,$mpeer) = @_;
  1923. TelegramBot_InternalContactsFromReading( $hash ) if ( ! defined( $hash->{Contacts} ) );
  1924. my $id;
  1925. if ( $mpeer =~ /^\-?[[:digit:]]+$/ ) {
  1926. # check if id is in hash
  1927. # $id = $mpeer if ( defined( $hash->{Contacts}{$mpeer} ) );
  1928. # Allow also sending to ids which are not in the contacts list
  1929. $id = $mpeer;
  1930. } elsif ( $mpeer =~ /^[@#].*$/ ) {
  1931. foreach my $mkey ( keys %{$hash->{Contacts}} ) {
  1932. my @clist = split( /:/, $hash->{Contacts}{$mkey} );
  1933. if ( (defined($clist[2])) && ( $clist[2] eq $mpeer ) ) {
  1934. $id = $clist[0];
  1935. last;
  1936. }
  1937. }
  1938. } else {
  1939. $mpeer =~ s/^\s+|\s+$//g;
  1940. $mpeer =~ s/ /_/g;
  1941. foreach my $mkey ( keys %{$hash->{Contacts}} ) {
  1942. my @clist = split( /:/, $hash->{Contacts}{$mkey} );
  1943. if ( (defined($clist[1])) && ( $clist[1] eq $mpeer ) ) {
  1944. $id = $clist[0];
  1945. last;
  1946. }
  1947. }
  1948. }
  1949. return $id
  1950. }
  1951. #####################################
  1952. # INTERNAL: get full name for contact id
  1953. sub TelegramBot_GetContactInfoForContact($$)
  1954. {
  1955. my ($hash,$mcid) = @_;
  1956. TelegramBot_InternalContactsFromReading( $hash ) if ( ! defined( $hash->{Contacts} ) );
  1957. return ( $hash->{Contacts}{$mcid});
  1958. }
  1959. #####################################
  1960. # INTERNAL: get full name for contact id
  1961. sub TelegramBot_GetFullnameForContact($$)
  1962. {
  1963. my ($hash,$mcid) = @_;
  1964. my $contact = TelegramBot_GetContactInfoForContact( $hash,$mcid );
  1965. my $ret = "";
  1966. if ( defined( $contact ) ) {
  1967. Log3 $hash->{NAME}, 4, "TelegramBot_GetFullnameForContact # Contacts is $contact:";
  1968. my @clist = split( /:/, $contact );
  1969. $ret = $clist[1];
  1970. $ret = $clist[2] if ( ! $ret);
  1971. $ret = $clist[0] if ( ! $ret);
  1972. Log3 $hash->{NAME}, 4, "TelegramBot_GetFullnameForContact # name is $ret";
  1973. } else {
  1974. Log3 $hash->{NAME}, 4, "TelegramBot_GetFullnameForContact # Contacts is <undef>";
  1975. }
  1976. return $ret;
  1977. }
  1978. #####################################
  1979. # INTERNAL: get full name for a chat
  1980. sub TelegramBot_GetFullnameForChat($$)
  1981. {
  1982. my ($hash,$mcid) = @_;
  1983. my $ret = "";
  1984. return $ret if ( ! $mcid );
  1985. my $contact = TelegramBot_GetContactInfoForContact( $hash,$mcid );
  1986. if ( defined( $contact ) ) {
  1987. my @clist = split( /:/, $contact );
  1988. $ret = $clist[0];
  1989. $ret .= " (".$clist[2].")" if ( $clist[2] );
  1990. Log3 $hash->{NAME}, 4, "TelegramBot_GetFullnameForChat # $mcid is $ret";
  1991. }
  1992. return $ret;
  1993. }
  1994. #####################################
  1995. # INTERNAL: check if a contact is already known in the internals->Contacts-hash
  1996. sub TelegramBot_IsKnownContact($$)
  1997. {
  1998. my ($hash,$mpeer) = @_;
  1999. TelegramBot_InternalContactsFromReading( $hash ) if ( ! defined( $hash->{Contacts} ) );
  2000. # foreach my $key (keys $hash->{Contacts} )
  2001. # {
  2002. # Log3 $hash->{NAME}, 4, "Contact :$key: is :".$hash->{Contacts}{$key}.":";
  2003. # }
  2004. # Debug "Is known ? ".( defined( $hash->{Contacts}{$mpeer} ) );
  2005. return ( defined( $hash->{Contacts}{$mpeer} ) );
  2006. }
  2007. #####################################
  2008. # INTERNAL: calculate internals->contacts-hash from Readings->Contacts string
  2009. sub TelegramBot_CalcContactsHash($$)
  2010. {
  2011. my ($hash, $cstr) = @_;
  2012. # create a new hash
  2013. if ( defined( $hash->{Contacts} ) ) {
  2014. foreach my $key (keys %{$hash->{Contacts}} )
  2015. {
  2016. delete $hash->{Contacts}{$key};
  2017. }
  2018. } else {
  2019. $hash->{Contacts} = {};
  2020. }
  2021. # split reading at separator
  2022. my @contactList = split(/\s+/, $cstr );
  2023. # for each element - get id as hashtag and full contact as value
  2024. foreach my $contact ( @contactList ) {
  2025. my ( $id, $cname, $cuser ) = split( ":", $contact, 3 );
  2026. # add contact only if all three parts are there and either 2nd or 3rd part not empty and 3rd part either empty or start with @ or # and at least 3 chars
  2027. # and id must be only digits
  2028. $cuser = "" if ( ! defined( $cuser ) );
  2029. $cname = "" if ( ! defined( $cname ) );
  2030. Log3 $hash->{NAME}, 5, "Contact add :$contact: :$id: :$cname: :$cuser:";
  2031. if ( ( length( $cname ) == 0 ) && ( length( $cuser ) == 0 ) ) {
  2032. Log3 $hash->{NAME}, 5, "Contact add :$contact: has empty cname and cuser:";
  2033. next;
  2034. } elsif ( ( length( $cuser ) > 0 ) && ( length( $cuser ) < 3 ) ) {
  2035. Log3 $hash->{NAME}, 5, "Contact add :$contact: cuser not long enough (3):";
  2036. next;
  2037. } elsif ( ( length( $cuser ) > 0 ) && ( $cuser !~ /^[\@#]/ ) ) {
  2038. Log3 $hash->{NAME}, 5, "Contact add :$contact: cuser not matching start chars:";
  2039. next;
  2040. } elsif ( $id !~ /^\-?[[:digit:]]+$/ ) {
  2041. Log3 $hash->{NAME}, 5, "Contact add :$contact: cid is not number or -number:";
  2042. next;
  2043. } else {
  2044. $cname = TelegramBot_encodeContactString( $cname );
  2045. $cuser = TelegramBot_encodeContactString( $cuser );
  2046. $hash->{Contacts}{$id} = $id.":".$cname.":".$cuser;
  2047. }
  2048. }
  2049. }
  2050. #####################################
  2051. # INTERNAL: calculate internals->contacts-hash from Readings->Contacts string
  2052. sub TelegramBot_InternalContactsFromReading($)
  2053. {
  2054. my ($hash) = @_;
  2055. TelegramBot_CalcContactsHash( $hash, ReadingsVal($hash->{NAME},"Contacts","") );
  2056. }
  2057. #####################################
  2058. # INTERNAL: update contacts hash and change readings string (no return)
  2059. sub TelegramBot_ContactUpdate($@) {
  2060. my ($hash, @contacts) = @_;
  2061. my $newfound = ( int(@contacts) == 0 );
  2062. my $oldContactString = ReadingsVal($hash->{NAME},"Contacts","");
  2063. TelegramBot_InternalContactsFromReading( $hash ) if ( ! defined( $hash->{Contacts} ) );
  2064. Log3 $hash->{NAME}, 4, "TelegramBot_ContactUpdate # Contacts in hash before :".scalar(keys %{$hash->{Contacts}}).":";
  2065. foreach my $user ( @contacts ) {
  2066. my $contactString = TelegramBot_userObjectToString( $user );
  2067. # keep the username part of the new contatc for deleting old users with same username
  2068. my $unamepart;
  2069. my @clist = split( /:/, $contactString );
  2070. if (defined($clist[2])) {
  2071. $unamepart = $clist[2];
  2072. }
  2073. if ( ! defined( $hash->{Contacts}{$user->{id}} ) ) {
  2074. Log3 $hash->{NAME}, 3, "TelegramBot_ContactUpdate new contact :".$contactString.":";
  2075. next if ( AttrVal($hash->{NAME},'allowUnknownContacts',1) == 0 );
  2076. $newfound = 1;
  2077. } elsif ( $contactString ne $hash->{Contacts}{$user->{id}} ) {
  2078. Log3 $hash->{NAME}, 3, "TelegramBot_ContactUpdate updated contact :".$contactString.":";
  2079. }
  2080. # remove all contacts with same username
  2081. if ( defined( $unamepart ) ) {
  2082. my $dupid = TelegramBot_GetIdForPeer( $hash, $unamepart );
  2083. while ( $dupid ) {
  2084. Log3 $hash->{NAME}, 3, "TelegramBot_ContactUpdate removed stale/duplicate contact ($dupid:$unamepart):".$hash->{Contacts}{$dupid}.":" if ( $dupid ne $user->{id} );
  2085. delete( $hash->{Contacts}{$dupid} );
  2086. $dupid = TelegramBot_GetIdForPeer( $hash, $unamepart );
  2087. }
  2088. }
  2089. # set new contact data
  2090. $hash->{Contacts}{$user->{id}} = $contactString;
  2091. }
  2092. Log3 $hash->{NAME}, 4, "TelegramBot_ContactUpdate # Contacts in hash after :".scalar(keys %{$hash->{Contacts}}).":";
  2093. my $rc = "";
  2094. foreach my $key ( keys %{$hash->{Contacts}} )
  2095. {
  2096. if ( length($rc) > 0 ) {
  2097. $rc .= " ".$hash->{Contacts}{$key};
  2098. } else {
  2099. $rc = $hash->{Contacts}{$key};
  2100. }
  2101. }
  2102. # Do a readings change directly for contacts
  2103. readingsSingleUpdate($hash, "Contacts", $rc , 1) if ( $rc ne $oldContactString );
  2104. # save state file on new contact
  2105. if ( $newfound ) {
  2106. WriteStatefile() if ( AttrVal($hash->{NAME}, 'saveStateOnContactChange', 1) ) ;
  2107. Log3 $hash->{NAME}, 2, "TelegramBot_ContactUpdate Updated Contact list :".$rc.":";
  2108. }
  2109. return;
  2110. }
  2111. #####################################
  2112. # INTERNAL: Convert TelegramBot user and chat object to string
  2113. sub TelegramBot_userObjectToString($) {
  2114. my ( $user ) = @_;
  2115. my $ret = $user->{id}.":";
  2116. # user objects do not contain a type field / chat objects need to contain a type but only if type=group or type=supergroup it is really a group
  2117. if ( ( defined( $user->{type} ) ) && ( ( $user->{type} eq "group" ) || ( $user->{type} eq "supergroup" ) ) ) {
  2118. $ret .= ":";
  2119. $ret .= "#".TelegramBot_encodeContactString($user->{title}) if ( defined( $user->{title} ) );
  2120. } else {
  2121. my $part = "";
  2122. $part .= $user->{first_name} if ( defined( $user->{first_name} ) );
  2123. $part .= " ".$user->{last_name} if ( defined( $user->{last_name} ) );
  2124. $ret .= TelegramBot_encodeContactString($part).":";
  2125. $ret .= "@".TelegramBot_encodeContactString($user->{username}) if ( defined( $user->{username} ) );
  2126. }
  2127. return $ret;
  2128. }
  2129. #####################################
  2130. # INTERNAL: Convert TelegramBot user and chat object to string
  2131. sub TelegramBot_encodeContactString($) {
  2132. my ($str) = @_;
  2133. $str =~ s/:/_/g;
  2134. $str =~ s/^\s+|\s+$//g;
  2135. $str =~ s/ /_/g;
  2136. return $str;
  2137. }
  2138. #####################################
  2139. # INTERNAL: Check if peer is allowed - true if allowed
  2140. sub TelegramBot_checkAllowedPeer($$$) {
  2141. my ($hash,$mpeer,$msg) = @_;
  2142. my $name = $hash->{NAME};
  2143. Log3 $name, 5, "TelegramBot_checkAllowedPeer $name: called with $mpeer";
  2144. my $cp = AttrVal($name,'cmdRestrictedPeer','');
  2145. return 1 if ( $cp eq '' );
  2146. my @peers = split( " ", $cp);
  2147. foreach my $cp (@peers) {
  2148. return 1 if ( $cp eq $mpeer );
  2149. my $cdefpeer = TelegramBot_GetIdForPeer( $hash, $cp );
  2150. if ( defined( $cdefpeer ) ) {
  2151. return 1 if ( $cdefpeer eq $mpeer );
  2152. }
  2153. }
  2154. # get human readble name for peer
  2155. my $pname = TelegramBot_GetFullnameForContact( $hash, $mpeer );
  2156. # unauthorized fhem cmd
  2157. Log3 $name, 1, "TelegramBot unauthorized cmd from user :$pname: ($mpeer) \n Msg: $msg";
  2158. # LOCAL: External message
  2159. my $ret = AttrVal( $name, 'textResponseUnauthorized', 'UNAUTHORIZED: TelegramBot FHEM request from user :$peer \n Msg: $msg');
  2160. $ret =~ s/\$peer/$pname ($mpeer)/g;
  2161. $ret =~ s/\$msg/$msg/g;
  2162. # my $ret = "UNAUTHORIZED: TelegramBot FHEM request from user :$pname: ($mpeer) \n Msg: $msg";
  2163. # send unauthorized to defaultpeer
  2164. my $defpeer = AttrVal($name,'defaultPeer',undef);
  2165. if ( defined( $defpeer ) ) {
  2166. AnalyzeCommand( undef, "set $name message $ret" );
  2167. }
  2168. return 0;
  2169. }
  2170. ##############################################################################
  2171. ##############################################################################
  2172. ##
  2173. ## HELPER
  2174. ##
  2175. ##############################################################################
  2176. ##############################################################################
  2177. #####################################
  2178. # INTERNAL: get only numeric part of a value (simple)
  2179. sub TelegramBot_AttrNum($$$)
  2180. {
  2181. my ($d,$n,$default) = @_;
  2182. my $val = AttrVal($d,$n,$default);
  2183. $val =~ s/[^-\.\d]//g;
  2184. return $val;
  2185. }
  2186. #####################################
  2187. # INTERNAL: Convert (Mark) a scalar as UTF8 - coming from telegram
  2188. sub TelegramBot_GetUTF8Back( $ ) {
  2189. my ( $data ) = @_;
  2190. return $data;
  2191. #JVI
  2192. # return encode('utf8', $data);
  2193. }
  2194. #####################################
  2195. # INTERNAL: used to encode a string aas Utf-8 coming from the code
  2196. sub TelegramBot_PutToUTF8( $ ) {
  2197. my ( $data ) = @_;
  2198. return $data;
  2199. #JVI
  2200. # return decode('utf8', $data);
  2201. }
  2202. ######################################
  2203. # Get a string and identify possible media streams
  2204. # PNG is tested
  2205. # returns
  2206. # -1 for image
  2207. # -2 for Audio
  2208. # -3 for other media
  2209. # and extension without dot as 2nd list element
  2210. sub TelegramBot_IdentifyStream($$) {
  2211. my ($hash, $msg) = @_;
  2212. # signatures for media files are documented here --> https://en.wikipedia.org/wiki/List_of_file_signatures
  2213. # seems sometimes more correct: https://wangrui.wordpress.com/2007/06/19/file-signatures-table/
  2214. return (-1,"png") if ( $msg =~ /^\x89PNG\r\n\x1a\n/ ); # PNG
  2215. return (-1,"jpg") if ( $msg =~ /^\xFF\xD8\xFF/ ); # JPG not necessarily complete, but should be fine here
  2216. return (-2 ,"mp3") if ( $msg =~ /^\xFF\xF3/ ); # MP3 MPEG-1 Layer 3 file without an ID3 tag or with an ID3v1 tag
  2217. return (-2 ,"mp3") if ( $msg =~ /^\xFF\xFB/ ); # MP3 MPEG-1 Layer 3 file without an ID3 tag or with an ID3v1 tag
  2218. # MP3 MPEG-1 Layer 3 file with an ID3v2 tag
  2219. # starts with ID3 then version (most popular 03, new 04 seldom used, old 01 and 02) ==> Only 2,3 and 4 are tested currently
  2220. return (-2 ,"mp3") if ( $msg =~ /^ID3\x03/ );
  2221. return (-2 ,"mp3") if ( $msg =~ /^ID3\x04/ );
  2222. return (-2 ,"mp3") if ( $msg =~ /^ID3\x02/ );
  2223. return (-3,"pdf") if ( $msg =~ /^%PDF/ ); # PDF document
  2224. return (-3,"docx") if ( $msg =~ /^PK\x03\x04/ ); # Office new
  2225. return (-3,"docx") if ( $msg =~ /^PK\x05\x06/ ); # Office new
  2226. return (-3,"docx") if ( $msg =~ /^PK\x07\x08/ ); # Office new
  2227. return (-3,"doc") if ( $msg =~ /^\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1/ ); # Office old - D0 CF 11 E0 A1 B1 1A E1
  2228. return (0,undef);
  2229. }
  2230. #####################################
  2231. #####################################
  2232. # INTERNAL: prepare msg/ret for log file
  2233. sub TelegramBot_MsgForLog($;$) {
  2234. my ($msg, $stream) = @_;
  2235. if ( ! defined( $msg ) ) {
  2236. return "<undef>";
  2237. } elsif ( $stream ) {
  2238. return "<stream:".length($msg).">";
  2239. }
  2240. return $msg;
  2241. }
  2242. ######################################
  2243. # read binary file for Phototransfer - returns undef or empty string on error
  2244. #
  2245. sub TelegramBot_BinaryFileRead($$) {
  2246. my ($hash, $fileName) = @_;
  2247. return '' if ( ! (-e $fileName) );
  2248. my $fileData = '';
  2249. open TGB_BINFILE, '<'.$fileName;
  2250. binmode TGB_BINFILE;
  2251. while (<TGB_BINFILE>){
  2252. $fileData .= $_;
  2253. }
  2254. close TGB_BINFILE;
  2255. return $fileData;
  2256. }
  2257. ######################################
  2258. # write binary file for (hest hash, filename and the data
  2259. #
  2260. sub TelegramBot_BinaryFileWrite($$$) {
  2261. my ($hash, $fileName, $data) = @_;
  2262. open TGB_BINFILE, '>'.$fileName;
  2263. binmode TGB_BINFILE;
  2264. print TGB_BINFILE $data;
  2265. close TGB_BINFILE;
  2266. return undef;
  2267. }
  2268. ##############################################################################
  2269. ##############################################################################
  2270. ##
  2271. ## Documentation
  2272. ##
  2273. ##############################################################################
  2274. ##############################################################################
  2275. 1;
  2276. =pod
  2277. =item summary send and receive of messages through telegram instant messaging
  2278. =item summary_DE senden und empfangen von Nachrichten durch telegram IM
  2279. =begin html
  2280. <a name="TelegramBot"></a>
  2281. <h3>TelegramBot</h3>
  2282. <ul>
  2283. The TelegramBot module allows the usage of the instant messaging service <a href="https://telegram.org/">Telegram</a> from FHEM in both directions (sending and receiving).
  2284. So FHEM can use telegram for notifications of states or alerts, general informations and actions can be triggered.
  2285. <br>
  2286. <br>
  2287. TelegramBot makes use of the <a href=https://core.telegram.org/bots/api>telegram bot api</a> and does NOT rely on any addition local client installed.
  2288. <br>
  2289. Telegram Bots are different from normal telegram accounts, without being connected to a phone number. Instead bots need to be registered through the
  2290. <a href=https://core.telegram.org/bots#botfather>BotFather</a> to gain the needed token for authorizing as bot with telegram.org. This is done by connecting (in a telegram client) to the BotFather and sending the command <code>/newbot</code> and follow the steps specified by the BotFather. This results in a token, this token (e.g. something like <code>110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw</code> is required for defining a working telegram bot in fhem.
  2291. <br><br>
  2292. Bots also differ in other aspects from normal telegram accounts. Here some examples:
  2293. <ul>
  2294. <li>Bots can not initiate connections to arbitrary users, instead users need to first initiate the communication with the bot.</li>
  2295. <li>Bots have a different privacy setting then normal users (see <a href=https://core.telegram.org/bots#privacy-mode>Privacy mode</a>) </li>
  2296. <li>Bots support commands and specialized keyboards for the interaction (not yet supported in the fhem telegramBot)</li>
  2297. </ul>
  2298. <br><br>
  2299. Note:
  2300. <ul>
  2301. <li>This module requires the perl JSON module.<br>
  2302. Please install the module (e.g. with <code>sudo apt-get install libjson-perl</code>) or the correct method for the underlying platform/system.</li>
  2303. <li>The attribute pollingTimeout needs to be set to a value greater than zero, to define the interval of receiving messages (if not set or set to 0, no messages will be received!)</li>
  2304. <li>Multiple infomations are stored in readings (esp contacts) and internals that are needed for the bot operation, so having an recent statefile will help in correct operation of the bot. Generally it is recommended to regularly store the statefile (see save command)</li>
  2305. </ul>
  2306. <br><br>
  2307. The TelegramBot module allows receiving of messages from any peer (telegram user) and can send messages to known users.
  2308. The contacts/peers, that are known to the bot are stored in a reading (named <code>Contacts</code>) and also internally in the module in a hashed list to allow the usage
  2309. of contact ids and also full names and usernames. Contact ids are made up from only digits, user names are prefixed with a @, group names are prefixed with a #.
  2310. All other names will be considered as full names of contacts. Here any spaces in the name need to be replaced by underscores (_).
  2311. Each contact is considered a triple of contact id, full name (spaces replaced by underscores) and username or groupname prefixed by @ respectively #.
  2312. The three parts are separated by a colon (:).
  2313. <br>
  2314. Contacts are collected automatically during communication by new users contacting the bot or users mentioned in messages.
  2315. <br><br>
  2316. Updates and messages are received via long poll of the GetUpdates message. This message currently supports a maximum of 20 sec long poll.
  2317. In case of failures delays are taken between new calls of GetUpdates. In this case there might be increasing delays between sending and receiving messages!
  2318. <br>
  2319. Beside pure text messages also media messages can be sent and received. This includes audio, video, images, documents, locations and venues.
  2320. <br><br>
  2321. <a name="TelegramBotdefine"></a>
  2322. <b>Define</b>
  2323. <ul>
  2324. <code>define &lt;name&gt; TelegramBot &lt;token&gt; </code>
  2325. <br><br>
  2326. Defines a TelegramBot device using the specified token perceived from botfather
  2327. <br>
  2328. Example:
  2329. <ul>
  2330. <code>define teleBot TelegramBot 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw</code><br>
  2331. </ul>
  2332. <br>
  2333. </ul>
  2334. <br><br>
  2335. <a name="TelegramBotset"></a>
  2336. <b>Set</b>
  2337. <ul>
  2338. <li><code>message|msg|_msg|send [ @&lt;peer1&gt; ... @&lt;peerN&gt; ] [ (&lt;keyrow1&gt;) ... (&lt;keyrowN&gt;) ] &lt;text&gt;</code><br>Sends the given message to the given peer or if peer(s) is ommitted currently defined default peer user. Each peer given needs to be always prefixed with a '@'. Peers can be specified as contact ids, full names (with underscore instead of space), usernames (prefixed with another @) or chat names (also known as groups in telegram groups must be prefixed with #). Multiple peers are to be separated by space<br>
  2339. A reply keyboard can be specified by adding a list of strings enclosed in parentheses "()". Each separate string will make one keyboard row in a reply keyboard. The different keys in the row need to be separated by |. The key strings can contain spaces.<br>
  2340. Messages do not need to be quoted if containing spaces. If you want to use parentheses at the start of the message than add one extra character before the parentheses (i.e. an underline) to avoid the message being parsed as a keyboard <br>
  2341. Examples:<br>
  2342. <dl>
  2343. <dt><code>set aTelegramBotDevice message @@someusername a message to be sent</code></dt>
  2344. <dd> to send to a peer having someusername as username (not first and last name) in telegram <br> </dd>
  2345. <dt><code>set aTelegramBotDevice message (yes) (may be) are you there?</code></dt>
  2346. <dd> to send the message "are you there?" and provide a reply keyboard with two buttons ("yes" and "may be") on separate rows to the default peer <br> </dd>
  2347. <dt><code>set aTelegramBotDevice message @@someusername (yes) (may be) are you there?</code></dt>
  2348. <dd> to send the message from above with reply keyboard to a peer having someusername as username <br> </dd>
  2349. <dt><code>set aTelegramBotDevice message (yes|no) (may be) are you there?</code></dt>
  2350. <dd> to send the message from above with reply keyboard having 3 keys, 2 in the first row ("yes" / "no") and a second row with just one key to the default peer <br> </dd>
  2351. <dt><code>set aTelegramBotDevice message @@someusername @1234567 a message to be sent to multiple receipients</code></dt>
  2352. <dd> to send to a peer having someusername as username (not first and last name) in telegram <br> </dd>
  2353. <dt><code>set aTelegramBotDevice message @Ralf_Mustermann another message</code></dt>
  2354. <dd> to send to a peer with Ralf as firstname and Mustermann as last name in telegram <br></dd>
  2355. <dt><code>set aTelegramBotDevice message @#justchatting Hello</code></dt>
  2356. <dd> to send the message "Hello" to a chat with the name "justchatting" <br></dd>
  2357. <dt><code>set aTelegramBotDevice message @1234567 Bye</code></dt>
  2358. <dd> to send the message "Bye" to a contact or chat with the id "1234567". Chat ids might be negative and need to be specified with a leading hyphen (-). <br></dd>
  2359. <dl>
  2360. </li>
  2361. <li><code>msgForceReply [ @&lt;peer1&gt; ... @&lt;peerN&gt; ] &lt;text&gt;</code><br>Sends the given message to the recipient(s) and requests (forces) a reply. Handling of peers is equal to the message command. Adding reply keyboards is currently not supported by telegram.
  2362. </li>
  2363. <li><code>reply &lt;msgid&gt; [ @&lt;peer1&gt; ] &lt;text&gt;</code><br>Sends the given message as a reply to the msgid (number) given to the given peer or if peer is ommitted to the defined default peer user. Only a single peer can be specified. Beside the handling of the message as a reply to a message received earlier, the peer and message handling is otherwise identical to the msg command.
  2364. </li>
  2365. <li><code>msgEdit &lt;msgid&gt; [ @&lt;peer1&gt; ] &lt;text&gt;</code><br>Changes the given message on the recipients clients. The msgid of the message to be changed must match a valid msgId and the peers need to match the original recipient, so only a single peer can be given or if peer is ommitted the defined default peer user is used. Beside the handling of a change of an existing message, the peer and message handling is otherwise identical to the msg command.
  2366. </li>
  2367. <li><code>queryInline [ @&lt;peer1&gt; ... @&lt;peerN&gt; ] (&lt;keyrow1&gt;) ... (&lt;keyrowN&gt;) &lt;text&gt;</code><br>Sends the given message to the recipient(s) with an inline keyboard allowing direct response <br>
  2368. IMPORTANT: The response coming from the keyboard will be provided in readings and a corresponding answer command with the query id is required, sicne the client is frozen otherwise waiting for the response from the bot!
  2369. REMARK: inline queries are only accepted from contacts/peers that are authorized (i.e. as for executing commands, see cmdKeyword and cmdRestrictedPeer !)
  2370. </li>
  2371. <li><code>queryEditInline &lt;msgid&gt; [ @&lt;peer&gt; ] (&lt;keyrow1&gt;) ... (&lt;keyrowN&gt;) &lt;text&gt;</code><br>Updates the original message specified with msgId with the given message to the recipient(s) with an inline keyboard allowing direct response <br>
  2372. With this method interactive inline dialogs are possible, since the edit of message or inline keyboard can be done multiple times.
  2373. </li>
  2374. <li><code>queryAnswer &lt;queryid&gt; [ &lt;text&gt; ] </code><br>Sends the response to the inline query button press. The message is optional, the query id can be collected from the reading "callbackID". This call is mandatory on reception of an inline query from the inline command above
  2375. </li>
  2376. <li><code>sendImage|image [ @&lt;peer1&gt; ... @&lt;peerN&gt;] &lt;file&gt; [&lt;caption&gt;]</code><br>Sends a photo to the given peer(s) or if ommitted to the default peer.
  2377. File is specifying a filename and path to the image file to be send.
  2378. Local paths should be given local to the root directory of fhem (the directory of fhem.pl e.g. /opt/fhem).
  2379. Filenames with spaces need to be given in double quotes (")
  2380. Rule for specifying peers are the same as for messages. Multiple peers are to be separated by space. Captions can also contain multiple words and do not need to be quoted.
  2381. </li>
  2382. <li><code>sendMedia|sendDocument [ @&lt;peer1&gt; ... @&lt;peerN&gt;] &lt;file&gt;</code><br>Sends a media file (video, audio, image or other file type) to the given peer(s) or if ommitted to the default peer. Handling for files and peers is as specified above.
  2383. </li>
  2384. <li><code>sendVoice [ @&lt;peer1&gt; ... @&lt;peerN&gt;] &lt;file&gt;</code><br>Sends a voice message for playing directly in the browser to the given peer(s) or if ommitted to the default peer. Handling for files and peers is as specified above.
  2385. </li>
  2386. <br>
  2387. <li><code>sendLocation [ @&lt;peer1&gt; ... @&lt;peerN&gt;] &lt;latitude&gt; &lt;longitude&gt;</code><br>Sends a location as pair of coordinates latitude and longitude as floating point numbers
  2388. <br>Example: <code>set aTelegramBotDevice sendLocation @@someusername 51.163375 10.447683</code> will send the coordinates of the geographical center of Germany as location.
  2389. </li>
  2390. <br>
  2391. <li><code>replaceContacts &lt;text&gt;</code><br>Set the contacts newly from a string. Multiple contacts can be separated by a space.
  2392. Each contact needs to be specified as a triple of contact id, full name and user name as explained above. </li>
  2393. <li><code>reset</code><br>Reset the internal state of the telegram bot. This is normally not needed, but can be used to reset the used URL,
  2394. internal contact handling, queue of send items and polling <br>
  2395. ATTENTION: Messages that might be queued on the telegram server side (especially commands) might be then worked off afterwards immedately.
  2396. If in doubt it is recommened to temporarily deactivate (delete) the cmdKeyword attribute before resetting.</li>
  2397. </ul>
  2398. <br><br>
  2399. <a name="TelegramBotattr"></a>
  2400. <b>Attributes</b>
  2401. <br><br>
  2402. <ul>
  2403. <li><code>defaultPeer &lt;name&gt;</code><br>Specify contact id, user name or full name of the default peer to be used for sending messages. </li>
  2404. <li><code>defaultPeerCopy &lt;1 (default) or 0&gt;</code><br>Copy all command results also to the defined defaultPeer. If set results are sent both to the requestor and the defaultPeer if they are different.
  2405. </li>
  2406. <li><code>parseModeSend &lt;0_None or 1_Markdown or 2_HTML or 3_Inmsg &gt;</code><br>Specify the parse_mode (allowing formatting of text messages) for sent text messages. 0_None is the default where no formatting is used and plain text is sent. The different formatting options for markdown or HTML are described here <a href="https://core.telegram.org/bots/api/#formatting-options">https://core.telegram.org/bots/api/#formatting-options</a>. The option 3_Inmsg allows to specify the correct parse_mode at the beginning of the message (e.g. "Markdown*bold text*..." as message).
  2407. </li>
  2408. <li><code>webPagePreview &lt;1 or 0&gt;</code><br>Disable / Enable (Default = 1) web page preview on links in messages. See parameter https://core.telegram.org/bots/api/#sendmessage as described here: https://core.telegram.org/bots/api/#sendmessage
  2409. </li>
  2410. <br>
  2411. <li><code>cmdKeyword &lt;keyword&gt;</code><br>Specify a specific text that needs to be sent to make the rest of the message being executed as a command.
  2412. So if for example cmdKeyword is set to <code>ok fhem</code> then a message starting with this string will be executed as fhem command
  2413. (see also cmdTriggerOnly).<br>
  2414. NOTE: It is advised to set cmdRestrictedPeer for restricting access to this feature!<br>
  2415. Example: If this attribute is set to a value of <code>ok fhem</code> a message of <code>ok fhem attr telegram room IM</code>
  2416. send to the bot would execute the command <code>attr telegram room IM</code> and set a device called telegram into room IM.
  2417. The result of the cmd is sent to the requestor and in addition (if different) sent also as message to the defaultPeer (This can be controlled with the attribute <code>defaultPeerCopy</code>).
  2418. <br>
  2419. Note: <code>shutdown</code> is not supported as a command (also in favorites) and will be rejected. This is needed to avoid reexecution of the shutdown command directly after restart (endless loop !).
  2420. </li>
  2421. <li><code>cmdSentCommands &lt;keyword&gt;</code><br>Specify a specific text that will trigger sending the last commands back to the sender<br>
  2422. Example: If this attribute is set to a value of <code>last cmd</code> a message of <code>last cmd</code>
  2423. woud lead to a reply with the list of the last sent fhem commands will be sent back.<br>
  2424. Please also consider cmdRestrictedPeer for restricting access to this feature!<br>
  2425. </li>
  2426. <br>
  2427. <li><code>cmdFavorites &lt;keyword&gt;</code><br>Specify a specific text that will trigger sending the list of defined favorites or executes a given favorite by number (the favorites are defined in attribute <code>favorites</code>).
  2428. <br>
  2429. NOTE: It is advised to set cmdRestrictedPeer for restricting access to this feature!<br>
  2430. Example: If this attribute is set to a value of <code>favorite</code> a message of <code>favorite</code> to the bot will return a list of defined favorite commands and their index number. In the same case the message <code>favorite &lt;n&gt;</code> (with n being a number) would execute the command that is the n-th command in the favorites list. The result of the command will be returned as in other command executions.
  2431. </li>
  2432. <li><code>favorites &lt;list of commands&gt;</code><br>Specify a list of favorite commands for Fhem (without cmdKeyword). Multiple commands are separated by semicolon (;). This also means that only simple commands (without embedded semicolon) can be defined. <br>
  2433. <br>
  2434. Favorite commands are fhem commands with an optional alias for the command given. The alias can be sent as message (instead of the favoriteCmd) to execute the command. Before the favorite command also an alias (other shortcut for the favorite) or/and a descriptive text (enclosed in []) can be specifed. If alias or description is specified this needs to be prefixed with a '/' and the alias if given needs to be specified first.
  2435. <br>
  2436. <br>
  2437. Example: Assuming cmdFavorites is set to a value of <code>favorite</code> and this attribute is set to a value of
  2438. <br><code>get lights status; /light=set lights on; /dark[Make it dark]=set lights off; /heating=set heater; /[status]=get heater status;</code> <br>
  2439. <ul>
  2440. <li>Then a message "favorite1" to the bot would execute the command <code>get lights status</code></li>
  2441. <li>A message "favorite 2" or "/light" to the bot would execute the command <code>set lights on</code>. And the favorite would show as "make it dark" in the list of favorites.</li>
  2442. <li>A message "/heating on" or "favorite 3 on" to the bot would execute the command <code>set heater on</code><br> (Attention the remainder after the alias will be added to the command in fhem!)</li>
  2443. <li>A message "favorite 4" to the bot would execute the command <code>get heater status</code> and this favorite would show as "status" as a description in the favorite list</li>
  2444. </ul>
  2445. <br>
  2446. Favorite commands can also be prefixed with a question mark ('?') to enable a confirmation being requested before executing the command.
  2447. <br>
  2448. Examples: <code>get lights status; /light=?set lights on; /dark=set lights off; ?set heater;</code> <br>
  2449. <br>
  2450. Meaning the full format for a single favorite is <code>/alias[description]=command</code> where the alias can be empty or <code>/alias=command</code> or just the <code>command</code>. In any case the command can be also prefixed with a '?'. Spaces are only allowed in the description and the command, usage of spaces in other areas might lead to wrong interpretation of the definition. Spaces and also many other characters are not supported in the alias commands by telegram, so if you want to have your favorite/alias directly recognized in then telegram app, restriction to letters, digits and underscore is required.
  2451. </li>
  2452. <br>
  2453. <li><code>cmdRestrictedPeer &lt;peer(s)&gt;</code><br>Restrict the execution of commands only to messages sent from the given peername or multiple peernames
  2454. (specified in the form of contact id, username or full name, multiple peers to be separated by a space).
  2455. A message with the cmd and sender is sent to the default peer in case of another peer trying to sent messages<br>
  2456. It is recommended to use only peer ids for this restriction to reduce spoofing risk!
  2457. </li>
  2458. <li><code>allowUnknownContacts &lt;1 or 0&gt;</code><br>Allow new contacts to be added automatically (1 - Default) or restrict message reception only to known contacts and unknwown contacts will be ignored (0).
  2459. </li>
  2460. <li><code>saveStateOnContactChange &lt;1 or 0&gt;</code><br>Allow statefile being written on every new contact found, ensures new contacts not being lost on any loss of statefile. Default is on (1).
  2461. </li>
  2462. <li><code>cmdReturnEmptyResult &lt;1 or 0&gt;</code><br>Return empty (success) message for commands (default). Otherwise return messages are only sent if a result text or error message is the result of the command execution.
  2463. </li>
  2464. <li><code>allowedCommands &lt;list of command&gt;</code><br>Restrict the commands that can be executed through favorites and cmdKeyword to the listed commands (separated by space). Similar to the corresponding restriction in FHEMWEB. The allowedCommands will be set on the corresponding instance of an allowed device with the name "allowed_&lt;TelegrambotDeviceName&gt; and not on the telegramBotDevice! This allowed device is created and modified automatically.<br>
  2465. <b>ATTENTION: This is not a hardened secure blocking of command execution, there might be ways to break the restriction!</b>
  2466. </li>
  2467. <li><code>cmdTriggerOnly &lt;0 or 1&gt;</code><br>Restrict the execution of commands only to trigger command. If this attr is set (value 1), then only the name of the trigger even has to be given (i.e. without the preceding statement trigger).
  2468. So if for example cmdKeyword is set to <code>ok fhem</code> and cmdTriggerOnly is set, then a message of <code>ok fhem someMacro</code> would execute the fhem command <code>trigger someMacro</code>.<br>
  2469. Note: This is deprecated and will be removed in one of the next releases
  2470. </li>
  2471. <li><code>queryAnswerText &lt;text&gt;</code><br>Specify the automatic answering to buttons send through queryInline command. If this attribute is set an automatic answer is provided to the press of the inline button. The text in the attribute is evaluated through set-logic, so that readings and also perl code can be stored here. The result of the translation with set-logic will be sent as a text with the answer (this text is currently limited by telegram to 200 characters). <br>
  2472. Note: A value of "0" in the attribute or as result of the evaluation will result in no text being sent with the answer.
  2473. <br>
  2474. Note: If the peer sending the button is not authorized an answer is always sent without any text.
  2475. </li>
  2476. <br>
  2477. <li><code>pollingTimeout &lt;number&gt;</code><br>Used to specify the timeout for long polling of updates. A value of 0 is switching off any long poll.
  2478. In this case no updates are automatically received and therefore also no messages can be received. It is recommended to set the pollingtimeout to a reasonable time between 15 (not too short) and 60 (to avoid broken connections). See also attribute <code>disable</code>.
  2479. </li>
  2480. <li><code>pollingVerbose &lt;0_None 1_Digest 2_Log&gt;</code><br>Used to limit the amount of logging for errors of the polling connection. These errors are happening regularly and usually are not consider critical, since the polling restarts automatically and pauses in case of excess errors. With the default setting "1_Digest" once a day the number of errors on the last day is logged (log level 3). With "2_Log" every error is logged with log level 2. With the setting "0_None" no errors are logged. In any case the count of errors during the last day and the last error is stored in the readings <code>PollingErrCount</code> and <code>PollingLastError</code> </li>
  2481. <li><code>disable &lt;0 or 1&gt;</code><br>Used to disable the polling if set to 1 (default is 0).
  2482. </li>
  2483. <br>
  2484. <li><code>cmdTimeout &lt;number&gt;</code><br>Used to specify the timeout for sending commands. The default is a value of 30 seconds, which should be normally fine for most environments. In the case of slow or on-demand connections to the internet this parameter can be used to specify a longer time until a connection failure is considered.
  2485. </li>
  2486. <br>
  2487. <li><code>maxFileSize &lt;number of bytes&gt;</code><br>Maximum file size in bytes for transfer of files (images). If not set the internal limit is specified as 10MB (10485760B).
  2488. </li>
  2489. <li><code>filenameUrlEscape &lt;0 or 1&gt;</code><br>Specify if filenames can be specified using url escaping, so that special chanarcters as in URLs. This specifically allows to specify spaces in filenames as <code>%20</code>. Default is off (0).
  2490. </li>
  2491. <li><code>maxReturnSize &lt;number of chars&gt;</code><br>Maximum size of command result returned as a text message including header (Default is unlimited). The internal shown on the device is limited to 1000 chars.
  2492. </li>
  2493. <li><code>maxRetries &lt;0,1,2,3,4,5&gt;</code><br>Specify the number of retries for sending a message in case of a failure. The first retry is sent after 10sec, the second after 100, then after 1000s (~16min), then after 10000s (~2.5h), then after approximately a day. Setting the value to 0 (default) will result in no retries.
  2494. </li>
  2495. <br>
  2496. <li><code>textResponseConfirm &lt;TelegramBot FHEM : $peer\n Bestätigung \n&gt;</code><br>Text to be sent when a confirmation for a command is requested. Default is shown here and $peer will be replaced with the actual contact full name if added.
  2497. </li>
  2498. <li><code>textResponseFavorites &lt;TelegramBot FHEM : $peer\n Favoriten \n&gt;</code><br>Text to be sent as starter for the list of favorites. Default is shown here and $peer will be replaced with the actual contact full name if added.
  2499. </li>
  2500. <li><code>textResponseCommands &lt;TelegramBot FHEM : $peer\n Letzte Befehle \n&gt;</code><br>Text to be sent as starter for the list of last commands. Default is shown here and $peer will be replaced with the actual contact full name if added.
  2501. </li>
  2502. <li><code>textResponseResult &lt;TelegramBot FHEM : $peer\n Befehl:$cmd:\n Ergebnis:\n$result\n&gt;</code><br>Text to be sent as result for a cmd execution. Default is shown here and $peer will be replaced with the actual contact full name if added. Similarly $cmd and $result will be replaced with the cmd and the execution result. If the result is a response with just spaces, or other separator characters the result will be not sent at all (i.e. a values of "\n") will result in no message at all.
  2503. </li>
  2504. <li><code>textResponseUnauthorized &lt;UNAUTHORIZED: TelegramBot FHEM request from user :$peer\n Msg: $msg&gt;</code><br>Text to be sent as warning for unauthorized command requests. Default is shown here and $peer will be replaced with the actual contact full name and id if added. $msg will be replaced with the sent message.
  2505. </li>
  2506. </ul>
  2507. <br><br>
  2508. <a name="TelegramBotreadings"></a>
  2509. <b>Readings</b>
  2510. <br><br>
  2511. <ul>
  2512. <li>Contacts &lt;text&gt;<br>The current list of contacts known to the telegram bot.
  2513. Each contact is specified as a triple in the same form as described above. Multiple contacts separated by a space. </li>
  2514. <br>
  2515. <li>msgId &lt;text&gt;<br>The id of the last received message is stored in this reading.
  2516. For secret chats a value of -1 will be given, since the msgIds of secret messages are not part of the consecutive numbering</li>
  2517. <li>msgPeer &lt;text&gt;<br>The sender name of the last received message (either full name or if not available @username)</li>
  2518. <li>msgPeerId &lt;text&gt;<br>The sender id of the last received message</li>
  2519. <li>msgText &lt;text&gt;<br>The last received message text is stored in this reading. Information about special messages like documents, audio, video, locations or venues will be also stored in this reading</li>
  2520. <li>msgFileId &lt;fileid&gt;<br>The last received message file_id (Audio, Photo, Video, Voice or other Document) is stored in this reading.</li>
  2521. <li>msgReplyMsgId &lt;text&gt;<br>Contains the message id of the original message, that this message was a reply to</li>
  2522. <br>
  2523. <li>prevMsgId &lt;text&gt;<br>The id of the SECOND last received message is stored in this reading</li>
  2524. <li>prevMsgPeer &lt;text&gt;<br>The sender name of the SECOND last received message (either full name or if not available @username)</li>
  2525. <li>prevMsgPeerId &lt;text&gt;<br>The sender id of the SECOND last received message</li>
  2526. <li>prevMsgText &lt;text&gt;<br>The SECOND last received message text is stored in this reading</li>
  2527. <li>prevMsgFileId &lt;fileid&gt;<br>The SECOND last received file id is stored in this reading</li>
  2528. <br><b>Note: All prev... Readings are not triggering events</b><br>
  2529. <br>
  2530. <li>sentMsgId &lt;text&gt;<br>The id of the last sent message is stored in this reading, if not succesful the id is empty</li>
  2531. <li>sentMsgResult &lt;text&gt;<br>The result of the send process for the last message is contained in this reading - SUCCESS if succesful</li>
  2532. <br>
  2533. <li>StoredCommands &lt;text&gt;<br>A list of the last commands executed through TelegramBot. Maximum 10 commands are stored.</li>
  2534. <br>
  2535. <li>PollingErrCount &lt;number&gt;<br>Show the number of polling errors during the last day. The number is reset at the beginning of the next day.</li>
  2536. <li>PollingLastError &lt;number&gt;<br>Last error message that occured during a polling update call</li>
  2537. <br>
  2538. <li>callbackID &lt;id&gt; / callbackPeerId &lt;peer id&gt; / callbackPeer &lt;peer&gt;<br>Contains the query ID (respective the peer id and peer name) of the last received inline query from an inline query button (see set ... inline)</li>
  2539. </ul>
  2540. <br><br>
  2541. <a name="TelegramBotexamples"></a>
  2542. <b>Examples</b>
  2543. <br><br>
  2544. <ul>
  2545. <li>Send a telegram message if fhem has been newly started
  2546. <p>
  2547. <code>define notify_fhem_reload notify global:INITIALIZED set &lt;telegrambot&gt; message fhem started - just now </code>
  2548. </p>
  2549. </li>
  2550. <br>
  2551. <li>A command, that will retrieve an SVG plot and send this as a message back (can be also defined as a favorite).
  2552. <p>
  2553. Send the following message as a command to the bot <code>ok fhem { plotAsPng('SVG_FileLog_Aussen') }</code>
  2554. <br>assuming <code>ok fhem</code> is the command keyword)
  2555. </p> (
  2556. The png picture created by plotAsPng will then be send back in image format to the telegram client. This also works with other pictures returned and should also work with other media files (e.g. MP3 and doc files). The command can also be defined in a favorite.<br>
  2557. Remark: Example requires librsvg installed
  2558. </li>
  2559. <br>
  2560. <li>Allow telegram bot commands to be used<br>
  2561. If the keywords for commands are starting with a slash (/), the corresponding commands can be also defined with the
  2562. <a href=http://botsfortelegram.com/project/the-bot-father/>Bot Father</a>. So if a slash is typed a list of the commands will be automatically shown. Assuming that <code>cmdSentCommands</code> is set to <code>/History</code>. Then you can initiate the communication with the botfather, select the right bot and then with the command <code>/setcommands</code> define one or more commands like
  2563. <p>
  2564. <code>History-Show a history of the last 10 executed commands</code>
  2565. </p>
  2566. When typing a slash, then the text above will immediately show up in the client.
  2567. </li>
  2568. </ul>
  2569. </ul>
  2570. =end html
  2571. =cut