make a super basic arg and time parse method in Utilities class
This commit is contained in:
parent
bad7a0f67c
commit
f1f9f70e40
|
@ -14,6 +14,9 @@
|
|||
use TelegramClientManager\Objects\TelegramClient;
|
||||
use TelegramClientManager\Utilities\Hashing;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Utilities
|
||||
{
|
||||
/**
|
||||
|
@ -32,60 +35,47 @@
|
|||
if (
|
||||
strlen(trim($message->getText(true))) == 0 &&
|
||||
$message->getReplyToMessage() == null
|
||||
)
|
||||
{
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if($message->getText(true) !== null && strlen($message->getText(true)) > 0)
|
||||
{
|
||||
if ($message->getText(true) !== null && strlen($message->getText(true)) > 0) {
|
||||
$Username = trim(str_ireplace('@', (string)null, $message->getText(true)));
|
||||
|
||||
if(strlen($Username) > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (strlen($Username) > 0) {
|
||||
try {
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byUsername, $Username
|
||||
);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
} catch (Exception $e) {
|
||||
unset($e);
|
||||
}
|
||||
}
|
||||
|
||||
if(stripos(strtoupper(trim($message->getText(true))), 'TEL-') !== false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (stripos(strtoupper(trim($message->getText(true))), 'TEL-') !== false) {
|
||||
try {
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byPublicId, trim($message->getText(true))
|
||||
);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
} catch (Exception $e) {
|
||||
unset($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($clients->ReplyToUserClient !== null)
|
||||
{
|
||||
if ($clients->ReplyToUserClient !== null) {
|
||||
if ($clients->ReplyToSenderChatClient !== null)
|
||||
return $clients->ReplyToSenderChatClient;
|
||||
|
||||
return $clients->ReplyToUserClient;
|
||||
}
|
||||
|
||||
if($clients->MentionUserClients !== null)
|
||||
{
|
||||
if ($clients->MentionUserClients !== null) {
|
||||
if (count($clients->MentionUserClients) > 0)
|
||||
return $clients->MentionUserClients[array_keys($clients->MentionUserClients)[0]];
|
||||
}
|
||||
|
||||
if($reply_only == false)
|
||||
{
|
||||
if ($reply_only == false) {
|
||||
if ($clients->SenderChatClient !== null)
|
||||
return $clients->SenderChatClient;
|
||||
|
||||
|
@ -94,53 +84,81 @@
|
|||
}
|
||||
|
||||
// Final attempt with pop
|
||||
if($message->getText(true) !== null && strlen($message->getText(true)) > 0)
|
||||
{
|
||||
if ($message->getText(true) !== null && strlen($message->getText(true)) > 0) {
|
||||
// NOTE: Argument parsing is done with pop now.
|
||||
$options = pop::parse($message->getText(true));
|
||||
|
||||
$TargetTelegramParameter = array_values($options)[(count($options) - 1)];
|
||||
if(is_bool($TargetTelegramParameter))
|
||||
{
|
||||
if (is_bool($TargetTelegramParameter)) {
|
||||
$TargetTelegramParameter = array_keys($options)[(count($options) - 1)];
|
||||
}
|
||||
|
||||
$EstimatedPrivateID = Hashing::telegramClientPublicID((int)$TargetTelegramParameter, (int)$TargetTelegramParameter);
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byPublicId, $EstimatedPrivateID
|
||||
);
|
||||
}
|
||||
catch(TelegramClientNotFoundException $e)
|
||||
{
|
||||
} catch (TelegramClientNotFoundException $e) {
|
||||
unset($e);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byPublicId, $TargetTelegramParameter
|
||||
);
|
||||
}
|
||||
catch(TelegramClientNotFoundException $e)
|
||||
{
|
||||
} catch (TelegramClientNotFoundException $e) {
|
||||
unset($e);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient(
|
||||
TelegramClientSearchMethod::byUsername, str_ireplace('@', (string)null, $TargetTelegramParameter)
|
||||
);
|
||||
}
|
||||
catch(TelegramClientNotFoundException $e)
|
||||
{
|
||||
} catch (TelegramClientNotFoundException $e) {
|
||||
unset($e);
|
||||
}
|
||||
}
|
||||
|
||||
throw new SynicalBot\Exceptions\CannotFindTargetUserException('Cannot resolve user ' . $message->getText(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text other than the mention and command entity itself
|
||||
* @param Message $message
|
||||
* @return string|null
|
||||
*/
|
||||
public static function parseArg(Message $message): ?string
|
||||
{
|
||||
$reply = $message->getReplyToMessage();
|
||||
$text = $message->getText();
|
||||
if ($reply == null) {
|
||||
return substr(strstr($text, " "), " ", 2);
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract time from the message object (can be used for getting time for tban, tmute, yada yada
|
||||
* format: "5d"
|
||||
* @param Message $message
|
||||
* @return int|null (time in seconds)
|
||||
*/
|
||||
public static function parseTime(Message $message): ?int
|
||||
{
|
||||
$toParse = self::parseArg($message);
|
||||
if ($toParse != null) {
|
||||
$timeStr = $toParse[0];
|
||||
$unitStr = $toParse[1];
|
||||
switch ($unitStr) {
|
||||
case "h":
|
||||
return intval($timeStr, 10) * 3600;
|
||||
case "m":
|
||||
return intval($timeStr, 10) * 60;
|
||||
case "s":
|
||||
return intval($timeStr, 10);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue