如何用Python解析Bash返回的JSON并打包发送至Twilio实现账户余额短信通知?
Got it, let's break this down step by step to get those non-Chase account balance alerts sent via Twilio. Here's how you can pull it off:
First, confirm you can get mintapi to spit out your account data as JSON. You can either export it to a file or pipe it directly to Python. For example:
- To save to a file:
mintapi --export accounts.json(you’ll need to handle login prompts, maybe use headless mode if running via cron) - To output to stdout directly:
mintapi --export -
You have two options here: either read the pre-generated JSON file, or call the mintapi Bash command directly from Python and capture its output. Let’s cover both:
Option A: Read from a JSON file
If you already ran the mintapi command to create accounts.json, use this code to parse it:
import json # Load the JSON data with open("accounts.json", "r") as f: accounts = json.load(f) # Filter and extract the data you care about (adjust filters as needed) alert_accounts = [] for account in accounts: # Skip Chase checking if you already get alerts for it if "Chase" in account["name"] and "Checking" in account["name"]: continue # Grab name and formatted balance alert_accounts.append(f"{account['name']}: ${account['balance']:.2f}") # Format the message body message_body = "Your non-Chase account balances:\n" + "\n".join(alert_accounts)
Option B: Call mintapi directly from Python
If you want to automate the entire flow (fetch + parse + send) in one script, use subprocess to run the mintapi command and capture its output:
import json import subprocess # Run mintapi command and capture JSON output result = subprocess.run( ["mintapi", "--export", "-"], capture_output=True, text=True, check=True ) accounts = json.loads(result.stdout) # Same filtering as above alert_accounts = [] for account in accounts: if "Chase" in account["name"] and "Checking" in account["name"]: continue alert_accounts.append(f"{account['name']}: ${account['balance']:.2f}") message_body = "Your non-Chase account balances:\n" + "\n".join(alert_accounts)
First, install the Twilio Python library:
pip install twilio
Then, grab your Twilio credentials (Account SID, Auth Token) from the Twilio console, plus your Twilio phone number and the number you want to send alerts to. Never hardcode these credentials—use environment variables or a .env file for safety.
Here's the code to send the message:
from twilio.rest import Client import os from dotenv import load_dotenv # Install with pip install python-dotenv # Load environment variables (create a .env file with your credentials) load_dotenv() twilio_account_sid = os.getenv("TWILIO_ACCOUNT_SID") twilio_auth_token = os.getenv("TWILIO_AUTH_TOKEN") twilio_phone_number = os.getenv("TWILIO_PHONE_NUMBER") your_phone_number = os.getenv("YOUR_PHONE_NUMBER") # Initialize Twilio client client = Client(twilio_account_sid, twilio_auth_token) # Send the message message = client.messages.create( body=message_body, from_=twilio_phone_number, to=your_phone_number ) print(f"Alert sent! Message SID: {message.sid}")
Your .env file should look like this:
TWILIO_ACCOUNT_SID=your_account_sid_here TWILIO_AUTH_TOKEN=your_auth_token_here TWILIO_PHONE_NUMBER=+1234567890 YOUR_PHONE_NUMBER=+0987654321
To get daily alerts like your Chase notifications, set up a cron job (Linux/macOS) or Windows Task Scheduler to run your Python script at the same time every day. For example, a cron entry to run at 8 AM daily:
0 8 * * * /usr/bin/python3 /path/to/your/alert_script.py
Quick Notes:
- Make sure mintapi is set up to log in without manual intervention (use
--headlessflag, store credentials securely, or use session cookies). - Test the script manually first to ensure it parses accounts correctly and sends SMS without errors.
- Adjust the account filtering logic to match your specific accounts (e.g., include/exclude savings, credit cards, etc.).
内容的提问来源于stack exchange,提问作者mttvlk

