└── utils.py /utils.py: -------------------------------------------------------------------------------- 1 | import re 2 | from datetime import datetime 3 | 4 | def extract_mpesa_details(message, action): 5 | if action == 'add': 6 | transaction_id_match = re.search(r'([A-Z0-9]+) Confirmed', message) 7 | amount_match = re.search(r'Ksh([\d,]+\.\d{2})', message) 8 | date_match = re.search(r'on (\d{1,2}/\d{1,2}/\d{2}) at (\d{1,2}:\d{2} [APM]{2})', message) 9 | phone_match = re.search(r'from .+?\b(\d{10})\b', message) 10 | elif action == 'verify': 11 | transaction_id_match = re.search(r'([A-Z0-9]+) Confirmed', message) 12 | amount_match = re.search(r'Ksh([\d,]+\.\d{2})', message) 13 | date_match = re.search(r'on (\d{1,2}/\d{1,2}/\d{2}) at (\d{1,2}:\d{2} [APM]{2})', message) 14 | phone_match = re.search(r'to .+?\b(\d{10})\b', message) 15 | 16 | if transaction_id_match and amount_match and date_match and phone_match: 17 | transaction_id = transaction_id_match.group(1) 18 | amount = float(amount_match.group(1).replace(',', '')) 19 | date_str = f"{date_match.group(1)} {date_match.group(2)}" 20 | transaction_date = datetime.strptime(date_str, "%d/%m/%y %I:%M %p") 21 | phone_number = phone_match.group(1) 22 | 23 | return transaction_id, amount, transaction_date, phone_number 24 | 25 | return None, None, None, None 26 | --------------------------------------------------------------------------------