2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-05-02 13:05:12 +00:00

50_SSChatBot: contrib 1.6.1

git-svn-id: https://svn.fhem.de/fhem/trunk@22007 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
nasseeder1 2020-05-22 17:42:15 +00:00
parent 491d7f82cf
commit c5ed0744af

View File

@ -35,20 +35,21 @@ package main;
use strict;
use warnings;
eval "use JSON;1;" or my $SSChatBotMM = "JSON"; # Debian: apt-get install libjson-perl
eval "use JSON;1;" or my $SSChatBotMM = "JSON"; ## no critic 'eval' # Debian: apt-get install libjson-perl
use Data::Dumper; # Perl Core module
use MIME::Base64;
use Time::HiRes;
use HttpUtils;
use Encode;
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
eval "use FHEM::Meta;1" or my $modMetaAbsent = 1;
eval "use Net::Domain qw(hostname hostfqdn hostdomain domainname);1" or my $SSChatBotNDom = "Net::Domain";
eval "use FHEM::Meta;1" or my $modMetaAbsent = 1; ## no critic 'eval'
eval "use Net::Domain qw(hostname hostfqdn hostdomain domainname);1" or my $SSChatBotNDom = "Net::Domain"; ## no critic 'eval'
# no if $] >= 5.017011, warnings => 'experimental';
# Versions History intern
our %SSChatBot_vNotesIntern = (
my %SSChatBot_vNotesIntern = (
"1.6.1" => "22.05.2020 changes according to PBP ",
"1.6.0" => "22.05.2020 replace \" H\" with \"%20H\" in attachments due to problem in HttpUtils ",
"1.5.0" => "15.03.2020 slash commands set in interactive answer field 'value' will be executed ",
"1.4.0" => "15.03.2020 rename '1_sendItem' to 'asyncSendItem' because of Aesthetics ",
@ -63,7 +64,7 @@ our %SSChatBot_vNotesIntern = (
);
# Versions History extern
our %SSChatBot_vNotesExtern = (
my %SSChatBot_vNotesExtern = (
"1.4.0" => "15.03.2020 Command '1_sendItem' renamed to 'asyncSendItem' because of Aesthetics ",
"1.3.0" => "13.03.2020 The set command 'sendItem' was renamed to '1_sendItem' to avoid changing the botToken by chance. ".
"Also attachments are allowed now in the '1_sendItem' command. ",
@ -115,7 +116,7 @@ sub SSChatBot_Initialize {
"httptimeout ".
$readingFnAttributes;
eval { FHEM::Meta::InitMod( __FILE__, $hash ) }; # für Meta.pm (https://forum.fhem.de/index.php/topic,97589.0.html)
FHEM::Meta::InitMod( __FILE__, $hash ) if(!$modMetaAbsent); # für Meta.pm (https://forum.fhem.de/index.php/topic,97589.0.html)
return;
}
@ -236,7 +237,7 @@ return;
sub SSChatBot_Attr {
my ($cmd,$name,$aName,$aVal) = @_;
my $hash = $defs{$name};
my ($do,$val,$cache);
my ($do,$val);
# $cmd can be "del" or "set"
# $name is device name
@ -262,13 +263,13 @@ sub SSChatBot_Attr {
}
if ($cmd eq "set") {
if ($aName =~ m/httptimeout/) {
unless ($aVal =~ /^\d+$/) { return "The Value for $aName is not valid. Use only figures 1-9 !";}
if ($aName =~ m/httptimeout/x) {
unless ($aVal =~ /^\d+$/x) { return "The Value for $aName is not valid. Use only figures 1-9 !";}
}
if ($aName =~ m/ownCommand([1-9][0-9]*)$/) {
my $num = $1;
return "The value of $aName must start with a slash like \"/Weather \"." unless ($aVal =~ /^\/.*$/);
return qq{The value of $aName must start with a slash like "/Weather ".} unless ($aVal =~ /^\/.*$/);
addToDevAttrList($name, "ownCommand".($num+1)); # add neue ownCommand dynamisch
}
}
@ -277,15 +278,16 @@ return;
}
################################################################
sub SSChatBot_Set {
sub SSChatBot_Set { ## no critic 'complexity'
my ($hash, @a) = @_;
return "\"set X\" needs at least an argument" if ( @a < 2 );
my $name = $a[0];
my $opt = $a[1];
my $prop = $a[2];
my $prop1 = $a[3];
my $prop2 = $a[4];
my $prop3 = $a[5];
return qq{"set X" needs at least an argument} if ( @a < 2 );
my @items = @a;
my $name = shift @a;
my $opt = shift @a;
my $prop = shift @a;
my $prop1 = shift @a;
my $prop2 = shift @a;
my $prop3 = shift @a;
my ($success,$setlist);
return if(IsDisabled($name));
@ -313,14 +315,14 @@ sub SSChatBot_Set {
if($success) {
CommandGet(undef, "$name chatUserlist"); # Chatuser Liste abrufen
return "botToken saved successfully";
return qq{botToken saved successfully};
} else {
return "Error while saving botToken - see logfile for details";
return qq{Error while saving botToken - see logfile for details};
}
} elsif ($opt eq "listSendqueue") {
my $sub = sub ($) {
my ($idx) = @_;
my $idx = shift;
my $ret;
foreach my $key (reverse sort keys %{$data{SSChatBot}{$name}{sendqueue}{entries}{$idx}}) {
$ret .= ", " if($ret);
@ -330,7 +332,7 @@ sub SSChatBot_Set {
};
if (!keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}) {
return "SendQueue is empty.";
return qq{SendQueue is empty.};
}
my $sq;
foreach my $idx (sort{$a<=>$b} keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}) {
@ -344,15 +346,17 @@ sub SSChatBot_Set {
delete $data{SSChatBot}{$name}{sendqueue}{entries};
$data{SSChatBot}{$name}{sendqueue}{index} = 0;
return "All entries of SendQueue are deleted";
} elsif($prop eq "-permError-") {
foreach my $idx (keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}) {
delete $data{SSChatBot}{$name}{sendqueue}{entries}{$idx}
if($data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{forbidSend});
}
return "All entries with state \"permanent send error\" are deleted";
return qq{All entries with state "permanent send error" are deleted};
} else {
delete $data{SSChatBot}{$name}{sendqueue}{entries}{$prop};
return "SendQueue entry with index \"$prop\" deleted";
return qq{SendQueue entry with index "$prop" deleted};
}
} elsif ($opt eq "asyncSendItem") {
@ -361,11 +365,12 @@ sub SSChatBot_Set {
# text="<https://www.synology.com>" users="user1"
# text="Check this!! <https://www.synology.com|Click here> for details!" users="user1,user2"
# text="a fun image" fileUrl="http://imgur.com/xxxxx" users="user1,user2"
return undef if(!$hash->{HELPER}{USERFETCHED});
return if(!$hash->{HELPER}{USERFETCHED});
my ($text,$users);
my ($fileUrl,$attachment) = ("","");
my $cmd = join(" ", map { $_ =~ s/\s//g; $_; } @a );
my ($a,$h) = parseParams($cmd);
my $cmd = join(" ", map { my $p = $_; $p =~ s/\s//g; $p; } @items);
my ($arr,$h) = parseParams($cmd);
if($h) {
$text = $h->{text} if(defined $h->{text});
$users = $h->{users} if(defined $h->{users});
@ -373,8 +378,8 @@ sub SSChatBot_Set {
$attachment = SSChatBot_formString($h->{attachments}, "attachement") if(defined $h->{attachments});
}
if($a) {
my @t = @{$a};
if($arr) {
my @t = @{$arr};
shift @t; shift @t;
$text = join(" ", @t) if(!$text);
}
@ -392,7 +397,7 @@ sub SSChatBot_Set {
foreach (@ua) {
next if(!$_);
my $uid = $hash->{HELPER}{USERS}{$_}{id};
return "The receptor \"$_\" seems to be unknown because its ID coulnd't be found." if(!$uid);
return qq{The receptor "$_" seems to be unknown because its ID coulnd't be found.} if(!$uid);
# Eintrag zur SendQueue hinzufügen
# Werte: (name,opmode,method,userid,text,fileUrl,channel,attachment)
@ -406,7 +411,7 @@ sub SSChatBot_Set {
if($ret) {
return $ret;
} else {
return "The SendQueue has been restarted.";
return qq{The SendQueue has been restarted.};
}
} else {
@ -417,7 +422,7 @@ return;
}
################################################################
sub SSChatBot_Get {
sub SSChatBot_Get { ## no critic 'complexity'
my ($hash, @a) = @_;
return "\"get X\" needs at least an argument" if ( @a < 2 );
my $name = shift @a;
@ -455,6 +460,7 @@ sub SSChatBot_Get {
} elsif ($opt eq "chatUserlist") {
# übergebenen CL-Hash (FHEMWEB) in Helper eintragen
SSChatBot_delclhash ($name);
SSChatBot_getclhash($hash,1);
# Eintrag zur SendQueue hinzufügen
@ -465,6 +471,7 @@ sub SSChatBot_Get {
} elsif ($opt eq "chatChannellist") {
# übergebenen CL-Hash (FHEMWEB) in Helper eintragen
SSChatBot_delclhash ($name);
SSChatBot_getclhash($hash,1);
# Eintrag zur SendQueue hinzufügen
@ -473,7 +480,7 @@ sub SSChatBot_Get {
SSChatBot_getapisites($name);
} elsif ($opt =~ /versionNotes/) {
} elsif ($opt =~ /versionNotes/x) {
my $header = "<b>Module release information</b><br>";
my $header1 = "<b>Helpful hints</b><br>";
my %hs;
@ -485,12 +492,12 @@ sub SSChatBot_Get {
$ret = "<html>";
# Hints
if(!$arg || $arg =~ /hints/ || $arg =~ /[\d]+/) {
if(!$arg || $arg =~ /hints/x || $arg =~ /[\d]+/x) {
$ret .= sprintf("<div class=\"makeTable wide\"; style=\"text-align:left\">$header1 <br>");
$ret .= "<table class=\"block wide internals\">";
$ret .= "<tbody>";
$ret .= "<tr class=\"even\">";
if($arg && $arg =~ /[\d]+/) {
if($arg && $arg =~ /[\d]+/x) {
my @hints = split(",",$arg);
foreach (@hints) {
if(AttrVal("global","language","EN") eq "DE") {
@ -526,7 +533,7 @@ sub SSChatBot_Get {
}
# Notes
if(!$arg || $arg =~ /rel/) {
if(!$arg || $arg =~ /rel/x) {
$ret .= sprintf("<div class=\"makeTable wide\"; style=\"text-align:left\">$header <br>");
$ret .= "<table class=\"block wide internals\">";
$ret .= "<tbody>";
@ -585,7 +592,7 @@ sub SSChatBot_initonboot {
my $room = AttrVal($name, "room", "Chat");
my $port = 8082;
while (grep(/^$port$/ , @FWports)) { # den ersten freien FHEMWEB-Port ab 8082 finden
while (grep {/^$port$/} @FWports) { # den ersten freien FHEMWEB-Port ab 8082 finden
$port++;
}
@ -717,7 +724,7 @@ sub SSChatBot_checkretry {
my $rc = $data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{retryCount};
my $errorcode = ReadingsVal($name, "Errorcode", 0);
if($errorcode =~ /100|101|117|120|407|409|410|800|900/) { # bei diesen Errorcodes den Queueeintrag nicht wiederholen, da dauerhafter Fehler !
if($errorcode =~ /100|101|117|120|407|409|410|800|900/x) { # bei diesen Errorcodes den Queueeintrag nicht wiederholen, da dauerhafter Fehler !
$forbidSend = SSChatBot_experror($hash,$errorcode); # Fehlertext zum Errorcode ermitteln
$data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{forbidSend} = $forbidSend;
@ -760,7 +767,6 @@ sub SSChatBot_checkretry {
return
}
sub SSChatBot_getapisites ($) {
my ($name) = @_;
my $hash = $defs{$name};
@ -972,7 +978,7 @@ sub SSChatBot_chatop {
Log3($name, 5, "$name - HTTP-Call will be done with httptimeout: $httptimeout s");
if ($opmode =~ /^chatUserlist$|^chatChannellist$/) {
if ($opmode =~ /^chatUserlist$|^chatChannellist$/x) {
$url = "$inprot://$inaddr:$inport/webapi/$chatexternalpath?api=$chatexternal&version=$chatexternalmaxver&method=$method&token=\"$token\"";
}
@ -994,7 +1000,7 @@ sub SSChatBot_chatop {
if(AttrVal($name, "showTokenInLog", "0") == 1) {
Log3($name, 4, "$name - Call-Out: $url");
} else {
$part =~ s/$token/<secret>/;
$part =~ s/$token/<secret>/x;
Log3($name, 4, "$name - Call-Out: $part");
}
@ -1015,7 +1021,7 @@ return;
#############################################################################################
# Callback from SSChatBot_chatop
#############################################################################################
sub SSChatBot_chatop_parse {
sub SSChatBot_chatop_parse { ## no critic 'complexity'
my ($param, $err, $myjson) = @_;
my $hash = $param->{hash};
my $name = $hash->{NAME};
@ -1023,7 +1029,7 @@ sub SSChatBot_chatop_parse {
my $inaddr = $hash->{INADDR};
my $inport = $hash->{INPORT};
my $opmode = $hash->{OPMODE};
my ($rectime,$data,$success,$error,$errorcode,$cherror);
my ($data,$success,$error,$errorcode,$cherror);
my $lang = AttrVal("global","language","EN");
@ -1032,7 +1038,7 @@ sub SSChatBot_chatop_parse {
Log3($name, 2, "$name - ERROR message: $err");
$errorcode = "none";
$errorcode = "800" if($err =~ /: malformed or unsupported URL$/s);
$errorcode = "800" if($err =~ /:\smalformed\sor\sunsupported\sURL$/xs);
readingsBeginUpdate ($hash);
readingsBulkUpdateIfChanged ($hash, "Error", $err);
@ -1115,7 +1121,7 @@ sub SSChatBot_chatop_parse {
# Ausgabe Popup der User-Daten (nach readingsEndUpdate positionieren sonst
# "Connection lost, trying reconnect every 5 seconds" wenn > 102400 Zeichen)
asyncOutput($hash->{HELPER}{CL}{1},"$out");
delete($hash->{HELPER}{CL});
InternalTimer(gettimeofday()+10.0, "SSChatBot_delclhash", $name, 0);
} elsif ($opmode eq "chatChannellist") {
my %channels = ();
@ -1153,7 +1159,7 @@ sub SSChatBot_chatop_parse {
# Ausgabe Popup der User-Daten (nach readingsEndUpdate positionieren sonst
# "Connection lost, trying reconnect every 5 seconds" wenn > 102400 Zeichen)
asyncOutput($hash->{HELPER}{CL}{1},"$out");
delete($hash->{HELPER}{CL});
InternalTimer(gettimeofday()+5.0, "SSChatBot_delclhash", $name, 0);
} elsif ($opmode eq "sendItem" && $hash->{OPIDX}) {
my $postid = "";
@ -1491,7 +1497,7 @@ sub SSChatBot_formString {
$txt .= $_;
}
$pat = join '|', map quotemeta, keys(%replacements);
$pat = join '|', map { quotemeta; } keys(%replacements);
$txt =~ s/($pat)/$replacements{$1}/g;
@ -1511,7 +1517,7 @@ sub SSChatBot_getclhash {
# nur übergebenen CL-Hash speichern,
# keine Hintergrundverarbeitung bzw. synthetische Erstellung CL-Hash
$hash->{HELPER}{CL}{1} = $hash->{CL};
return undef;
return;
}
if (!defined($hash->{CL})) {
@ -1549,6 +1555,18 @@ sub SSChatBot_getclhash {
return ($ret);
}
#############################################################################################
# Clienthash löschen
#############################################################################################
sub SSChatBot_delclhash {
my $name = shift;
my $hash = $defs{$name};
delete($hash->{HELPER}{CL});
return;
}
#############################################################################################
# Versionierungen des Moduls setzen
# Die Verwendung von Meta.pm und Packages wird berücksichtigt
@ -1566,7 +1584,7 @@ sub SSChatBot_setVersionInfo {
# META-Daten sind vorhanden
$modules{$type}{META}{version} = "v".$v; # Version aus META.json überschreiben, Anzeige mit {Dumper $modules{SSChatBot}{META}}
if($modules{$type}{META}{x_version}) { # {x_version} ( nur gesetzt wenn $Id: 50_SSChatBot.pm 20534 2019-11-18 17:50:17Z DS_Starter $ im Kopf komplett! vorhanden )
$modules{$type}{META}{x_version} =~ s/1.1.1/$v/g;
$modules{$type}{META}{x_version} =~ s/1\.1\.1/$v/gx;
} else {
$modules{$type}{META}{x_version} = $v;
}
@ -1574,7 +1592,7 @@ sub SSChatBot_setVersionInfo {
if(__PACKAGE__ eq "FHEM::$type" || __PACKAGE__ eq $type) {
# es wird mit Packages gearbeitet -> Perl übliche Modulversion setzen
# mit {<Modul>->VERSION()} im FHEMWEB kann Modulversion abgefragt werden
use version 0.77; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
use version 0.77; our $VERSION = FHEM::Meta::Get( $hash, 'version' ); ## no critic 'VERSION'
}
} else {
# herkömmliche Modulstruktur
@ -1588,7 +1606,7 @@ return;
# Common Gateway Interface
# parsen von outgoing Messages Chat -> FHEM
#############################################################################################
sub SSChatBot_CGI {
sub SSChatBot_CGI { ## no critic 'complexity'
my ($request) = @_;
my ($hash,$name,$link,$args);
my ($text,$timestamp,$channelid,$channelname,$userid,$username,$postid,$triggerword) = ("","","","","","","","");