fix: update bot

This commit is contained in:
2026-05-14 10:13:56 +07:00
parent 023f1f5929
commit ec0befdb5e
3 changed files with 41 additions and 14 deletions

46
main.py
View File

@@ -3,19 +3,40 @@ import time
import random
import requests
import schedule
import logging
from datetime import datetime
from dotenv import load_dotenv
from ai_generator import generate_bot_data
load_dotenv()
# Setup Logging
log_filename = "bot_activity.log"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[
logging.FileHandler(log_filename),
logging.StreamHandler()
]
)
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:3001/api")
BOT_SECRET = os.getenv("BOT_API_SECRET", "")
BOT_MODE = os.getenv("BOT_MODE", "PROD").upper() # TEST or PROD
# Ambil list tipe transaksi dari ENV (misal: "SERVICE,JOB")
ALLOWED_TYPES = os.getenv("TRANSACTION_TYPES", "SERVICE,JOB").split(",")
# Ambil jumlah transaksi per tipe
COUNT_PER_TYPE = int(os.getenv("COUNT_PER_TYPE", "1"))
def smart_sleep(seconds):
"""Sleep only if BOT_MODE is PROD"""
if BOT_MODE == "PROD":
log(f"Sleeping for {seconds}s...")
time.sleep(seconds)
else:
log("Skipping delay (TEST mode)")
headers = {
"Content-Type": "application/json",
@@ -23,7 +44,7 @@ headers = {
}
def log(msg):
print(f"[BOT] {time.strftime('%Y-%m-%d %H:%M:%S')} - {msg}")
logging.info(msg)
def run_service_simulation():
"""Menjalankan 1 siklus transaksi Layanan/Service"""
@@ -47,9 +68,11 @@ def run_service_simulation():
res_data1 = req1.json()
provider_id = res_data1['data']['user1']['providerProfile']['id']
buyer_id = res_data1['data']['user2']['id']
log(f"Accounts created! ProviderID: {provider_id}, BuyerID: {buyer_id}")
user1 = res_data1['data']['user1']
user2 = res_data1['data']['user2']
log(f"Accounts created! Provider: {user1['name']} ({user1['email']}), Buyer: {user2['name']} ({user2['email']})")
time.sleep(random.randint(60, 180))
smart_sleep(random.randint(60, 180))
# 3. Create Service
log("Step 2: Create Service")
@@ -63,9 +86,9 @@ def run_service_simulation():
res_data2 = req2.json()
service_id = res_data2['data']['id']
log(f"Service created! ServiceID: {service_id}")
log(f"Service created! '{res_data2['data']['name']}' - Price: Rp {res_data2['data']['price']}")
time.sleep(random.randint(120, 300))
smart_sleep(random.randint(120, 300))
# 4. Simulate Transaction
log("Step 3: Place Order (Service)")
@@ -105,9 +128,11 @@ def run_job_simulation():
res_data1 = req1.json()
provider_id = res_data1['data']['user1']['providerProfile']['id']
buyer_id = res_data1['data']['user2']['id']
log(f"Accounts created! ProviderID: {provider_id}, BuyerID: {buyer_id}")
user1 = res_data1['data']['user1']
user2 = res_data1['data']['user2']
log(f"Accounts created! Provider: {user1['name']} ({user1['email']}), Buyer: {user2['name']} ({user2['email']})")
time.sleep(random.randint(120, 300))
smart_sleep(random.randint(120, 300))
# 3. Simulate Job Transaction (Post Job -> Apply -> Accept -> Transact)
log("Step 2: Simulate Job Lifecycle")
@@ -122,7 +147,9 @@ def run_job_simulation():
return False
res_data2 = req2.json()
log(f"Job Transaction Completed! JobID: {res_data2['data']['job']['id']}, OrderID: {res_data2['data']['transaction']['orderId']}")
job_title = res_data2['data']['job']['title']
order_id = res_data2['data']['transaction']['orderId']
log(f"Job Transaction Completed! Job: '{job_title}', OrderID: {order_id}")
return True
def run_job():
@@ -157,8 +184,7 @@ def run_job():
# Jeda antar simulasi dalam batch (15-45 menit)
if i < len(tasks) - 1:
batch_delay = random.randint(900, 2700)
log(f"Jeda antar simulasi batch: {batch_delay} detik...")
time.sleep(batch_delay)
smart_sleep(batch_delay)
log(f"=== Batch Selesai: {success_count}/{len(tasks)} Berhasil ===")