You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Malio/app/Utils/Telegram/Commands/SetuserCommand.php

296 lines
11 KiB
PHTML

7 years ago
<?php
namespace App\Utils\Telegram\Commands;
use App\Models\User;
7 years ago
use App\Services\Config;
use App\Utils\Telegram\{Reply, TelegramTools};
7 years ago
use Telegram\Bot\Actions;
use Telegram\Bot\Commands\Command;
/**
* Class SetuserCommand.
*/
class SetuserCommand extends Command
{
/**
* @var string Command Name
*/
protected $name = 'setuser';
/**
* @var string Command Description
*/
protected $description = '[群组/私聊] 修改用户数据,管理员命令.';
7 years ago
/**
* {@inheritdoc}
*/
public function handle($arguments)
{
$Update = $this->getUpdate();
$Message = $Update->getMessage();
// 消息 ID
$MessageID = $Message->getMessageId();
// 消息会话 ID
$ChatID = $Message->getChat()->getId();
// 触发用户
$SendUser = [
'id' => $Message->getFrom()->getId(),
'name' => $Message->getFrom()->getFirstName() . ' ' . $Message->getFrom()->getLastName(),
'username' => $Message->getFrom()->getUsername(),
];
7 years ago
if ($_ENV['enable_delete_user_cmd'] === true) {
7 years ago
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $Message->getMessageId(),
]);
}
7 years ago
if (!in_array($SendUser['id'], $_ENV['telegram_admins'])) {
7 years ago
$AdminUser = User::where('is_admin', 1)->where('telegram_id', $SendUser['id'])->first();
if ($AdminUser == null) {
// 非管理员回复消息
7 years ago
if ($_ENV['enable_not_admin_reply'] === true && $_ENV['not_admin_reply_msg'] != '') {
7 years ago
$response = $this->replyWithMessage(
[
7 years ago
'text' => $_ENV['not_admin_reply_msg'],
7 years ago
'parse_mode' => 'HTML',
'reply_to_message_id' => $MessageID,
]
);
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
]);
return;
7 years ago
}
7 years ago
}
}
// 发送 '输入中' 会话状态
$this->replyWithChatAction(['action' => Actions::TYPING]);
7 years ago
7 years ago
return self::Reply($arguments, $SendUser, $Message, $MessageID, $ChatID);
7 years ago
}
7 years ago
public function Reply($arguments, $SendUser, $Message, $MessageID, $ChatID)
7 years ago
{
7 years ago
$User = null;
$FindUser = null;
7 years ago
$arguments = trim($arguments);
7 years ago
7 years ago
if ($Message->getReplyToMessage() != null) {
// 回复源消息用户
$FindUser = [
'id' => $Message->getReplyToMessage()->getFrom()->getId(),
'name' => $Message->getReplyToMessage()->getFrom()->getFirstName() . ' ' . $Message->getReplyToMessage()->getFrom()->getLastName(),
'username' => $Message->getReplyToMessage()->getFrom()->getUsername(),
];
$User = TelegramTools::getUser($FindUser['id']);
7 years ago
if ($User == null) {
7 years ago
$response = $this->replyWithMessage(
[
7 years ago
'text' => $_ENV['no_user_found'],
7 years ago
'parse_mode' => 'HTML',
'reply_to_message_id' => $MessageID,
]
);
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
return;
7 years ago
}
7 years ago
7 years ago
if ($arguments == '') {
7 years ago
// 无参数时回复用户信息
7 years ago
$response = $this->replyWithMessage(
[
'text' => Reply::getUserInfoFromAdmin($User, $ChatID),
7 years ago
'reply_to_message_id' => $MessageID,
]
);
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
// 储存 Bot 所发的 Message ID
TelegramTools::FindUserSave([
'userid' => $User->id,
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
]);
return;
7 years ago
}
7 years ago
}
7 years ago
if ($arguments == '') {
$strArray = [
'/setuser [用户识别] [操作字段] [操作参数]',
'',
'用户识别:[使用回复消息方式则无需填写]',
'- [' . implode(' | ', array_keys(TelegramTools::getUserSearchMethods())) . '],可省略,默认:email',
'- 例 1:/setuser admin@admin 操作字段 操作参数',
'- 例 2:/setuser port:10086 操作字段 操作参数',
'',
'操作字段:',
'[' . implode(' | ', array_keys(TelegramTools::getUserActionOption())) . ']',
'',
'操作参数:',
'- 请查看对应选项支持的写法.',
];
7 years ago
$response = $this->replyWithMessage(
[
'text' => TelegramTools::StrArrayToCode($strArray),
'parse_mode' => 'HTML',
'reply_to_message_id' => $MessageID,
]
);
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
return;
7 years ago
}
7 years ago
// 命令格式:
7 years ago
// - /setuser [用户识别码] 选项 操作值
7 years ago
// ############## 命令解析 ##############
7 years ago
$UserCode = '';
7 years ago
if ($User == null) {
7 years ago
$Options = TelegramTools::StrExplode($arguments, ' ', 3);
7 years ago
if (count($Options) < 3) {
7 years ago
$response = $this->replyWithMessage(
[
'text' => '没有提供选项或操作值.',
'parse_mode' => 'HTML',
'reply_to_message_id' => $MessageID,
]
);
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
return;
7 years ago
}
// 用户识别码
$UserCode = $Options[0];
// 选项
$Option = $Options[1];
// 操作值
$value = $Options[2];
} else {
7 years ago
$Options = TelegramTools::StrExplode($arguments, ' ', 2);
7 years ago
if (count($Options) < 2) {
7 years ago
$response = $this->replyWithMessage(
[
'text' => '没有提供选项或操作值.',
'parse_mode' => 'HTML',
'reply_to_message_id' => $MessageID,
]
);
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
return;
7 years ago
}
// 选项
$Option = $Options[0];
// 操作值
$value = $Options[1];
7 years ago
}
// ############## 命令解析 ##############
// ############## 用户识别码处理 ##############
if ($User == null) {
7 years ago
// 默认搜寻字段
7 years ago
$useMethod = 'email';
if (strpos($UserCode, ':') !== false) {
7 years ago
// 如果指定了字段
7 years ago
$UserCodeExplode = explode(':', $UserCode);
$Search = $UserCodeExplode[0];
$UserCode = $UserCodeExplode[1];
7 years ago
$SearchMethods = TelegramTools::getUserSearchMethods();
$useTempMethod = TelegramTools::getOptionMethod($SearchMethods, $Search);
7 years ago
if ($useTempMethod != '') {
$useMethod = $useTempMethod;
7 years ago
}
}
$User = TelegramTools::getUser($UserCode, $useMethod);
7 years ago
if ($User == null) {
7 years ago
$response = $this->replyWithMessage(
[
7 years ago
'text' => $_ENV['no_user_found'],
7 years ago
'parse_mode' => 'HTML',
'reply_to_message_id' => $MessageID,
]
);
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
return;
7 years ago
}
}
// ############## 用户识别码处理 ##############
// ############## 字段选项处理 ##############
7 years ago
$OptionMethods = TelegramTools::getUserActionOption();
$useOptionMethod = TelegramTools::getOptionMethod($OptionMethods, $Option);
7 years ago
if ($useOptionMethod == '') {
7 years ago
$response = $this->replyWithMessage(
7 years ago
[
7 years ago
'text' => $_ENV['data_method_not_found'],
7 years ago
'parse_mode' => 'HTML',
7 years ago
'reply_to_message_id' => $MessageID,
]
);
7 years ago
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
return;
7 years ago
}
7 years ago
// ############## 字段选项处理 ##############
7 years ago
7 years ago
$reply = TelegramTools::OperationUser($User, $useOptionMethod, $value, $ChatID);
$response = $this->replyWithMessage(
7 years ago
[
7 years ago
'text' => $reply['msg'],
'parse_mode' => 'HTML',
7 years ago
'reply_to_message_id' => $MessageID,
]
);
7 years ago
// 消息删除任务
TelegramTools::DeleteMessage([
'chatid' => $ChatID,
'messageid' => $response->getMessageId(),
'executetime' => $_ENV['delete_admin_message_time']
7 years ago
]);
return;
7 years ago
}
7 years ago
}