package com.isp.bkashautomation.parser import java.util.regex.Pattern data class ParsedBkashAlert( val trxId: String, val amount: Double, val referenceUsername: String, val rawText: String, val sender: String? = null, val timestamp: Long = System.currentTimeMillis() ) object RegexParser { // Regex Patterns for bKash Personal & Agent Received SMS // Example: "You have received Tk 650.00 from 01711000001. Ref tanvir.ahmed. Fee Tk 0.00. Balance Tk 15,200.00. TrxID BK9A87X410 at 25/08/2026 18:20" private val TRX_PATTERN = Pattern.compile("TrxID\\s+([A-Z0-9]+)", Pattern.CASE_INSENSITIVE) private val AMOUNT_PATTERN = Pattern.compile("(?:Tk|Tk\\.|BDT)\\s*([0-9,]+(?:\\.[0-9]{2})?)", Pattern.CASE_INSENSITIVE) private val REF_PATTERN = Pattern.compile("Ref\\s+([a-zA-Z0-9._-]+)", Pattern.CASE_INSENSITIVE) fun parse(smsBody: String, sender: String? = null): ParsedBkashAlert? { val trxMatcher = TRX_PATTERN.matcher(smsBody) val amountMatcher = AMOUNT_PATTERN.matcher(smsBody) val refMatcher = REF_PATTERN.matcher(smsBody) val trxId = if (trxMatcher.find()) trxMatcher.group(1)?.trim() else null val amountStr = if (amountMatcher.find()) amountMatcher.group(1)?.replace(",", "") else null val rawRef = if (refMatcher.find()) refMatcher.group(1)?.trim() else null // Strip trailing punctuation like '.' or ';' val reference = rawRef?.replace(Regex("[.,;:]+$"), "")?.trim() if (trxId.isNullOrEmpty() || reference.isNullOrEmpty() || amountStr.isNullOrEmpty()) { return null } val amount = amountStr.toDoubleOrNull() ?: 0.0 return ParsedBkashAlert( trxId = trxId, amount = amount, referenceUsername = reference, rawText = smsBody, sender = sender ) } }