Files
salvium-telegram-tipbot/src/salvium_tipbot_commands.php

117 lines
4.3 KiB
PHP

<?php
use Salvium\SalviumTipBotDB;
use Salvium\SalviumWallet;
class SalviumTipBotCommands {
private SalviumTipBotDB $db;
private SalviumWallet $wallet;
private array $config;
private array $commandAccess = [
'start' => ['private'],
'deposit' => ['private'],
'withdraw' => ['private'],
'balance' => ['private'],
'tip' => ['private', 'group', 'supergroup'],
];
public function __construct(SalviumTipBotDB $db, SalviumWallet $wallet, array $config) {
$this->db = $db;
$this->wallet = $wallet;
$this->config = $config;
}
public function handle(string $command, array $args, array $context): void {
$method = 'cmd_' . ltrim($command, '/');
$cmdKey = ltrim($command, '/');
$allowedChats = $this->commandAccess[$cmdKey] ?? [];
if (!in_array($context['chat_type'], $allowedChats)) {
return; // not allowed
}
if (method_exists($this, $method)) {
$response = $this->$method($args, $context);
} else {
$response = ""; // Optional fallback
}
if ($response) {
sendMessage($context['chat_id'], $response);
}
$this->db->logMessage($context['chat_id'], $context['chat_name'], $context['username'], $context['raw'], $response);
}
private function cmd_start(array $args, array $ctx): string {
if (!empty($ctx['username'])) {
$this->db->updateUsername($ctx['user_id'], $ctx['username']);
}
return "Welcome to the Salvium Tip Bot! Use /deposit to get started.";
}
private function cmd_deposit(array $args, array $ctx): string {
$user = $this->db->getUserByTelegramId($ctx['user_id']);
if (!$user) {
$sub = $this->wallet->getNewSubaddress();
if (!$sub) return "Error generating subaddress.";
$this->db->createUser($ctx['user_id'], $sub);
if (!empty($ctx['username'])) {
$this->db->updateUsername($ctx['user_id'], $ctx['username']);
}
return "Your deposit address: $sub";
}
return "Your deposit address: {$user['salvium_subaddress']}";
}
private function cmd_balance(array $args, array $ctx): string {
$user = $this->db->getUserByTelegramId($ctx['user_id']);
return $user ? "Your balance: {$user['tip_balance']} SAL" : "No account found. Use /deposit first.";
}
private function cmd_withdraw(array $args, array $ctx): string {
if (count($args) < 3) return "Usage: /withdraw <address> <amount>";
list(, $address, $amount) = $args;
$amount = (float) $amount;
if ($amount < $this->config['MIN_WITHDRAWAL_AMOUNT']) {
return "Withdrawal amount too low. Minimum is {$this->config['MIN_WITHDRAWAL_AMOUNT']} SAL.";
}
$user = $this->db->getUserByTelegramId($ctx['user_id']);
if (!$user || $user['tip_balance'] < $amount) return "Insufficient balance or invalid account.";
if (!isValidSalviumAddress($address)) return "Invalid SAL address format.";
$this->db->updateUserTipBalance($user['id'], $amount, 'subtract');
$this->db->logWithdrawal($user['id'], $address, $amount);
return "Withdrawal request submitted. Processing soon.";
}
private function cmd_tip(array $args, array $ctx): string {
if (count($args) < 3) return "Usage: /tip <username> <amount>";
list(, $targetUsername, $amount) = $args;
$amount = (float)$amount;
if ($amount < $this->config['MIN_TIP_AMOUNT']) {
return "Tip too small. Minimum tip is {$this->config['MIN_TIP_AMOUNT']} SAL.";
}
$sender = $this->db->getUserByTelegramId($ctx['user_id']);
$recipient = $this->db->getUserByUsername(ltrim($targetUsername, '@'));
if (!$sender || $sender['tip_balance'] < $amount) return "Insufficient funds or invalid sender.";
if (!$recipient) return "Recipient not found. Ask them to run /start first.";
$this->db->updateUserTipBalance($sender['id'], $amount, 'subtract');
$this->db->addTip($sender['id'], $recipient['id'], $amount, $ctx['chat_id']);
sendMessage($recipient['telegram_user_id'], "You received a tip of {$amount} SAL! Use /balance to check.");
return "Tipped {$targetUsername} {$amount} SAL successfully!";
}
}
?>