2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-03 16:56:54 +00:00

49_TBot_List: handleUnsolicited & multiline entries

git-svn-id: https://svn.fhem.de/fhem/trunk@13718 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
viegener 2017-03-16 22:47:36 +00:00
parent e0a54bad23
commit 440ba478e4
2 changed files with 197 additions and 92 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # 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. # Do not insert empty lines here, update check depends on it.
- update: 49_TBot_List: handleUnsolicited & multiline entries
- bugfix: 93_DbRep: get svrinfo has only used /opt/fhem/fhem.db (SQLite) - bugfix: 93_DbRep: get svrinfo has only used /opt/fhem/fhem.db (SQLite)
- new: 98_expandJSON: initial check in - new: 98_expandJSON: initial check in
- update: 98_DOIFtools: added hints to the derived operands - update: 98_DOIFtools: added hints to the derived operands

View File

@ -52,18 +52,29 @@
# changed log levels # changed log levels
# corrected listNo calculation on dialog and define # corrected listNo calculation on dialog and define
# 0.2 2017-02-26 Initial SVN Checkin # 0.2 2017-02-26 Initial SVN Checkin
# Add note to documentation on events and event-on attributes
# FIX: corrected allowedPeers definition
# Also support chats in start without args
# respond in chats / still only a single dialog per user allowed
# add entry for messages sent accidentially - absed on dialog
# removed debug statements
# make unsolicited entries configurable
# handle multiline entries --> remove line ends
# handle multiline entries --> multiple entries to be created
#
#
# #
############################################################################## ##############################################################################
# TASKS # TASKS
# #
# Use msgchat to store original chat and respond in chat
# #
# #
# Make texts and addtl buttons configurable # Make texts and addtl buttons configurable
# #
# internal value if waiting for msg or reply -- otherwise notify not looping through events # internal value if waiting for msg or reply -- otherwise notify not looping through events
# #
# add entry for messages sent accidentially - absed on dialog
# #
# TODOs # TODOs
# #
@ -124,7 +135,8 @@ sub TBot_List_Initialize($) {
$hash->{AttrList} = $hash->{AttrList} =
"telegramBots:textField ". "telegramBots:textField ".
"optionDouble:0,1 ". "optionDouble:0,1 ".
"allowedPeers:textfield ". "handleUnsolicited:0,1 ".
"allowedPeers:textField ".
$readingFnAttributes; $readingFnAttributes;
} }
@ -218,7 +230,8 @@ sub TBot_List_Set($@)
if ( $ret ) { if ( $ret ) {
# do nothing if error/ret is defined # This is wrong arg / ? --> just return without log
return $ret;
} elsif ($cmd eq 'start') { } elsif ($cmd eq 'start') {
Log3 $name, 4, "TBot_List_Set $name: start of dialog requested "; Log3 $name, 4, "TBot_List_Set $name: start of dialog requested ";
@ -226,6 +239,7 @@ sub TBot_List_Set($@)
my $tbot; my $tbot;
my $tpeer; my $tpeer;
my $tchat;
if ( ! $ret ) { if ( ! $ret ) {
$tbot = $args[0]; $tbot = $args[0];
$ret = "No telegramBot specified :$tbot:" if ( ! TBot_List_isTBot( $hash, $tbot ) ); $ret = "No telegramBot specified :$tbot:" if ( ! TBot_List_isTBot( $hash, $tbot ) );
@ -237,6 +251,7 @@ sub TBot_List_Set($@)
if ( ! $ret ) { if ( ! $ret ) {
if ( $numberOfArgs == 2 ) { if ( $numberOfArgs == 2 ) {
$tchat = ReadingsVal( $tbot, "msgChatId", undef );
$tpeer = ReadingsVal( $tbot, "msgPeerId", "" ); $tpeer = ReadingsVal( $tbot, "msgPeerId", "" );
} else { } else {
$tpeer = fhem( "get $tbot peerId ".$args[1] ); $tpeer = fhem( "get $tbot peerId ".$args[1] );
@ -257,6 +272,8 @@ sub TBot_List_Set($@)
Log3 $name, 1, "TBot_List_Set $name: Error :".$ret if ( $ret ); Log3 $name, 1, "TBot_List_Set $name: Error :".$ret if ( $ret );
# start uses a botname and an optional peer # start uses a botname and an optional peer
$tpeer .= " ".$tchat if ( defined( $tchat ) );
$ret = TBot_List_handler( $hash, "list", $tbot, $tpeer ) if ( ! $ret ); $ret = TBot_List_handler( $hash, "list", $tbot, $tpeer ) if ( ! $ret );
} elsif($cmd eq 'end') { } elsif($cmd eq 'end') {
@ -277,8 +294,13 @@ sub TBot_List_Set($@)
# start uses a botname and an optional peer # start uses a botname and an optional peer
$ret = TBot_List_handler( $hash, "end", $tbot, $tpeer ) if ( ! $ret ); $ret = TBot_List_handler( $hash, "end", $tbot, $tpeer ) if ( ! $ret );
} elsif($cmd eq 'reset') {
Log3 $name, 4, "TBot_List_Set $name: reset requested ";
TBot_List_Setup( $hash );
} }
Log3 $name, 4, "TBot_List_Set $name: $cmd ".((defined( $ret ))?"failed with :$ret: ":"done succesful "); Log3 $name, 4, "TBot_List_Set $name: $cmd ".((defined( $ret ))?"failed with :$ret: ":"done succesful ");
return $ret return $ret
} }
@ -304,8 +326,8 @@ sub TBot_List_Get($@)
my $ret = TBot_List_CheckSetGet( $hash, $cmd, $hash->{getoptions} ); my $ret = TBot_List_CheckSetGet( $hash, $cmd, $hash->{getoptions} );
if ( $ret ) { if ( $ret ) {
# do nothing if error/ret is defined # This is wrong arg / ? --> just return without log
return $ret;
} elsif($cmd eq "textList") { } elsif($cmd eq "textList") {
$ret = TBot_List_getTextList($hash); $ret = TBot_List_getTextList($hash);
@ -377,7 +399,7 @@ sub TBot_List_Attr(@) {
$aVal = ($aVal eq "1")? "1": "0"; $aVal = ($aVal eq "1")? "1": "0";
} elsif ($aName eq 'allowedPeers') { } elsif ($aName eq 'allowedPeers') {
return "\"TBot_List_Attr: \" $aName needs to be given in digits - and space only" if ( $aVal !~ /^[[:digit: -]]$/ ); return "\"TBot_List_Attr: \" $aName needs to be given in digits - and space only" if ( $aVal !~ /^[[:digit:] -]*$/ );
} }
@ -437,6 +459,11 @@ sub TBot_List_getConfigListname($)
sub TBot_List_getConfigListno($) sub TBot_List_getConfigListno($)
{ {
my ($hash) = @_; my ($hash) = @_;
if ( ! defined($hash->{listno}) ) {
TBot_List_calcListNo($hash);
}
return $hash->{listno}; return $hash->{listno};
} }
@ -499,6 +526,8 @@ sub TBot_List_getList($;$)
$rd = "postme".sprintf("%2.2d",TBot_List_getConfigListno($hash))."Cont"; $rd = "postme".sprintf("%2.2d",TBot_List_getConfigListno($hash))."Cont";
my $listCont = ReadingsVal(TBot_List_getConfigPostMe($hash),$rd,""); my $listCont = ReadingsVal(TBot_List_getConfigPostMe($hash),$rd,"");
$listCont =~ s/[\n\t\r\f]+/ /sg;
my @entries = split( /,/, $listCont ); my @entries = split( /,/, $listCont );
if ( defined( $entry ) ) { if ( defined( $entry ) ) {
@ -553,6 +582,18 @@ sub TBot_List_getMsgId($$$;$) {
return $hash->{inlinechats}{$key}; return $hash->{inlinechats}{$key};
} }
##############################################
# translate multiple lines into comma separated list
#
sub TBot_List_changeMultiLine($) {
my ($text) = @_;
$text =~ s/[\n\t\r\f]+/,/sg;
return $text;
}
############################################################################## ##############################################################################
############################################################################## ##############################################################################
@ -571,64 +612,77 @@ sub TBot_List_handleEvents($$$)
my ($hash, $tbot, $events ) = @_; my ($hash, $tbot, $events ) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $unsolic = AttrVal($name,"handleUnsolicited",0);
# events - look for sentMsgId / msgReplyMsgId # events - look for sentMsgId / msgReplyMsgId
foreach my $event ( @{$events} ) { foreach my $event ( @{$events} ) {
next if(!defined($event)); next if(!defined($event));
# msgPeer is chat here in chats
if ( $event =~ /sentMsgId\:/ ) { if ( $event =~ /sentMsgId\:/ ) {
Log3 $name, 4, "TBot_List_handleEvents $name: found sentMsgId ". $event; Log3 $name, 4, "TBot_List_handleEvents $name: found sentMsgId ". $event;
my $msgPeer = InternalVal( $tbot, "sentMsgPeerId", "" ); my $msgChat = InternalVal( $tbot, "sentMsgPeerId", "" );
my $msgWait = TBot_List_getMsgId( $hash, $tbot, $msgPeer, "textmsg" ); my $msgWait = TBot_List_getMsgId( $hash, $tbot, $msgChat, "textmsg" );
my $msgSent = InternalVal( $tbot, "sentMsgText", "" ); my $msgSent = InternalVal( $tbot, "sentMsgText", "" );
$msgSent =~ s/\s//g; $msgSent =~ s/\s//g;
# Debug "wait :".$msgWait.": sent :".$msgSent.":"; # Debug "wait :".$msgWait.": sent :".$msgSent.": msgPeer/chat :$msgChat:";
if ( defined( $msgWait ) && ( $msgSent eq $msgWait ) ) { if ( defined( $msgWait ) && ( $msgSent eq $msgWait ) ) {
my $arg = ReadingsVal($tbot,"sentMsgId",""); my $arg = ReadingsVal($tbot,"sentMsgId","");
# store key set means a reply is expected # store key set means a reply is expected
if ( defined( TBot_List_getMsgId( $hash, $tbot, $msgPeer, "store") ) ) { if ( defined( TBot_List_getMsgId( $hash, $tbot, $msgChat, "store") ) ) {
# reply received # reply received
TBot_List_setMsgId( $hash, $tbot, $msgPeer, $arg, "reply"); TBot_List_setMsgId( $hash, $tbot, $msgChat, $arg, "reply");
TBot_List_setMsgId( $hash, $tbot, $msgPeer, undef, "store"); TBot_List_setMsgId( $hash, $tbot, $msgChat, undef, "store");
} else { } else {
TBot_List_setMsgId( $hash, $tbot, $msgPeer, $arg ); TBot_List_setMsgId( $hash, $tbot, $msgChat, $arg );
# remove old entry ids from chg entries # remove old entry ids from chg entries
TBot_List_setMsgId( $hash, $tbot, $msgPeer, undef, "entry"); TBot_List_setMsgId( $hash, $tbot, $msgChat, undef, "entry");
} }
# set internal msg # reset internal msg
TBot_List_setMsgId( $hash, $tbot, $msgPeer, undef, "textmsg" ); TBot_List_setMsgId( $hash, $tbot, $msgChat, undef, "textmsg" );
} }
} elsif ( $event =~ /msgReplyMsgId\:/ ) { } elsif ( $event =~ /msgId\:/ ) {
Log3 $name, 4, "TBot_List_handleEvents $name: found msgReplyMsgId ". $event; Log3 $name, 4, "TBot_List_handleEvents $name: found msgId ". $event;
my $msgPeer = ReadingsVal( $tbot, "msgPeerId", "" );
my $msgReplyId = ReadingsVal($tbot,"msgReplyMsgId",""); my $msgReplyId = ReadingsVal($tbot,"msgReplyMsgId","");
$msgReplyId =~ s/\s//g; my $msgChat = ReadingsVal( $tbot, "msgChatId", "" );
my $replyMsg = TBot_List_getMsgId( $hash, $tbot, $msgChat, "reply");
my $hasChat = TBot_List_getMsgId( $hash, $tbot, $msgChat );
my $replyMsg = TBot_List_getMsgId( $hash, $tbot, $msgPeer, "reply"); # distinguish between reply (check for waiting reply)
if ( $replyMsg eq $msgReplyId ) { if ( length($msgReplyId) != 0 ) {
TBot_List_setMsgId( $hash, $tbot, $msgPeer, undef, "reply"); # reply found
# Debug "reply :".$replyMsg.": rece reply :".$msgReplyId.": msgPeer/chat :$msgChat:";
my $msgText = ReadingsVal( $tbot, "msgText", "" ); if ( defined( $replyMsg ) && ( $replyMsg eq $msgReplyId ) ) {
TBot_List_setMsgId( $hash, $tbot, $msgChat, undef, "reply");
my $msgText = ReadingsVal( $tbot, "msgText", "" );
# now check if an id of an entry was stored then this is edit # now check if an id of an entry was stored then this is edit
my $entryno = TBot_List_getMsgId( $hash, $tbot, $msgPeer, "entry"); my $entryno = TBot_List_getMsgId( $hash, $tbot, $msgChat, "entry");
if ( defined( $entryno ) ) { if ( defined( $entryno ) ) {
TBot_List_setMsgId( $hash, $tbot, $msgPeer, undef, "entry"); TBot_List_setMsgId( $hash, $tbot, $msgChat, undef, "entry");
TBot_List_handler( $hash, "list_chg-$entryno", $tbot, $msgPeer, $msgText ); TBot_List_handler( $hash, "list_chg-$entryno", $tbot, $msgChat, $msgText );
} else { } else {
TBot_List_handler( $hash, "list_add", $tbot, $msgPeer, $msgText ); TBot_List_handler( $hash, "list_add", $tbot, $msgChat, $msgText );
}
} }
} elsif( ( defined( $hasChat ) ) && ( ! defined( $replyMsg ) ) && ( $unsolic ) ) {
# not waiting for reply but received message -> ask if entry should be added
my $msgText = ReadingsVal( $tbot, "msgText", "" );
TBot_List_handler( $hash, "list_expadd", $tbot, $msgChat, $msgText );
} }
} }
} }
} }
@ -647,17 +701,26 @@ sub TBot_List_handler($$$$;$)
my $lname = TBot_List_getConfigListname($hash); my $lname = TBot_List_getConfigListname($hash);
my $msgId; my $msgId;
my $chatId;
my @list; my @list;
if ( ! $ret ) { # in start case from group chat both ids will be given and need to be allowed
$ret = "TBot_List_handler: $name - $tbot ERROR - $peer not allowed" if ( ! TBot_List_isAllowed( $hash, $peer ) ); ($peer, $chatId) = split( / /, $peer );
}
$ret = "TBot_List_handler: $name - $tbot ERROR - $peer peer not allowed" if ( ( ! $ret ) && ( ! TBot_List_isAllowed( $hash, $peer ) ) );
$ret = "TBot_List_handler: $name - $tbot ERROR - $chatId chat not allowed" if ( ( ! $ret ) && ( defined($chatId) ) && ( ! TBot_List_isAllowed( $hash, $peer ) ) );
# get Msgid and list as prefetch # get Msgid and list as prefetch
if ( ! $ret ) { if ( ! $ret ) {
$chatId = TBot_List_getMsgId( $hash, $tbot, $peer, "chat" ) if ( ! defined($chatId) );
$chatId = $peer if ( ! defined($chatId) );
$msgId = TBot_List_getMsgId( $hash, $tbot, $peer ); $msgId = TBot_List_getMsgId( $hash, $tbot, $peer );
$msgId = TBot_List_getMsgId( $hash, $tbot, $chatId ) if ( ! defined( $msgId ) );
@list = TBot_List_getList( $hash ); @list = TBot_List_getList( $hash );
} }
Log3 $name, 4, "JVLISTMGR_handler: $name - after prefetch peer :$peer: chatId :$chatId: msgId :$msgId: ";
##################### #####################
if ( $ret ) { if ( $ret ) {
@ -665,40 +728,31 @@ sub TBot_List_handler($$$$;$)
# Log 1,$ret; # Log 1,$ret;
##################### #####################
} elsif ( $cmd eq "list_ok" ) { } elsif ( ( $cmd eq "list_ok" ) || ( $cmd eq "list_done" ) ) {
# ok means clean buttons and show only list # done means clean buttons and show only list
my $textmsg = (defined($arg)?$arg:"DONE"); # default for done
# start the inline
my $inline = " "; my $inline = " ";
# get the list of entries in the list if ( $cmd eq "list_ok" ) {
my $liste = ""; # get the list of entries in the list
foreach my $entry ( @list ) { my $liste = "";
$liste .= "\n".$entry; foreach my $entry ( @list ) {
$liste .= "\n ".$entry;
}
$textmsg = "Liste ".$lname;
$textmsg .= " ist leer " if ( scalar(@list) == 0 );
$textmsg .= " : $arg " if ( defined($arg) );
$textmsg .= $liste;
} }
my $textmsg = "Liste ".$lname;
$textmsg .= " ist leer " if ( scalar(@list) == 0 );
$textmsg .= " : $arg " if ( defined($arg) );
$textmsg .= $liste;
if ( defined($msgId ) ) { if ( defined($msgId ) ) {
# show new list # show final list
fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$peer." $inline $textmsg" ); fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" );
TBot_List_setMsgId( $hash, $tbot, $peer ); TBot_List_setMsgId( $hash, $tbot, $chatId );
TBot_List_setMsgId( $hash, $tbot, $peer, undef, "chat" );
} else { } else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
}
#####################
} elsif ( $cmd eq "list_done" ) {
# done means clean buttons and show only list
if ( defined($msgId ) ) {
# show new list
fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$peer." DONE" );
} else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
} }
##################### #####################
@ -736,25 +790,33 @@ sub TBot_List_handler($$$$;$)
$textmsg .= " : $arg " if ( defined($arg) ); $textmsg .= " : $arg " if ( defined($arg) );
if ( $cmd eq "list" ) { if ( $cmd eq "list" ) {
# remove msgId
# remove msgId if existing
if ( defined($msgId ) ) { if ( defined($msgId ) ) {
# done old list now and start a new list message # done old list now and start a new list message
TBot_List_handler( $hash, "list_done", $tbot, $peer ); TBot_List_handler( $hash, "list_done", $tbot, $peer, "wurde beendet" );
TBot_List_setMsgId( $hash, $tbot, $peer ); } else {
# there might be still a dialog in another chat
my $oldchatId = TBot_List_getMsgId( $hash, $tbot, $peer, "chat" );
TBot_List_handler( $hash, "list_done", $tbot, $peer, "wurde beendet" ) if ( defined( TBot_List_getMsgId( $hash, $tbot, $oldchatId ) ) );
} }
# store text msg to recognize msg id in dummy # store text msg to recognize msg id in dummy
TBot_List_setMsgId( $hash, $tbot, $peer, $textmsg, "textmsg" ); TBot_List_setMsgId( $hash, $tbot, $chatId, $textmsg, "textmsg" );
# store chat
TBot_List_setMsgId( $hash, $tbot, $peer, $chatId, "chat" );
# send msg and keys # send msg and keys
fhem( "set ".$tbot." queryInline ".'@'.$peer." $inline $textmsg" ); fhem( "set ".$tbot." queryInline ".'@'.$chatId." $inline $textmsg" );
} else { } else {
if ( defined($msgId ) ) { if ( defined($msgId ) ) {
# show new list # show new list
fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$peer." $inline $textmsg" ); fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." $inline $textmsg" );
} else { } else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
} }
} }
@ -770,9 +832,9 @@ sub TBot_List_handler($$$$;$)
# show ask for removal # show ask for removal
my $textmsg = "Liste ".$lname."\nEintrag ".($no+1)." (".$list[$no].") ?"; my $textmsg = "Liste ".$lname."\nEintrag ".($no+1)." (".$list[$no].") ?";
# show ask msg # show ask msg
fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$peer." (Entfernen:".$name."\%"."list_rem-$no|Aendern:".$name."\%"."list_askchg-$no) (Nach Oben:".$name."\%"."list_totop-$no|Zurueck:".$name."\%"."list_edit) $textmsg" ); fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." (Entfernen:".$name."\%"."list_rem-$no|Aendern:".$name."\%"."list_askchg-$no) (Nach Oben:".$name."\%"."list_totop-$no|Zurueck:".$name."\%"."list_edit) $textmsg" );
} else { } else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
} }
} }
@ -796,7 +858,7 @@ sub TBot_List_handler($$$$;$)
# show updated list -> call recursively # show updated list -> call recursively
TBot_List_handler( $hash, "list_edit", $tbot, $peer, " Nach oben gesetzt" ); TBot_List_handler( $hash, "list_edit", $tbot, $peer, " Nach oben gesetzt" );
} }
##################### #####################
} elsif ( $cmd =~ /^list_rem-(\d+)$/ ) { } elsif ( $cmd =~ /^list_rem-(\d+)$/ ) {
# means remove a numbered entry from list - first ask # means remove a numbered entry from list - first ask
@ -809,9 +871,9 @@ sub TBot_List_handler($$$$;$)
# show ask for removal # show ask for removal
my $textmsg = "Liste ".$lname."\nSoll der Eintrag ".($no+1)." (".$list[$no].") entfernt werden?"; my $textmsg = "Liste ".$lname."\nSoll der Eintrag ".($no+1)." (".$list[$no].") entfernt werden?";
# show ask msg # show ask msg
fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$peer." (Ja:".$name."\%"."list_remyes-$no) (Nein:".$name."\%"."list_edit) $textmsg" ); fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." (Ja:".$name."\%"."list_remyes-$no) (Nein:".$name."\%"."list_edit) $textmsg" );
} else { } else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
} }
} }
@ -836,9 +898,9 @@ sub TBot_List_handler($$$$;$)
# show ask for removal # show ask for removal
my $textmsg = "Liste ".$lname."\nSoll die gesamte Liste ".scalar(@list)." Einträge gelöscht werden?"; my $textmsg = "Liste ".$lname."\nSoll die gesamte Liste ".scalar(@list)." Einträge gelöscht werden?";
# show ask msg # show ask msg
fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$peer." (Ja - Liste löschen:".$name."\%"."list_clryes|Nein:".$name."\%"."list_edit) $textmsg" ); fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." (Ja - Liste löschen:".$name."\%"."list_clryes|Nein:".$name."\%"."list_edit) $textmsg" );
} else { } else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
} }
##################### #####################
@ -852,19 +914,21 @@ sub TBot_List_handler($$$$;$)
##################### #####################
} elsif ( $cmd eq "list_askadd" ) { } elsif ( $cmd eq "list_askadd" ) {
TBot_List_setMsgId( $hash, $tbot, $peer, $msgId, "store" ); TBot_List_setMsgId( $hash, $tbot, $chatId, $msgId, "store" );
my $textmsg = "Liste ".$lname." Neuen Eintrag eingeben:"; my $textmsg = "Liste ".$lname." Neuen Eintrag eingeben:";
# store text msg to recognize msg id in dummy # store text msg to recognize msg id in dummy
TBot_List_setMsgId( $hash, $tbot, $peer, $textmsg, "textmsg" ); TBot_List_setMsgId( $hash, $tbot, $chatId, $textmsg, "textmsg" );
# means ask for an entry to be added to the list # means ask for an entry to be added to the list
fhem( "set ".$tbot." msgForceReply ".'@'.$peer." $textmsg" ); fhem( "set ".$tbot." msgForceReply ".'@'.$chatId." $textmsg" );
##################### #####################
} elsif ( $cmd eq "list_add" ) { } elsif ( $cmd eq "list_add" ) {
# means add entry to list # means add entry to list
$arg = TBot_List_changeMultiLine( $arg );
# ! means put on top # ! means put on top
if ( $arg =~ /^\!(.+)$/ ) { if ( $arg =~ /^\!(.+)$/ ) {
@ -887,7 +951,7 @@ sub TBot_List_handler($$$$;$)
$ret = undef; $ret = undef;
} else { } else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
} }
##################### #####################
@ -896,18 +960,18 @@ sub TBot_List_handler($$$$;$)
if ( ( $no >= 0 ) && ( $no < scalar(@list) ) ) { if ( ( $no >= 0 ) && ( $no < scalar(@list) ) ) {
TBot_List_setMsgId( $hash, $tbot, $peer, $msgId, "store" ); TBot_List_setMsgId( $hash, $tbot, $chatId, $msgId, "store" );
# remove old entry ids from chg entries # remove old entry ids from chg entries
TBot_List_setMsgId( $hash, $tbot, $peer, $no, "entry" ); TBot_List_setMsgId( $hash, $tbot, $chatId, $no, "entry" );
my $textmsg = "Liste ".$lname." Eintrag ".($no+1)." ändern : ".$list[$no]; my $textmsg = "Liste ".$lname." Eintrag ".($no+1)." ändern : ".$list[$no];
# store text msg to recognize msg id in dummy # store text msg to recognize msg id in dummy
TBot_List_setMsgId( $hash, $tbot, $peer, $textmsg, "textmsg" ); TBot_List_setMsgId( $hash, $tbot, $chatId, $textmsg, "textmsg" );
# means ask for an entry to be added to the list # means ask for an entry to be added to the list
fhem( "set ".$tbot." msgForceReply ".'@'.$peer." $textmsg" ); fhem( "set ".$tbot." msgForceReply ".'@'.$chatId." $textmsg" );
} }
@ -916,6 +980,8 @@ sub TBot_List_handler($$$$;$)
# means add entry to list # means add entry to list
my $no = $1; my $no = $1;
$arg = TBot_List_changeMultiLine( $arg );
if ( ( $no >= 0 ) && ( $no < scalar(@list) ) ) { if ( ( $no >= 0 ) && ( $no < scalar(@list) ) ) {
my $nre = 0; my $nre = 0;
my $text = ""; my $text = "";
@ -933,12 +999,45 @@ sub TBot_List_handler($$$$;$)
if ( defined($msgId ) ) { if ( defined($msgId ) ) {
# show new list -> call recursively # show new list -> call recursively
$ret = "Eintrag hinzugefuegt"; $ret = "Eintrag gändert";
TBot_List_handler( $hash, "list", $tbot, $peer, $ret ); TBot_List_handler( $hash, "list", $tbot, $peer, $ret );
$ret = undef; $ret = undef;
} else { } else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: cmd :$cmd: ".(defined($arg)?"arg :$arg:":""); $ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
}
#####################
} elsif ( $cmd =~ /^list_expadd$/ ) {
# means unsolicited message ask for adding - first ask
$arg = TBot_List_changeMultiLine( $arg );
my $textmsg = "Liste ".$lname."\nSoll der Eintrag ".$arg." hinzugefügt werden?";
if ( defined($msgId ) ) {
# store text for adding
TBot_List_setMsgId( $hash, $tbot, $chatId, $arg, "expadd" );
fhem( "set ".$tbot." queryEditInline $msgId ".'@'.$chatId." (Ja:".$name."\%"."list_expaddyes) (Nein:".$name."\%"."list_edit) $textmsg" );
} else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
}
#####################
} elsif ( $cmd eq "list_expaddyes" ) {
# means add entry to list
my $addentry = TBot_List_getMsgId( $hash, $tbot, $chatId, "expadd" );
if ( defined($addentry ) ) {
fhem( "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" );
} else {
$ret = "TBot_List_handler: $name - $tbot ERROR no msgId known for peer :$peer: chat :$chatId: cmd :$cmd: ".(defined($arg)?"arg :$arg:":"");
}
} }
} }
@ -1066,6 +1165,7 @@ sub TBot_List_Setup($) {
my %sets = ( my %sets = (
"start" => undef, "start" => undef,
"end" => undef, "end" => undef,
"reset" => undef,
); );
@ -1086,8 +1186,6 @@ sub TBot_List_Setup($) {
$hash->{STATE} = "Defined"; $hash->{STATE} = "Defined";
TBot_List_calcListNo($hash);
Log3 $name, 4, "TBot_List_Setup $name: ended "; Log3 $name, 4, "TBot_List_Setup $name: ended ";
} }
@ -1124,6 +1222,8 @@ sub TBot_List_Setup($) {
Defines a TBot_List device, which will allow interaction between the telegrambot and the postme device Defines a TBot_List device, which will allow interaction between the telegrambot and the postme device
<br><br> <br><br>
Example: <code>define testtbotlist TBot_List testposteme testlist</code><br> Example: <code>define testtbotlist TBot_List testposteme testlist</code><br>
<br><br>
Note: The module relies on events send from the corresponding TelegramBot devices. Specifically changes to the readings <code>sentMsgId</code> and <code>msgReplyMsgId</code> are required to enable to find the corresponding message ids to be able to modify messages. This needs to be taken into account when using the attributes event-on-*-reading on the TelegramBot device.<br>
<br> <br>
</ul> </ul>
<br><br> <br><br>
@ -1175,6 +1275,10 @@ sub TBot_List_Setup($) {
<li><code>allowedPeers &lt;list of peer ids&gt;</code><br>If specifed further restricts the users for the given list to these peers. It can be specifed in the same form as in the telegramBot msg command but without the leading @ (so ids will be just numbers). <li><code>allowedPeers &lt;list of peer ids&gt;</code><br>If specifed further restricts the users for the given list to these peers. It can be specifed in the same form as in the telegramBot msg command but without the leading @ (so ids will be just numbers).
</li> </li>
<li><code>handleUnsolicited &lt;1 or 0&gt;</code><br>If set to 1 and new messages are sent in a chat where a dialog of this list is active the bot will ask if an entry should be added. This helps for accidential messages without out first pressing the "add" button.
</li>
</ul> </ul>
<br><br> <br><br>