First working version.
This commit is contained in:
140
salvium_tipbot_monitor.php
Normal file → Executable file
140
salvium_tipbot_monitor.php
Normal file → Executable file
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
// salvium_tipbot.php
|
||||
// salvium_tipbot_monitor.php
|
||||
use Salvium\SalviumTipBotDB;
|
||||
use Salvium\SalviumWallet;
|
||||
|
||||
$config = require __DIR__ . '/config.php';
|
||||
require_once __DIR__ . '/src/salvium_tipbot_db.php';
|
||||
require_once __DIR__ . '/src/salvium_tipbot_wallet.php';
|
||||
require_once __DIR__ . '/src/salvium_tipbot_common.php';
|
||||
|
||||
$db = new SalviumTipBotDB($config);
|
||||
$wallet = new SalviumWallet(
|
||||
@@ -16,97 +16,55 @@ $wallet = new SalviumWallet(
|
||||
$config['SALVIUM_RPC_PASSWORD']
|
||||
);
|
||||
|
||||
function sendMessage(int $chatId, string $text, array $options = []): void {
|
||||
global $config;
|
||||
$payload = array_merge(['chat_id' => $chatId, 'text' => $text], $options);
|
||||
file_get_contents("https://api.telegram.org/bot{$config['TELEGRAM_BOT_TOKEN']}/sendMessage?" . http_build_query($payload));
|
||||
// Handle incoming transfers
|
||||
$incoming = $wallet->getTransfers('in');
|
||||
if ($incoming) {
|
||||
foreach ($incoming as $tx) {
|
||||
if ($db->isTxidLogged($tx['txid'])) continue;
|
||||
|
||||
$subaddress = $tx['address'];
|
||||
$user = $db->getUserBySubaddress($subaddress);
|
||||
if (!$user) continue;
|
||||
|
||||
$amount = $tx['amount'] / 1e8; // Convert atomic to major units (1 SAL = 1e8 atomic)
|
||||
$db->logDeposit($user['id'], $tx['txid'], $amount, $tx['height']);
|
||||
$db->updateUserTipBalance($user['id'], $amount, 'add');
|
||||
|
||||
sendMessage($user['telegram_user_id'], "Deposit received: {$amount} SAL added to your balance.");
|
||||
}
|
||||
}
|
||||
|
||||
$update = json_decode(file_get_contents("php://input"), true);
|
||||
if (!$update || !isset($update['message'])) exit;
|
||||
// Handle pending withdrawals
|
||||
$withdrawals = $db->getPendingWithdrawals();
|
||||
foreach ($withdrawals as $withdrawal) {
|
||||
$result = $wallet->transfer([
|
||||
[
|
||||
'address' => $withdrawal['address'],
|
||||
'amount' => (int)($withdrawal['amount'] * 1e8) // major to atomic
|
||||
]
|
||||
]);
|
||||
|
||||
$message = $update['message'];
|
||||
$chatId = $message['chat']['id'];
|
||||
$userId = $message['from']['id'];
|
||||
$username = $message['from']['username'] ?? '';
|
||||
$text = trim($message['text'] ?? '');
|
||||
$args = explode(' ', $text);
|
||||
$command = strtolower($args[0] ?? '');
|
||||
|
||||
switch ($command) {
|
||||
case '/start':
|
||||
sendMessage($chatId, "Welcome to the Salvium Tip Bot! Use /deposit to get started.");
|
||||
break;
|
||||
|
||||
case '/deposit':
|
||||
$user = $db->getUserByTelegramId($userId);
|
||||
if (!$user) {
|
||||
$subaddress = $wallet->getNewSubaddress();
|
||||
if (!$subaddress) {
|
||||
sendMessage($chatId, "Error generating subaddress. Try again later.");
|
||||
exit;
|
||||
}
|
||||
$db->createUser($userId, $subaddress);
|
||||
$user = ['salvium_subaddress' => $subaddress];
|
||||
}
|
||||
sendMessage($chatId, "Your Salvium deposit address is: {$user['salvium_subaddress']}");
|
||||
break;
|
||||
|
||||
case '/balance':
|
||||
$user = $db->getUserByTelegramId($userId);
|
||||
if (!$user) {
|
||||
sendMessage($chatId, "You don't have an account yet. Use /deposit to create one.");
|
||||
break;
|
||||
}
|
||||
sendMessage($chatId, "Your balance: {$user['tip_balance']} XSL.");
|
||||
break;
|
||||
|
||||
case '/withdraw':
|
||||
if (count($args) < 3) {
|
||||
sendMessage($chatId, "Usage: /withdraw <address> <amount>");
|
||||
break;
|
||||
}
|
||||
list(, $address, $amount) = $args;
|
||||
$amount = (float)$amount;
|
||||
$user = $db->getUserByTelegramId($userId);
|
||||
if (!$user || $user['tip_balance'] < $amount) {
|
||||
sendMessage($chatId, "Insufficient balance or invalid account.");
|
||||
break;
|
||||
}
|
||||
if (!preg_match('/^4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}$/', $address)) {
|
||||
sendMessage($chatId, "Invalid address format.");
|
||||
break;
|
||||
}
|
||||
$db->updateUserTipBalance($user['id'], $amount, 'subtract');
|
||||
$db->logWithdrawal($user['id'], $address, $amount);
|
||||
sendMessage($chatId, "Withdrawal request submitted. Processing soon.");
|
||||
break;
|
||||
|
||||
default:
|
||||
if (str_starts_with($command, '/tip')) {
|
||||
if (count($args) < 3) {
|
||||
sendMessage($chatId, "Usage: /tip <username> <amount>");
|
||||
break;
|
||||
}
|
||||
list(, $targetUsername, $amount) = $args;
|
||||
$amount = (float)$amount;
|
||||
$sender = $db->getUserByTelegramId($userId);
|
||||
$recipient = $db->getUserByTelegramId(ltrim($targetUsername, '@'));
|
||||
if (!$sender || $sender['tip_balance'] < $amount) {
|
||||
sendMessage($chatId, "Insufficient funds or invalid sender.");
|
||||
break;
|
||||
}
|
||||
if (!$recipient) {
|
||||
sendMessage($chatId, "Recipient not found. Ask them to run /start first.");
|
||||
break;
|
||||
}
|
||||
$db->updateUserTipBalance($sender['id'], $amount, 'subtract');
|
||||
$db->addTip($sender['id'], $recipient['id'], $amount, $chatId);
|
||||
sendMessage($chatId, "Tipped {$targetUsername} {$amount} XSL successfully!");
|
||||
sendMessage($recipient['telegram_user_id'], "You received a tip of {$amount} XSL! Use /balance to check.");
|
||||
} else {
|
||||
sendMessage($chatId, "Unknown command.");
|
||||
}
|
||||
break;
|
||||
if ($result && isset($result['tx_hash'])) {
|
||||
$db->updateWithdrawalTxid($withdrawal['id'], $result['tx_hash']);
|
||||
$db->updateWithdrawalStatus($withdrawal['id'], 'sent');
|
||||
sendMessage($withdrawal['user_id'], "Withdrawal of {$withdrawal['amount']} SAL sent. TxID: {$result['tx_hash']}");
|
||||
} else {
|
||||
$db->updateWithdrawalStatus($withdrawal['id'], 'failed');
|
||||
sendMessage($withdrawal['user_id'], "Withdrawal failed. Please try again later or contact support.");
|
||||
}
|
||||
}
|
||||
|
||||
// Handle pending tips
|
||||
$users = [];
|
||||
$tips = $db->getAllPendingTips();
|
||||
foreach ($tips as $tip) {
|
||||
$recipientId = $tip['recipient_user_id'];
|
||||
if (!isset($users[$recipientId])) {
|
||||
$users[$recipientId] = $db->getUserByTelegramId($recipientId);
|
||||
}
|
||||
$db->updateUserTipBalance($recipientId, $tip['amount'], 'add');
|
||||
$db->markTipsAsCredited([$tip['id']]);
|
||||
sendMessage($recipientId, "You received a credited tip of {$tip['amount']} SAL. Use /balance to check.");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user