feat: configurable call timeout & username normalization

This commit is contained in:
Joan
2026-01-26 18:07:22 +01:00
parent 442c08888d
commit b062b15797
6 changed files with 8772 additions and 1021 deletions

View File

@@ -19,6 +19,13 @@ if ($method === 'POST' && $uri === '/api/queue') {
exit;
}
// Normalize Username (Add @ if missing and not numeric/phone)
$username = trim($data['username']);
if (!empty($username) && !is_numeric($username) && $username[0] !== '+' && $username[0] !== '@') {
$username = '@' . $username;
}
$data['username'] = $username;
$jobId = uniqid('job_');
$data['id'] = $jobId;
$data['status'] = 'queued';
@@ -31,7 +38,7 @@ if ($method === 'POST' && $uri === '/api/queue') {
// Push to history list (for display)
$redis->lpush('job_history', $jobId);
echo json_encode(['job_id' => $jobId, 'status' => 'queued']);
echo json_encode(['job_id' => $jobId, 'status' => 'queued', 'normalized_username' => $username]);
exit;
}
@@ -107,6 +114,12 @@ if ($method === 'GET' && $uri === '/sendmessage') {
exit;
}
// Normalize Username (Add @ if missing and not numeric/phone)
$user = trim($user);
if (!empty($user) && !is_numeric($user) && $user[0] !== '+' && $user[0] !== '@') {
$user = '@' . $user;
}
$audio = 0;
if ($audioParam && strtolower($audioParam) === 'yes') {
$audio = 1;
@@ -144,15 +157,17 @@ if ($method === 'POST' && $uri === '/api/logout') {
// API: Save Config
if ($method === 'POST' && $uri === '/api/config') {
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['api_id'], $data['api_hash'])) {
if (!isset($data['api_id'], $data['api_hash'], $data['phone'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing api_id or api_hash']);
echo json_encode(['error' => 'Missing api_id, api_hash, or phone']);
exit;
}
file_put_contents('/app/data/config.json', json_encode([
'api_id' => trim($data['api_id']),
'api_hash' => trim($data['api_hash'])
'api_hash' => trim($data['api_hash']),
'phone' => trim($data['phone']),
'call_timeout' => isset($data['call_timeout']) ? (int)$data['call_timeout'] : 15
]));
// Force worker restart to pick up new config
@@ -193,20 +208,27 @@ if ($method === 'POST' && $uri === '/login') {
// We will use a dedicated session API wrapper or just basic calls if possible.
// For simplicity, we try to Instantiate MP and check state.
// Load Config
if (!file_exists('/app/data/config.json')) {
echo json_encode(['status' => 'error', 'message' => 'Config missing']);
exit;
}
$config = json_decode(file_get_contents('/app/data/config.json'), true);
$settings = new \danog\MadelineProto\Settings();
$settings->setAppInfo((new \danog\MadelineProto\Settings\AppInfo())
->setApiId((int)getenv('API_ID'))
->setApiHash(getenv('API_HASH'))
->setApiId((int)$config['api_id'])
->setApiHash($config['api_hash'])
);
$MadelineProto = new API('/app/data/session.madeline', $settings);
$phone = $_POST['phone'] ?? null;
$phone = $_POST['phone'] ?? $config['phone'] ?? null;
$code = $_POST['code'] ?? null;
$password = $_POST['password'] ?? null;
try {
if ($phone) {
if ($phone && !$code && !$password) {
// Create lock file to pause worker
touch('/app/data/login.lock');
@@ -227,11 +249,10 @@ if ($method === 'POST' && $uri === '/login') {
echo json_encode(['status' => 'error', 'message' => 'Invalid Request']);
}
} catch (\Throwable $e) {
// Don't remove lock on error immediately, user might retry code?
// Actually if phoneLogin failed, we should probably remove it?
// For now, let's keep it simple. If it's a fatal error or user gives up, they might need to restart.
// Or we can provide a 'reset' button.
// Let's just catch and return error.
// Remove lock on error so worker can resume or user can retry
if (file_exists('/app/data/login.lock')) {
unlink('/app/data/login.lock');
}
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
exit;