50_SSChatBot: use setCredentials from SMUtils

git-svn-id: https://svn.fhem.de/fhem/trunk@22923 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
DS_Starter
2020-10-05 20:34:44 +00:00
parent 6c2d8d458c
commit be9e24d55b

View File

@@ -48,6 +48,7 @@ use FHEM::SynoModules::SMUtils qw(
completeAPI completeAPI
showAPIinfo showAPIinfo
evaljson evaljson
setCredentials
getCredentials getCredentials
showStoredCredentials showStoredCredentials
setReadingErrorNone setReadingErrorNone
@@ -133,6 +134,7 @@ BEGIN {
# Versions History intern # Versions History intern
my %vNotesIntern = ( my %vNotesIntern = (
"1.11.4" => "05.10.2020 use setCredentials from SMUtils ",
"1.11.3" => "04.10.2020 use showStoredCredentials from SMUtils ", "1.11.3" => "04.10.2020 use showStoredCredentials from SMUtils ",
"1.11.2" => "01.10.2020 move startFunctionDelayed, checkSendRetry to SMUtils ", "1.11.2" => "01.10.2020 move startFunctionDelayed, checkSendRetry to SMUtils ",
"1.11.1" => "28.09.2020 use evaljson from SMUtils ", "1.11.1" => "28.09.2020 use evaljson from SMUtils ",
@@ -183,6 +185,9 @@ my %vHintsExt_de = (
); );
# Standardvariablen
my $queueStartFn = "FHEM::SSChatBot::getApiSites"; # Startfunktion zur Queue-Abarbeitung
my %hset = ( # Hash für Set-Funktion my %hset = ( # Hash für Set-Funktion
botToken => { fn => \&_setbotToken }, botToken => { fn => \&_setbotToken },
listSendqueue => { fn => \&listSendqueue }, listSendqueue => { fn => \&listSendqueue },
@@ -340,7 +345,8 @@ return 0;
# Gerät zu löschen die mit dieser Gerätedefinition zu tun haben. # Gerät zu löschen die mit dieser Gerätedefinition zu tun haben.
################################################################# #################################################################
sub Delete { sub Delete {
my ($hash, $arg) = @_; my $hash = shift;
my $arg = shift;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $index = $hash->{TYPE}."_".$hash->{NAME}."_botToken"; my $index = $hash->{TYPE}."_".$hash->{NAME}."_botToken";
@@ -456,14 +462,14 @@ sub _setbotToken {
my $prop = $paref->{prop}; my $prop = $paref->{prop};
return qq{The command "$opt" needs an argument.} if (!$prop); return qq{The command "$opt" needs an argument.} if (!$prop);
my ($success) = setToken($hash, $prop, "botToken"); my ($success) = setCredentials($hash, "botToken", $prop);
if($success) { if($success) {
CommandGet(undef, "$name chatUserlist"); # Chatuser Liste abrufen CommandGet(undef, "$name chatUserlist"); # Chatuser Liste abrufen
return qq{botToken saved successfully}; return qq{Token saved successfully};
} }
else { else {
return qq{Error while saving botToken - see logfile for details}; return qq{Error while saving the Token - see logfile for details};
} }
return; return;
@@ -551,6 +557,7 @@ sub _setasyncSendItem {
channel => "", channel => "",
attachment => $attachment attachment => $attachment
}; };
addSendqueue ($params); addSendqueue ($params);
} }
@@ -577,7 +584,10 @@ sub _setrestartSendqueue {
my $ret = getApiSites($name); my $ret = getApiSites($name);
return $ret if($ret); if($ret) {
return $ret;
}
return qq{The SendQueue has been restarted.}; return qq{The SendQueue has been restarted.};
} }
@@ -723,6 +733,7 @@ sub _getchatChannellist {
channel => "", channel => "",
attachment => "" attachment => ""
}; };
addSendqueue($params); addSendqueue($params);
getApiSites ($name); getApiSites ($name);
@@ -971,7 +982,7 @@ sub getApiSites_parse {
Log3($name, 2, "$name - ERROR message: $err"); Log3($name, 2, "$name - ERROR message: $err");
setReadingErrorState ($hash, $err); setReadingErrorState ($hash, $err);
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
return; return;
} }
@@ -980,7 +991,7 @@ sub getApiSites_parse {
if (!$success) { if (!$success) {
Log3($name, 4, "$name - Data returned: ".$myjson); Log3($name, 4, "$name - Data returned: ".$myjson);
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
return; return;
} }
@@ -1000,7 +1011,7 @@ sub getApiSites_parse {
setReadingErrorState ($hash, $error, $errorcode); setReadingErrorState ($hash, $error, $errorcode);
Log3($name, 2, "$name - ERROR - $error"); Log3($name, 2, "$name - ERROR - $error");
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
return; return;
} }
@@ -1010,7 +1021,7 @@ sub getApiSites_parse {
if ($opmode eq "apiInfo") { # API Infos in Popup anzeigen if ($opmode eq "apiInfo") { # API Infos in Popup anzeigen
showAPIinfo ($hash, $hash->{HELPER}{API}); # übergibt Referenz zum instanziierten API-Hash) showAPIinfo ($hash, $hash->{HELPER}{API}); # übergibt Referenz zum instanziierten API-Hash)
checkSendRetry ($name,0,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 0, $queueStartFn);
return; return;
} }
} }
@@ -1021,7 +1032,7 @@ sub getApiSites_parse {
setReadingErrorState ($hash, $error, $errorcode); setReadingErrorState ($hash, $error, $errorcode);
Log3($name, 2, "$name - ERROR - $error"); Log3($name, 2, "$name - ERROR - $error");
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
return; return;
} }
} }
@@ -1053,7 +1064,7 @@ sub chatOp {
setReadingErrorState ($hash, $error, $errorcode); setReadingErrorState ($hash, $error, $errorcode);
Log3($name, 2, "$name - ERROR - $error"); Log3($name, 2, "$name - ERROR - $error");
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
return; return;
} }
@@ -1137,16 +1148,17 @@ sub chatOp_parse {
$errorcode = "800" if($err =~ /:\smalformed\sor\sunsupported\sURL$/xs); $errorcode = "800" if($err =~ /:\smalformed\sor\sunsupported\sURL$/xs);
setReadingErrorState ($hash, $err, $errorcode); setReadingErrorState ($hash, $err, $errorcode);
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
return; return;
}
} elsif ($myjson ne "") { elsif ($myjson ne "") {
# wenn die Abfrage erfolgreich war ($data enthält die Ergebnisdaten des HTTP Aufrufes) # wenn die Abfrage erfolgreich war ($data enthält die Ergebnisdaten des HTTP Aufrufes)
# Evaluiere ob Daten im JSON-Format empfangen wurden # Evaluiere ob Daten im JSON-Format empfangen wurden
($success) = evaljson ($hash,$myjson); ($success) = evaljson ($hash,$myjson);
unless ($success) {
if(!$success) {
Log3 ($name, 4, "$name - Data returned: ".$myjson); Log3 ($name, 4, "$name - Data returned: ".$myjson);
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
return; return;
} }
@@ -1163,7 +1175,7 @@ sub chatOp_parse {
&{$hmodep{$opmode}{fn}} ($hash, $data); &{$hmodep{$opmode}{fn}} ($hash, $data);
} }
checkSendRetry ($name,0,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 0, $queueStartFn);
readingsBeginUpdate ($hash); readingsBeginUpdate ($hash);
readingsBulkUpdateIfChanged ($hash, "Errorcode", "none" ); readingsBulkUpdateIfChanged ($hash, "Errorcode", "none" );
@@ -1184,7 +1196,7 @@ sub chatOp_parse {
setReadingErrorState ($hash, $error, $errorcode); setReadingErrorState ($hash, $error, $errorcode);
Log3($name, 2, "$name - ERROR - Operation $opmode was not successful. Errorcode: $errorcode - $error"); Log3($name, 2, "$name - ERROR - Operation $opmode was not successful. Errorcode: $errorcode - $error");
checkSendRetry ($name,1,"FHEM::SSChatBot::getApiSites"); checkSendRetry ($name, 1, $queueStartFn);
} }
undef $data; undef $data;
@@ -1321,6 +1333,7 @@ sub _parseSendItem {
my $postid = ""; my $postid = "";
my $idx = $hash->{OPIDX}; my $idx = $hash->{OPIDX};
my $uid = $data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{userid}; my $uid = $data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{userid};
if($data->{data}{succ}{user_id_post_map}{$uid}) { if($data->{data}{succ}{user_id_post_map}{$uid}) {
$postid = $data->{data}{succ}{user_id_post_map}{$uid}; $postid = $data->{data}{succ}{user_id_post_map}{$uid};
} }
@@ -1333,38 +1346,6 @@ sub _parseSendItem {
return; return;
} }
######################################################################################
# botToken speichern
######################################################################################
sub setToken {
my ($hash, $token, $ao) = @_;
my $name = $hash->{NAME};
my ($success, $credstr, $index, $retcode);
my (@key,$len,$i);
$credstr = encode_base64($token);
# Beginn Scramble-Routine
@key = qw(1 3 4 5 6 3 2 1 9);
$len = scalar @key;
$i = 0;
$credstr = join "", map { $i = ($i + 1) % $len; chr((ord($_) + $key[$i]) % 256) } split //x, $credstr; ## no critic 'Map blocks'
# End Scramble-Routine
$index = $hash->{TYPE}."_".$hash->{NAME}."_".$ao;
$retcode = setKeyValue($index, $credstr);
if ($retcode) {
Log3($name, 2, "$name - Error while saving Token - $retcode");
$success = 0;
}
else {
($success, $token) = getCredentials($hash,1,$ao); # Credentials nach Speicherung lesen und in RAM laden ($boot=1)
}
return $success;
}
############################################################################################# #############################################################################################
# FHEMWEB Extension hinzufügen # FHEMWEB Extension hinzufügen
############################################################################################# #############################################################################################
@@ -1771,6 +1752,7 @@ sub __botCGIdataInterprete {
channel => "", channel => "",
attachment => "" attachment => ""
}; };
addSendqueue ($params); addSendqueue ($params);
} }