From bc83bed022c72f38fa85e9c93710b81645755ed8 Mon Sep 17 00:00:00 2001 From: viegener <> Date: Wed, 20 Dec 2017 11:29:39 +0000 Subject: [PATCH] 50_TelegramBot: new set cmd silentmsg to disable_notification git-svn-id: https://svn.fhem.de/fhem/trunk@15652 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/50_TelegramBot.pm | 91 +++++++++++++++++++++++++++++-------- 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 8c5c191fd..9bdf2267e 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # 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. + - feature: 50_TelegramBot: new set cmd silentmsg to disable_notification - bugfix: 73_GardenaSmart: v0.4.0 bugfixes - feature: 31_PLAYBULB: v1.4.0 support for Sphere Model BTL301w - change: 77_UWZ: v2.0.0, replace Iconv-Converter with perl encode() diff --git a/fhem/FHEM/50_TelegramBot.pm b/fhem/FHEM/50_TelegramBot.pm index bdeb95290..71cace73a 100644 --- a/fhem/FHEM/50_TelegramBot.pm +++ b/fhem/FHEM/50_TelegramBot.pm @@ -138,24 +138,28 @@ # Fix minusdesc undefined issue # Cleanup old code +# add favoritesMenu to send favorites +# doc favoritesMenu +# correct favoritesMenu to allow parameter +# 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 # ############################################################################## # TASKS # -# \n in inline keyboards -# # queryDialogStart / queryDialogEnd - keep msg id # -# -# # remove keyboard after favorite confirm # # cleanup encodings # # replyKeyboardRemove - #msg592808 # -# add an option to send silent messages - msg556631 +# add an option to send silent messages - msg556631 ?? +# \n in inline keyboards - not possible currently # ############################################################################## @@ -207,6 +211,8 @@ my %sets = ( "message" => "textField", "msg" => "textField", "send" => "textField", + + "silentmsg" => "textField", "msgDelete" => "textField", @@ -223,9 +229,11 @@ my %sets = ( "sendDocument" => "textField", "sendMedia" => "textField", "sendVoice" => "textField", - + "sendLocation" => "textField", + "favoritesMenu" => "textField", + "cmdSend" => "textField", "replaceContacts" => "textField", @@ -464,12 +472,13 @@ sub TelegramBot_Set($@) 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 =~ /^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 =~ /^send.*/ ) ) { my $msgid; my $msg; my $addPar; my $sendType = 0; + my $options = ""; my $peers; my $inline = 0; @@ -480,7 +489,9 @@ sub TelegramBot_Set($@) $numberOfArgs--; $inline = 1 if ($cmd eq 'queryEditInline'); } elsif ($cmd eq 'msgForceReply') { - $addPar = "{\"force_reply\":true}"; + $options .= " -force_reply- "; + } elsif ($cmd eq 'silentmsg') { + $options .= " -silent- "; } elsif ($cmd eq 'queryInline') { $inline = 1; } @@ -587,9 +598,32 @@ sub TelegramBot_Set($@) } Log3 $name, 5, "TelegramBot_Set $name: start send for cmd :$cmd: and sendType :$sendType:"; - $ret = TelegramBot_SendIt( $hash, $peers, $msg, $addPar, $sendType, $msgid ); + $ret = TelegramBot_SendIt( $hash, $peers, $msg, $addPar, $sendType, $msgid, $options ); + } elsif($cmd eq 'favoritesMenu') { + + my $peers; + if ( int(@args) > 0 ) { + while ( $args[0] =~ /^@(..+)$/ ) { + my $ppart = $1; + return "TelegramBot_Set: Command $cmd, need exactly one peer" if ( ( defined( $peers ) ) ); + $peers = (defined($peers)?$peers." ":"").$ppart; + shift @args; + last if ( int(@args) == 0 ); + } + return "TelegramBot_Set: Command $cmd, addiitonal parameter specified" if ( int(@args) >= 1 ); + } + + if ( ! defined( $peers ) ) { + $peers = AttrVal($name,'defaultPeer',undef); + return "TelegramBot_Set: Command $cmd, without explicit peer requires defaultPeer being set" if ( ! defined($peers) ); + } + + return "TelegramBot_Set: Command $cmd, no favorites defined" if ( ! defined( AttrVal($name,'favorites',undef) ) ); + + TelegramBot_SendFavorites($hash, $peers, undef, "", undef, undef, 0); + } elsif($cmd eq 'cmdSend') { return "TelegramBot_Set: Command $cmd, no peers and no text/file specified" if ( $numberOfArgs < 2 ); @@ -598,7 +632,7 @@ sub TelegramBot_Set($@) my $peers; while ( $args[0] =~ /^@(..+)$/ ) { my $ppart = $1; - return "TelegramBot_Set: Command $cmd, need exactly one peer" if ( ($cmd eq 'reply') && ( defined( $peers ) ) ); + return "TelegramBot_Set: Command $cmd, need exactly one peer" if ( ( defined( $peers ) ) ); $peers .= " " if ( defined( $peers ) ); $peers = "" if ( ! defined( $peers ) ); $peers .= $ppart; @@ -1052,6 +1086,7 @@ sub TelegramBot_SendFavorites($$$$$;$$) { my $name = $hash->{NAME}; $aliasExec = 0 if ( ! $aliasExec ); + $iscallback = 0 if ( ! $iscallback ); my $ret; @@ -1641,7 +1676,7 @@ sub TelegramBot_SendIt($$$$$;$$$) } Log3 $name, 5, "TelegramBot_SendIt $name: try to send message to :$peer: -:". - TelegramBot_MsgForLog($msg, ($isMedia<0) ).": - :".(defined($addPar)?$addPar:"").":"; + TelegramBot_MsgForLog($msg, ($isMedia<0) ).": - :".(defined($addPar)?$addPar:"").":".": options :".$options.":"; # trim and convert spaces in peer to underline my $peer2 = TelegramBot_GetIdForPeer( $hash, $peer ); @@ -1798,7 +1833,14 @@ sub TelegramBot_SendIt($$$$$;$$$) if ( defined( $addPar ) ) { $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "reply_markup", undef, $addPar, 0 ) if ( ! defined( $ret ) ); + } elsif ( $options =~ /-force_reply-/ ) { + $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "reply_markup", undef, "{\"force_reply\":true}", 0 ) if ( ! defined( $ret ) ); } + + if ( $options =~ /-silent-/ ) { + $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, "disable_notification", undef, "true", 0 ) if ( ! defined( $ret ) ); + } + # finalize multipart $ret = TelegramBot_AddMultipart($hash, $hash->{HU_DO_PARAMS}, undef, undef, undef, 0 ) if ( ! defined( $ret ) ); @@ -1949,14 +1991,21 @@ sub TelegramBot_MakeKeyboard($$$@) } my $refkb = \%par; - - my $json = JSON->new->utf8; - $ret = $json->utf8(0)->encode( $refkb ); - Log3 $name, 4, "TelegramBot_MakeKeyboard $name: json :$ret: is utf8? ".(utf8::is_utf8($ret)?"yes":"no"); - if ( utf8::is_utf8($ret) ) { - utf8::downgrade($ret); - Log3 $name, 4, "TelegramBot_MakeKeyboard $name: json downgraded :$ret: is utf8? ".(utf8::is_utf8($ret)?"yes":"no"); + # encode keyboard with JSON + my $json = JSON->new->utf8->allow_nonref; + eval { + $ret = $json->utf8(0)->encode( $refkb ); + }; + Log3 $name, 2, "JSON encode() did fail with: ".(( $@ )?$@:"") if ( ! $ret ); + + if ( $ret ) { + Log3 $name, 4, "TelegramBot_MakeKeyboard $name: json :$ret: is utf8? ".(utf8::is_utf8($ret)?"yes":"no"); + + if ( utf8::is_utf8($ret) ) { + utf8::downgrade($ret); + Log3 $name, 4, "TelegramBot_MakeKeyboard $name: json downgraded :$ret: is utf8? ".(utf8::is_utf8($ret)?"yes":"no"); + } } return $ret; @@ -3427,6 +3476,9 @@ 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. +
  • +
  • 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.
  • reply <msgid> [ @<peer1> ] <text>
    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. @@ -3438,6 +3490,9 @@ sub TelegramBot_BinaryFileWrite($$$) {
  • msgDelete <msgid> [ @<peer1> ]
    Deletes 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. Restrictions apply for deleting messages in the Bot API as currently specified here (deleteMessage)
  • +
  • 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.
    Example: The following command would sent the resulting SVG picture to the default peer:
    set tbot cmdSend { plotAsPng('SVG_FileLog_Aussen') }