diff --git a/fhem/CHANGED b/fhem/CHANGED index 1cc0537e4..adf9898c5 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - change: 49_TBot_List: deleteonly lists / internal changes + - change: 50_TelegramBot: more silent cmds, caption formatting,several fixes - change: lib/OWNet.pm: updated to latest version from https://sourceforge.net/p/owfs/code/ci/master/tree/module/ ownet/perl5/OWNet/lib/OWNet.pm diff --git a/fhem/FHEM/49_TBot_List.pm b/fhem/FHEM/49_TBot_List.pm index 433c3cd59..27113876a 100644 --- a/fhem/FHEM/49_TBot_List.pm +++ b/fhem/FHEM/49_TBot_List.pm @@ -77,6 +77,15 @@ # confirm delete configurable as attribute confirmDelete # confirm add unsolicited configurable as attribute confirmUnsolicited # 0.6 2017-07-16 confirmDelete & confirmUnsolicited +# +# added list getter for simple text list with \n and empty string if no entries +# switched from fhem( calls to AnalyzeCommandChain +# added count getter for count of list entries +# FIX: Some log entries / issues with inline commands +# new attribute deleteOnly to have deleteonly lists / no changes or adds +# document deleteOnly +# 0.7 2018-03-11 deleteonly lists / internal changes + # ############################################################################## # TASKS @@ -147,6 +156,7 @@ sub TBot_List_Initialize($) { "handleUnsolicited:0,1 ". "confirmDelete:0,1 ". "confirmUnsolicited:0,1 ". + "deleteOnly:0,1 ". "allowedPeers:textField ". $readingFnAttributes; } @@ -234,17 +244,13 @@ sub TBot_List_Set($@) my $addArg = ($args[0] ? join(" ", @args ) : undef); - Log3 $name, 4, "TBot_List_Set $name: Processing TBot_List_Set( $cmd ) - args :".(defined($addArg)?$addArg:"").":"; - # check cmd / handle ? my $ret = TBot_List_CheckSetGet( $hash, $cmd, $hash->{setoptions} ); + return $ret if ( $ret ); + + Log3 $name, 4, "TBot_List_Set $name: Processing TBot_List_Set( $cmd ) - args :".(defined($addArg)?$addArg:"").":"; - if ( $ret ) { - - # This is wrong arg / ? --> just return without log - return $ret; - - } elsif ($cmd eq 'start') { + if ($cmd eq 'start') { Log3 $name, 4, "TBot_List_Set $name: start of dialog requested "; $ret = "start requires a telegrambot and optionally a peer" if ( ( $numberOfArgs < 2 ) && ( $numberOfArgs > 3 ) ); @@ -265,7 +271,7 @@ sub TBot_List_Set($@) $tchat = ReadingsVal( $tbot, "msgChatId", undef ); $tpeer = ReadingsVal( $tbot, "msgPeerId", "" ); } else { - $tpeer = fhem( "get $tbot peerId ".$args[1] ); + $tpeer = AnalyzeCommandChain( $hash, "get $tbot peerId ".$args[1] ); } $ret = "No peer found or specified :$tbot: ".(( $numberOfArgs == 2 )?"":$args[1]) if ( ! $tpeer ); } @@ -299,7 +305,7 @@ sub TBot_List_Set($@) } if ( ! $ret ) { - $tpeer = fhem( "get $tbot peerId ".$args[1], 1 ); + $tpeer = AnalyzeCommandChain( $hash, "get $tbot peerId ".$args[1], 1 ); $ret = "No peer found or specified :$tbot: ".$args[1] if ( ! $tpeer ); } @@ -331,18 +337,24 @@ sub TBot_List_Get($@) my $cmd = $args[0]; my $arg = $args[1]; - Log3 $name, 5, "TBot_List_Get $name: Processing TBot_List_Get( $cmd )"; - # check cmd / handle ? my $ret = TBot_List_CheckSetGet( $hash, $cmd, $hash->{getoptions} ); + return $ret if ( $ret ); - if ( $ret ) { - # This is wrong arg / ? --> just return without log - return $ret; + Log3 $name, 5, "TBot_List_Get $name: Processing TBot_List_Get( $cmd )"; - } elsif($cmd eq "textList") { + if($cmd eq "textList") { $ret = TBot_List_getTextList($hash); - + + } elsif($cmd eq "list") { + my @list = TBot_List_getList( $hash ); + $ret = ""; + $ret = join("\n", @list ) if ( scalar( @list ) != 0 ); + + } elsif($cmd eq "count") { + my @list = TBot_List_getList( $hash ); + $ret = scalar( @list ); + } elsif($cmd eq 'queryAnswer') { # parameters cmd - queryAnswer if ( $numberOfArgs != 4 ) { @@ -366,7 +378,7 @@ sub TBot_List_Get($@) $ret = "No telegramBot specified :$tbot:" if ( ! TBot_List_isTBot( $hash, $tbot ) ); } if ( ! $ret ) { - $tpeer = fhem( "get $tbot peerId ".$args[2] ); + $tpeer = AnalyzeCommandChain( $hash, "get $tbot peerId ".$args[2] ); $ret = "No peer specified :$tbot: ".$args[2] if ( ! $tpeer ); } @@ -409,7 +421,7 @@ sub TBot_List_Attr(@) { if ( ($aName eq 'optionDouble') ) { $aVal = ($aVal eq "1")? "1": "0"; - } elsif ( ($aName eq "confirmDelete" ) || ($aName eq "confirmUnsolicited" ) ) { + } elsif ( ($aName eq "confirmDelete" ) || ($aName eq "confirmUnsolicited" ) || ($aName eq "deleteOnly" ) ) { $aVal = ($aVal eq "1")? "1": "0"; } elsif ($aName eq 'allowedPeers') { @@ -723,13 +735,15 @@ sub TBot_List_handler($$$$;$) my $ret; - Log3 $name, 4, "JVLISTMGR_handler: $name - $tbot peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); + Log3 $name, 4, "TBot_List_handler: $name - $tbot peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); my $lname = TBot_List_getConfigListname($hash); my $msgId; my $chatId; my @list; + my $donly = AttrVal($name,'deleteOnly',0); + # in start case from group chat both ids will be given and need to be allowed ($peer, $chatId) = split( / /, $peer ); @@ -746,7 +760,7 @@ sub TBot_List_handler($$$$;$) @list = TBot_List_getList( $hash ); } - Log3 $name, 4, "JVLISTMGR_handler: $name - after prefetch peer :$peer: chatId :$chatId: msgId :".($msgId?$msgId:"").": "; + Log3 $name, 4, "TBot_List_handler: $name - after prefetch peer :$peer: chatId :$chatId: msgId :".($msgId?$msgId:"").": "; ##################### if ( $ret ) { @@ -774,7 +788,7 @@ sub TBot_List_handler($$$$;$) if ( defined($msgId ) ) { # show final list - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); TBot_List_setMsgId( $hash, $tbot, $chatId ); TBot_List_setMsgId( $hash, $tbot, $peer, undef, "chat" ); } else { @@ -808,9 +822,15 @@ sub TBot_List_handler($$$$;$) } $inline .= ") " if ( $double == 2 ); - - $inline .= "(".TBot_List_inlinekey( $hash, "ok", "list_ok" )."|".TBot_List_inlinekey( $hash, "ändern", "list_menu" )."|". + + $inline .= "(".TBot_List_inlinekey( $hash, "ok", "list_ok" ); + + if ( $donly ) { + $inline .= "|".TBot_List_inlinekey( $hash, "Leeren", "list_askclr" ).")"; + } else { + $inline .= "|".TBot_List_inlinekey( $hash, "ändern", "list_menu" )."|". TBot_List_inlinekey( $hash, "hinzu", "list_askadd" ).")"; + } my $textmsg = "Liste ".$lname; $textmsg .= " ist leer " if ( scalar(@list) == 0 ); @@ -835,12 +855,12 @@ sub TBot_List_handler($$$$;$) TBot_List_setMsgId( $hash, $tbot, $peer, $chatId, "chat" ); # send msg and keys - fhem( "set ".$tbot." queryInline ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryInline ".'@'.$chatId." $inline $textmsg" ); } else { if ( defined($msgId ) ) { # show new list - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); } else { $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); } @@ -859,10 +879,15 @@ sub TBot_List_handler($$$$;$) my $textmsg = "Liste ".$lname."\nEintrag ".($no+1)." (".$list[$no].") ?"; # show ask msgs (depending on attr) my $indata = ( AttrVal($name,'confirmDelete',1) ? "list_rem-$no" : "list_remyes-$no" ); - my $inline = "(".TBot_List_inlinekey( $hash, "Entfernen", $indata )."|". - TBot_List_inlinekey( $hash, "Aendern", "list_askchg-$no" )."|". - TBot_List_inlinekey( $hash, "Nach Oben", "list_totop-$no" )."|".TBot_List_inlinekey( $hash, "Zurueck", "list_edit" ).")"; - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + my $inline = "(".TBot_List_inlinekey( $hash, "Entfernen", $indata ); + + if ( ! $donly ) { + $inline .= "|".TBot_List_inlinekey( $hash, "Aendern", "list_askchg-$no" )."|". + TBot_List_inlinekey( $hash, "Nach Oben", "list_totop-$no" ); + } + $inline .= "|".TBot_List_inlinekey( $hash, "Zurueck", "list_edit" ).")"; + + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); } else { $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); } @@ -886,8 +911,8 @@ sub TBot_List_handler($$$$;$) my $text = join(",", @list ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); # show updated list -> call recursively TBot_List_handler( $hash, "list_edit", $tbot, $peer, " Nach oben gesetzt" ); @@ -906,7 +931,7 @@ sub TBot_List_handler($$$$;$) my $textmsg = "Liste ".$lname."\nSoll der Eintrag ".($no+1)." (".$list[$no].") entfernt werden?"; # show ask msg my $inline = "(".TBot_List_inlinekey( $hash, "Ja", "list_remyes-$no" )."|".TBot_List_inlinekey( $hash, "Nein", "list_edit" ).")"; - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); } else { $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); } @@ -924,8 +949,8 @@ sub TBot_List_handler($$$$;$) my $text = join(",", @list ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); # show updated list -> call recursively TBot_List_handler( $hash, "list_edit", $tbot, $peer, " Eintrag geloescht" ); @@ -941,7 +966,7 @@ sub TBot_List_handler($$$$;$) # show menu msg my $inline = "(".TBot_List_inlinekey( $hash, "Sortieren", "list_asksrt" )."|".TBot_List_inlinekey( $hash, "Leeren", "list_askclr" )."|". TBot_List_inlinekey( $hash, "Zurück", "list_edit" ).")"; - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); } else { $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); } @@ -955,7 +980,7 @@ sub TBot_List_handler($$$$;$) # show ask msg my $inline = "(".TBot_List_inlinekey( $hash, "Ja - von A-Z", "list_srtyes1" )."|".TBot_List_inlinekey( $hash, "Ja - von Z-A", "list_srtyes2" )."|". TBot_List_inlinekey( $hash, "Nein", "list_edit" ).")"; - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); } else { $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); } @@ -973,8 +998,8 @@ sub TBot_List_handler($$$$;$) @list = sort {$b cmp $a} @list; } my $text = join( ",", @list ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); } # show updated list -> call recursively @@ -989,7 +1014,7 @@ sub TBot_List_handler($$$$;$) my $textmsg = "Liste ".$lname."\nSoll die gesamte Liste ".scalar(@list)." Einträge gelöscht werden?"; # show ask msg my $inline = "(".TBot_List_inlinekey( $hash, "Ja - Liste löschen", "list_clryes" )."|".TBot_List_inlinekey( $hash, "Nein", "list_edit" ).")"; - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); } else { $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); } @@ -997,7 +1022,7 @@ sub TBot_List_handler($$$$;$) ##################### } elsif ( $cmd eq "list_clryes" ) { # means remove all entries - now it is confirmed - fhem( "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); # show updated list -> call recursively TBot_List_handler( $hash, "list_edit", $tbot, $peer, " Liste geloescht" ); @@ -1013,7 +1038,7 @@ sub TBot_List_handler($$$$;$) TBot_List_setMsgId( $hash, $tbot, $chatId, $textmsg, "textmsg" ); # means ask for an entry to be added to the list - fhem( "set ".$tbot." msgForceReply ".'@'.$chatId." $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." msgForceReply ".'@'.$chatId." $textmsg" ); ##################### } elsif ( $cmd eq "list_add" ) { @@ -1028,11 +1053,11 @@ sub TBot_List_handler($$$$;$) $text .= ",".$entry ; } - fhem( "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); } else { - fhem( "set ".TBot_List_getConfigPostMe($hash)." add $lname ".$arg ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." add $lname ".$arg ); } if ( defined($msgId ) ) { @@ -1062,7 +1087,7 @@ sub TBot_List_handler($$$$;$) TBot_List_setMsgId( $hash, $tbot, $chatId, $textmsg, "textmsg" ); # means ask for an entry to be added to the list - fhem( "set ".$tbot." msgForceReply ".'@'.$chatId." $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." msgForceReply ".'@'.$chatId." $textmsg" ); } @@ -1084,8 +1109,8 @@ sub TBot_List_handler($$$$;$) } $nre++; } - fhem( "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); - fhem( "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." clear $lname " ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." add $lname $text" ); } if ( defined($msgId ) ) { @@ -1112,7 +1137,7 @@ sub TBot_List_handler($$$$;$) if ( AttrVal($name,'confirmUnsolicited',1) ) { my $inline = "(".TBot_List_inlinekey( $hash, "Ja", "list_expaddyes" )."|".TBot_List_inlinekey( $hash, "Nein", "list_edit" ).")"; - fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); + AnalyzeCommandChain( $hash, "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" ); } else { # directly add entry --> call recursively TBot_List_handler( $hash, "list_expaddyes", $tbot, $peer ); @@ -1128,7 +1153,7 @@ sub TBot_List_handler($$$$;$) my $addentry = TBot_List_getMsgId( $hash, $tbot, $chatId, "expadd" ); if ( defined($addentry ) ) { - fhem( "set ".TBot_List_getConfigPostMe($hash)." add $lname ".$addentry ); + AnalyzeCommandChain( $hash, "set ".TBot_List_getConfigPostMe($hash)." add $lname ".$addentry ); # show list again -> call recursively if ( defined($msgId ) ) { TBot_List_handler( $hash, "list_edit", $tbot, $peer, " Eintrag hinzugefuegt" ); @@ -1257,6 +1282,8 @@ sub TBot_List_Setup($) { my %gets = ( "queryAnswer" => undef, "textList" => undef, + "list" => undef, + "count" => undef, ); @@ -1344,6 +1371,9 @@ sub TBot_List_Setup($) {
  • textList
    Returns a multiline string containing the list elements or
  • +
  • list
    Returns a multiline string containing the list elements or an empty String +
  • +

    @@ -1367,6 +1397,9 @@ sub TBot_List_Setup($) {
  • confirmDelete <1 or 0>
    If set to 1 the bot will ask for a confirmation if an entry should be deleted. This is the default. With a value of 0 the additional confirmation will not be requested.
  • +
  • deleteOnly <1 or 0>
    If set to 1 the bot will only allow deletion of entries or the complete list (no new entries or entry text can be changed - neither sorting or similar will be possible). Default is 0 (all changes allowed). +
  • +

    diff --git a/fhem/FHEM/50_TelegramBot.pm b/fhem/FHEM/50_TelegramBot.pm index 71cace73a..b4c09a26c 100644 --- a/fhem/FHEM/50_TelegramBot.pm +++ b/fhem/FHEM/50_TelegramBot.pm @@ -144,12 +144,29 @@ # FIX: allow_nonref / eval also for makekeyboard #msg732757 # new set cmd silentmsg for disable_notification - syntax as in msg # INT: change forceReply to options for sendit -# 2.7 2017-12-20 +# 2.7 2017-12-20 set command silentmsg + +# new set cmd silentImage for disable_notification - syntax as in sendImage +# FIX: allow queryAsnwer also with defaultpeer not set #msg757339 +# General. set commands not requiring a peer - internally set peers to 0 for sendit +# FIX: Doc missing end code tag +# Change log to not write _Set/_Get on ? parameter +# attr to handle set / del types for polling/allowedCmds/favorites +# silentInline added +# cmdSendSilent added and documented +# tests and fixes on handling of peers/chats for tbot_list and replies +# FIX: peer names not numeric in send commands +# FIX: disable also sending messages +# FIX: have disable attribute with dropdown +# Allow caption in sendImage also with \n\t +# 2.8 2018-03-11 more silent cmds, caption formatting, several fixes # ############################################################################## # TASKS # +# additional silent commands +# # queryDialogStart / queryDialogEnd - keep msg id # # remove keyboard after favorite confirm @@ -158,7 +175,6 @@ # # replyKeyboardRemove - #msg592808 # -# add an option to send silent messages - msg556631 ?? # \n in inline keyboards - not possible currently # ############################################################################## @@ -213,6 +229,8 @@ my %sets = ( "send" => "textField", "silentmsg" => "textField", + "silentImage" => "textField", + "silentInline" => "textField", "msgDelete" => "textField", @@ -235,6 +253,7 @@ my %sets = ( "favoritesMenu" => "textField", "cmdSend" => "textField", + "cmdSendSilent" => "textField", "replaceContacts" => "textField", "reset" => undef, @@ -291,7 +310,7 @@ sub TelegramBot_Initialize($) { $hash->{SetFn} = "TelegramBot_Set"; $hash->{AttrFn} = "TelegramBot_Attr"; $hash->{AttrList} = "defaultPeer defaultPeerCopy:0,1 cmdKeyword cmdSentCommands favorites:textField-long favoritesInline:0,1 cmdFavorites cmdRestrictedPeer ". "cmdTriggerOnly:0,1 saveStateOnContactChange:1,0 maxFileSize maxReturnSize cmdReturnEmptyResult:1,0 pollingVerbose:1_Digest,2_Log,0_None ". - "cmdTimeout pollingTimeout disable queryAnswerText:textField cmdRespondChat:0,1 ". + "cmdTimeout pollingTimeout disable:1,0 queryAnswerText:textField cmdRespondChat:0,1 ". "allowUnknownContacts:1,0 textResponseConfirm:textField textResponseCommands:textField allowedCommands filenameUrlEscape:1,0 ". "textResponseFavorites:textField textResponseResult:textField textResponseUnauthorized:textField ". "parseModeSend:0_None,1_Markdown,2_HTML,3_InMsg webPagePreview:1,0 utf8Special:1,0 favorites2Col:0,1 ". @@ -444,7 +463,7 @@ sub TelegramBot_Set($@) { my ( $hash, $name, @args ) = @_; - Log3 $name, 4, "TelegramBot_Set $name: called "; + Log3 $name, 5, "TelegramBot_Set $name: called "; ### Check Args my $numberOfArgs = int(@args); @@ -452,8 +471,6 @@ sub TelegramBot_Set($@) my $cmd = shift @args; - Log3 $name, 4, "TelegramBot_Set $name: Processing TelegramBot_Set( $cmd )"; - if (!exists($sets{$cmd})) { my @cList; foreach my $k (keys %sets) { @@ -470,9 +487,13 @@ sub TelegramBot_Set($@) return "TelegramBot_Set: Unknown argument $cmd, choose one of " . join(" ", @cList); } # error unknown cmd handling + Log3 $name, 4, "TelegramBot_Set $name: Processing TelegramBot_Set( $cmd )"; + my $ret = undef; - 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 eq 'silentmsg') || ($cmd =~ /^send.*/ ) ) { + 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 eq 'silentmsg') || ($cmd eq 'silentImage') || ($cmd eq 'silentInline') || ($cmd =~ /^send.*/ ) ) { my $msgid; my $msg; @@ -481,21 +502,24 @@ sub TelegramBot_Set($@) my $options = ""; my $peers; my $inline = 0; + my $needspeer = 1; if ( ($cmd eq 'reply') || ($cmd eq 'msgEdit' ) || ($cmd eq 'queryEditInline' ) ) { - return "TelegramBot_Set: Command $cmd, no peer, msgid and no text/file specified" if ( $numberOfArgs < 3 ); + return "TelegramBot_Set: Command $cmd, no msgid and no text/file specified" if ( $numberOfArgs < 3 ); $msgid = shift @args; return "TelegramBot_Set: Command $cmd, msgId must be given as first parameter before peer" if ( $msgid =~ /^@/ ); $numberOfArgs--; - $inline = 1 if ($cmd eq 'queryEditInline'); - } elsif ($cmd eq 'msgForceReply') { - $options .= " -force_reply- "; - } elsif ($cmd eq 'silentmsg') { - $options .= " -silent- "; - } elsif ($cmd eq 'queryInline') { - $inline = 1; + # all three messages need also a peer/chat_id + } elsif ($cmd eq 'queryAnswer') { + $needspeer = 0; } + # special options + $inline = 1 if ( ($cmd eq 'queryInline') || ($cmd eq 'queryEditInline') || ($cmd eq 'silentInline') ); + $options .= " -force_reply- " if ($cmd eq 'msgForceReply'); + $options .= " -silent- " if ( ($cmd eq 'silentmsg') || ($cmd eq 'silentImage') || ($cmd eq 'silentInline') ) ; + + return "TelegramBot_Set: Command $cmd, no peers and no text/file specified" if ( $numberOfArgs < 2 ); # numberOfArgs might not be correct beyond this point @@ -509,15 +533,17 @@ sub TelegramBot_Set($@) shift @args; last if ( int(@args) == 0 ); } - + return "TelegramBot_Set: Command $cmd, no msg content specified" if ( int(@args) < 1 ); - - if ( ! defined( $peers ) ) { + + if ( ($needspeer ) && ( ! defined( $peers ) ) ) { $peers = AttrVal($name,'defaultPeer',undef); return "TelegramBot_Set: Command $cmd, without explicit peer requires defaultPeer being set" if ( ! defined($peers) ); + } elsif ( ! defined( $peers ) ) { + $peers = 0; } - if ( ($cmd eq 'sendPhoto') || ($cmd eq 'sendImage') || ($cmd eq 'image') ) { + if ( ($cmd eq 'sendPhoto') || ($cmd eq 'sendImage') || ($cmd eq 'image') || ($cmd eq 'silentImage') ) { $sendType = 1; } elsif ($cmd eq 'sendVoice') { $sendType = 2; @@ -624,10 +650,13 @@ sub TelegramBot_Set($@) TelegramBot_SendFavorites($hash, $peers, undef, "", undef, undef, 0); - } elsif($cmd eq 'cmdSend') { + } elsif($cmd =~ 'cmdSend(Silent)?') { return "TelegramBot_Set: Command $cmd, no peers and no text/file specified" if ( $numberOfArgs < 2 ); # numberOfArgs might not be correct beyond this point + + my $options = ""; + $options .= " -silent- " if ( ($cmd eq 'cmdSendSilent') ) ; my $peers; while ( $args[0] =~ /^@(..+)$/ ) { @@ -670,7 +699,7 @@ sub TelegramBot_Set($@) ( $isMediaStream ) = TelegramBot_IdentifyStream( $hash, $msg ) if ( defined( $msg ) ); Log3 $name, 5, "TelegramBot_Set $name: start send for cmd :$cmd: and isMediaStream :$isMediaStream:"; - $ret = TelegramBot_SendIt( $hash, $peers, $msg, undef, $isMediaStream, undef ); + $ret = TelegramBot_SendIt( $hash, $peers, $msg, undef, $isMediaStream, undef, $options ); } elsif($cmd eq 'msgDelete') { @@ -737,7 +766,7 @@ sub TelegramBot_Set($@) Log3 $name, 5, "TelegramBot_Set $name: contacts newly set "; } - + if ( ! defined( $ret ) ) { Log3 $name, 5, "TelegramBot_Set $name: $cmd done succesful: "; } else { @@ -761,8 +790,6 @@ sub TelegramBot_Get($@) my $cmd = $args[0]; my $arg = ($args[1] ? $args[1] : ""); - Log3 $name, 5, "TelegramBot_Get $name: Processing TelegramBot_Get( $cmd )"; - if(!exists($gets{$cmd})) { my @cList; foreach my $k (sort keys %gets) { @@ -779,6 +806,7 @@ sub TelegramBot_Get($@) return "TelegramBot_Get: Unknown argument $cmd, choose one of " . join(" ", @cList); } # error unknown cmd handling + Log3 $name, 4, "TelegramBot_Get $name: Processing TelegramBot_Get( $cmd )"; my $ret = undef; @@ -843,21 +871,22 @@ sub TelegramBot_Attr(@) { } else { Log3 $name, 5, "TelegramBot_Attr $name: $cmd on $aName to "; } + # $cmd can be "del" or "set" # $name is device name # aName and aVal are Attribute name and value - if ($cmd eq "set") { - if ($aName eq 'favorites') { - # Empty current alias list in hash - if ( defined( $hash->{AliasCmds} ) ) { - foreach my $key (keys %{$hash->{AliasCmds}} ) - { - delete $hash->{AliasCmds}{$key}; - } - } else { - $hash->{AliasCmds} = {}; - } + if ($aName eq 'favorites') { + # Empty current alias list in hash + if ( defined( $hash->{AliasCmds} ) ) { + foreach my $key (keys %{$hash->{AliasCmds}} ) + { + delete $hash->{AliasCmds}{$key}; + } + } else { + $hash->{AliasCmds} = {}; + } + if ($cmd eq "set") { # keep double ; for inside commands $aVal =~ s/;;/SeMiCoLoN/g; my @clist = split( /;/, $aVal); @@ -887,8 +916,42 @@ sub TelegramBot_Attr(@) { # set attribute value to newly combined commands $attr{$name}{'favorites'} = $newVal; $aVal = $newVal; + } + + } elsif ($aName eq 'allowedCommands') { + my $allowedName = "allowed_$name"; + my $exists = ($defs{$allowedName} ? 1 : 0); + my $alcmd = (($cmd eq "set")?$aVal:""); + AnalyzeCommand(undef, "defmod $allowedName allowed"); + AnalyzeCommand(undef, "attr $allowedName validFor $name"); + AnalyzeCommand(undef, "attr $allowedName $aName ".$alcmd); + Log3 $name, 3, "TelegramBot_Attr $name: ".($exists ? "modified":"created")." $allowedName with commands :$alcmd:"; + # allowedCommands only set on the corresponding allowed_device + return "\"TelegramBot_Attr: \" $aName ".($exists ? "modified":"created")." $allowedName with commands :$alcmd:" + + } elsif ($aName eq 'pollingTimeout') { + return "\"TelegramBot_Attr: \" $aName needs to be given in digits only" if ( ($cmd eq "set") && ( $aVal !~ /^[[:digit:]]+$/ ) ); + # let all existing methods run into block + RemoveInternalTimer($hash); + $hash->{POLLING} = -1; + + # wait some time before next polling is starting + TelegramBot_ResetPolling( $hash ); - } elsif ($aName eq 'cmdRestrictedPeer') { + } elsif ($aName eq 'disable') { + return "\"TelegramBot_Attr: \" $aName needs to be 1 or 0" if ( ($cmd eq "set") && ( $aVal !~ /^(1|0)$/ ) ); + # let all existing methods run into block + RemoveInternalTimer($hash); + $hash->{POLLING} = -1; + + # wait some time before next polling is starting + TelegramBot_ResetPolling( $hash ); + + + # attributes where only the set is relevant for syntax check + } elsif ($cmd eq "set") { + + if ($aName eq 'cmdRestrictedPeer') { $aVal =~ s/^\s+|\s+$//g; } elsif ( ($aName eq 'defaultPeerCopy') || @@ -903,53 +966,13 @@ sub TelegramBot_Attr(@) { ($aName eq 'maxRetries') ) { return "\"TelegramBot_Attr: \" $aName needs to be given in digits only" if ( $aVal !~ /^[[:digit:]]+$/ ); - } elsif ($aName eq 'pollingTimeout') { - return "\"TelegramBot_Attr: \" $aName needs to be given in digits only" if ( $aVal !~ /^[[:digit:]]+$/ ); - # let all existing methods run into block - RemoveInternalTimer($hash); - $hash->{POLLING} = -1; - - # wait some time before next polling is starting - TelegramBot_ResetPolling( $hash ); - - } elsif ($aName eq 'disable') { - if ( $aVal =~ /^(1|0)$/ ) { - # let all existing methods run into block - RemoveInternalTimer($hash); - $hash->{POLLING} = -1; - - # wait some time before next polling is starting - TelegramBot_ResetPolling( $hash ); - } else { - return "\"TelegramBot_Attr: \" $aName needs to be 1 or 0"; - } } elsif ($aName eq 'pollingVerbose') { return "\"TelegramBot_Attr: \" Incorrect value given for pollingVerbose" if ( $aVal !~ /^((1_Digest)|(2_Log)|(0_None))$/ ); - - } elsif ($aName eq 'allowedCommands') { - my $allowedName = "allowed_$name"; - my $exists = ($defs{$allowedName} ? 1 : 0); - AnalyzeCommand(undef, "defmod $allowedName allowed"); - AnalyzeCommand(undef, "attr $allowedName validFor $name"); - AnalyzeCommand(undef, "attr $allowedName $aName ".$aVal); - Log3 $name, 3, "TelegramBot_Attr $name: ".($exists ? "modified":"created")." $allowedName with commands :$aVal:"; - # allowedCommands only set on the corresponding allowed_device - return "\"TelegramBot_Attr: \" $aName ".($exists ? "modified":"created")." $allowedName with commands :$aVal:" - } $_[3] = $aVal; - } elsif ($cmd eq "set") { - if ( ($aName eq 'pollingTimeout') || ($aName eq 'disable') ) { - # let all existing methods run into block - RemoveInternalTimer($hash); - $hash->{POLLING} = -1; - - # wait some time before next polling is starting - TelegramBot_ResetPolling( $hash ); - } } return undef; @@ -1335,7 +1358,7 @@ sub TelegramBot_ReadHandleCommand($$$$$) { my $ret; - Log3 $name, 3, "TelegramBot_ReadHandleCommand $name: cmd found :".$cmd.": "; + Log3 $name, 4, "TelegramBot_ReadHandleCommand $name: cmd found :".$cmd.": "; Log3 $name, 5, "TelegramBot_ReadHandleCommand cmd correct peer "; # Either no peer defined or cmdpeer matches peer for message -> good to execute @@ -1394,6 +1417,7 @@ sub TelegramBot_ExecuteCommand($$$$;$$) { # Check for image/doc/audio stream in return (-1 image ( $isMediaStream ) = TelegramBot_IdentifyStream( $hash, $ret ) if ( defined( $ret ) ); + Log3 $name, 3, "TelegramBot_ExecuteCommand $name: cmd executed :".$cmd.": --> :".TelegramBot_MsgForLog($ret, $isMediaStream ).":" if ( $ret ); } Log3 $name, 4, "TelegramBot_ExecuteCommand result for analyze :".TelegramBot_MsgForLog($ret, $isMediaStream ).": "; @@ -1644,6 +1668,9 @@ sub TelegramBot_SendIt($$$$$;$$$) $args[$TelegramBot_arg_retrycnt] = $retryCount+1; Log3 $name, 5, "TelegramBot_SendIt $name: called "; + + # ignore all sends if disabled + return if ( AttrVal($name,'disable',0) ); # ensure sentQueue exists $hash->{sentQueue} = [] if ( ! defined( $hash->{sentQueue} ) ); @@ -1676,10 +1703,15 @@ sub TelegramBot_SendIt($$$$$;$$$) } Log3 $name, 5, "TelegramBot_SendIt $name: try to send message to :$peer: -:". - TelegramBot_MsgForLog($msg, ($isMedia<0) ).": - :".(defined($addPar)?$addPar:"").":".": options :".$options.":"; + TelegramBot_MsgForLog($msg, ($isMedia<0) ).": - add :".(defined($addPar)?$addPar:""). + ": - replyid :".(defined($replyid)?$replyid:""). + ":".": options :".$options.":"; # trim and convert spaces in peer to underline - my $peer2 = TelegramBot_GetIdForPeer( $hash, $peer ); + $peer = 0 if ( ! $peer ); # ensure peer is defined + my $peer2 = (! $peer )?$peer:TelegramBot_GetIdForPeer( $hash, $peer ); + +# Debug "peer :$peer: peer2 :$peer2:"; if ( ! defined( $peer2 ) ) { $ret = "FAILED peer not found :$peer:"; @@ -1709,7 +1741,7 @@ sub TelegramBot_SendIt($$$$$;$$$) if ( ! defined( $ret ) ) { # add chat / user id (no file) --> this will also do init - $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "chat_id", undef, $peer2, 0 ); + $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "chat_id", undef, $peer2, 0 ) if ( $peer ); if ( ( $isMedia == 0 ) || ( $isMedia == 10 ) || ( $isMedia == 20 ) ) { if ( $isMedia == 0 ) { @@ -1785,6 +1817,7 @@ sub TelegramBot_SendIt($$$$$;$$$) $hash->{HU_DO_PARAMS}->{url} = TelegramBot_getBaseURL($hash)."answerCallbackQuery"; $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "callback_query_id", undef, $addPar, 0 ) if ( ! defined( $ret ) ); + $addPar = undef; $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "text", undef, $msg, 0 ) if ( ( ! defined( $ret ) ) && ( $msg ) ); @@ -1797,6 +1830,9 @@ sub TelegramBot_SendIt($$$$$;$$$) # add caption if ( defined( $addPar ) ) { + $addPar =~ s/(?{HU_DO_PARAMS}, "caption", undef, $addPar, 0 ) if ( ! defined( $ret ) ); $addPar = undef; } @@ -3386,7 +3422,7 @@ sub TelegramBot_BinaryFileWrite($$$) { 1; =pod -=item summary send and receive of messages through telegram instant messaging +=item summary send and receive of messages through telegram instant messaging =item summary_DE senden und empfangen von Nachrichten durch telegram IM =begin html @@ -3476,7 +3512,7 @@ sub TelegramBot_BinaryFileWrite($$$) {
    -
  • silentmsg ...
    Sends the given message silently (with disabled_notifications) to the recipients. Syntax and parameters are the same as in the send/message command. +
  • silentmsg, silentImage, silentInline ...
    Sends the given message silently (with disabled_notifications) to the recipients. Syntax and parameters are the same as in the corresponding send/message command.
  • msgForceReply [ @<peer1> ... @<peerN> ] <text>
    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. @@ -3493,7 +3529,7 @@ sub TelegramBot_BinaryFileWrite($$$) {
  • favoritesMenu [ @<peer> ]
    send the favorites menu to the corresponding peer if defined
  • -
  • cmdSend [ @<peer1> ... @<peerN> ] <fhem command>
    Executes the given fhem command and then sends the result to the given peers or the default peer.
    +
  • cmdSend|cmdSendSilent [ @<peer1> ... @<peerN> ] <fhem command>
    Executes the given fhem command and then sends the result to the given peers or the default peer (cmdSendSilent does the same as silent message).
    Example: The following command would sent the resulting SVG picture to the default peer:
    set tbot cmdSend { plotAsPng('SVG_FileLog_Aussen') }
  • @@ -3520,6 +3556,10 @@ sub TelegramBot_BinaryFileWrite($$$) {
  • sendVoice [ @<peer1> ... @<peerN>] <file>
    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.
  • + +
  • silentImage ...
    Sends the given image silently (with disabled_notifications) to the recipients. Syntax and parameters are the same as in the sendImage command. +
  • +
  • sendLocation [ @<peer1> ... @<peerN>] <latitude> <longitude>
    Sends a location as pair of coordinates latitude and longitude as floating point numbers