A Python library for monitoring and extracting iMessage data with real-time capabilities
Project description
iMessage Monitor
A Python library for monitoring and extracting iMessage data with real-time capabilities and beautiful terminal display.
Features
- = Real-time monitoring of incoming iMessages
- =� Pretty-printed chat bubbles with proper left/right alignment
- <� Sticker and reaction support with emoji display
- =� ASCII art generation for image attachments (optional)
- =� Attachment handling including HEIC conversion
- =� Outbound messaging via AppleScript or Shortcuts
- =� Safe database access with read-only mode
Requirements
- macOS only (requires access to Messages database)
- Python 3.11+
- Full Disk Access permission for terminal/IDE
Installation
pip install imessage-monitor
or
uv add imessage-monitor
Quick Start
Real-time Monitoring
from imessage_monitor import iMessageMonitor
from imessage_monitor.display import pretty_print_bubble
def handle_message(message):
"""Handle new messages with pretty printing."""
print(pretty_print_bubble(message, show_ascii_art=True))
# Start monitoring
monitor = iMessageMonitor()
monitor.start(message_callback=handle_message)
# Keep running (in practice, you'd run this in an async loop)
input("Press Enter to stop...")
monitor.stop()
Message Retrieval
from imessage_monitor import iMessageMonitor, to_json
# Get recent messages
monitor = iMessageMonitor()
messages = monitor.get_recent_messages(limit=50)
# Convert to JSON
json_data = to_json(messages)
print(json_data)
Sending Messages
from imessage_monitor import ImessageOutbound
# Send via AppleScript
outbound = ImessageOutbound()
success = outbound.send_message_applescript("+1234567890", "Hello!")
# Send attachment
success = outbound.send_attachment_applescript("+1234567890", "/path/to/file.jpg")
Display Options
The library provides three pretty-print formats:
Chat Bubbles
from imessage_monitor.display import pretty_print_bubble
# Basic bubble (no ASCII art)
print(pretty_print_bubble(message))
# With ASCII art for images
print(pretty_print_bubble(message, show_ascii_art=True))
Reactions
from imessage_monitor.display import pretty_print_reaction
print(pretty_print_reaction(message)) # Shows d, =M, =, etc.
Stickers
from imessage_monitor.display import pretty_print_sticker
print(pretty_print_sticker(message, show_ascii_art=True))
Data Conversion
from imessage_monitor import to_json, to_toml
# Convert messages to different formats
json_str = to_json(messages)
toml_str = to_toml(messages)
# Save to files
to_json(messages, "messages.json")
to_toml(messages, "messages.toml")
Configuration
The library uses sensible defaults but can be configured:
from imessage_monitor.config import Config
# Create custom config
config = Config.default()
config.monitoring.poll_interval_seconds = 1 # Faster polling
monitor = iMessageMonitor(config_path="path/to/config.toml")
Contact Filtering
Filter messages by specific contacts with whitelist/blacklist functionality:
from imessage_monitor import iMessageMonitor
from imessage_monitor.config import ContactFilter
# Create contact filter
contact_filter = ContactFilter(
outbound_behavior="whitelist", # Only include outbound messages to these contacts
outbound_ids=["+1234567890", "friend@example.com"],
inbound_behavior="blacklist", # Exclude inbound messages from these contacts
inbound_ids=["spam@example.com", "+9876543210"]
)
# Apply filter to message retrieval
config = Config.default()
config.contacts = contact_filter
monitor = iMessageMonitor()
filtered_messages = monitor.get_recent_messages(limit=100)
Contact filtering supports:
- Chat-level filtering: Group chats can be whitelisted/blacklisted by chat ID
- Individual-level filtering: Individual contacts filtered by phone/email
- Precedence: Chat-level filtering takes precedence over individual-level
- Directional filtering: Separate rules for inbound vs outbound messages
Date Range Filtering
Filter messages by date range (applies to manual queries only, not monitoring):
from imessage_monitor.config import DateRange
from datetime import datetime
# Custom date range
date_range = DateRange(
start_date=datetime(2024, 1, 1),
end_date=datetime(2024, 12, 31)
)
# Quick helpers
date_range = DateRange.from_days_back(7) # Last 7 days
date_range = DateRange.from_hours_back(24) # Last 24 hours
config = Config.default()
config.date_range = date_range
monitor = iMessageMonitor()
recent_messages = monitor.get_recent_messages(limit=100)
Example TOML Configuration
[apple]
chat_db_path = "~/Library/Messages/chat.db"
attachments_path = "~/Library/Messages/Attachments"
permissions_check = true
[monitoring]
poll_interval_seconds = 20 # Used as backup if enable_real_time=true
max_batch_size = 1000
enable_real_time = true
[contacts]
outbound_behavior = "whitelist" # "whitelist" | "blacklist" | "none"
outbound_ids = ["+1234567890", "friend@example.com"]
inbound_behavior = "blacklist" # "whitelist" | "blacklist" | "none"
inbound_ids = ["spam@example.com", "+9876543210"]
[date_range] # Ignored when monitoring
start_date = "2024-01-01T00:00:00"
end_date = "2024-12-31T23:59:59"
[outbound]
method = "applescript"
rate_limit_per_minute = 30
Permissions
Required: Full Disk Access
- Open System Preferences � Security & Privacy � Privacy
- Select Full Disk Access
- Add your terminal application (Terminal.app, iTerm2, etc.)
- Add your IDE if running from there (VS Code, PyCharm, etc.)
Optional: Shortcuts (for outbound messaging)
For Shortcuts-based messaging, create these shortcuts in the Shortcuts app:
- "Send Message" - Not Yet Implemented
- "Send Attachment" - Not Yet Implemented
Platform Support
- macOS: Supported
- Windows/Linux: Not supported L (requires macOS Messages database)
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
pytest - Submit a pull request
Troubleshooting
"Database not found" error
- Ensure you're running on macOS
- Check Full Disk Access permissions
- Verify Messages app has been used at least once
"Permission denied" error
- Add Full Disk Access for your terminal/IDE
- Restart terminal after adding permissions
ASCII art not showing
- Ensure
ascii-magicis installed - Enable with
show_ascii_art=Trueparameter - Check image file accessibility
Examples
See example_usage.py for a complete real-time monitoring application.
Security Note
This library accesses your Messages database in read-only mode for monitoring. It does not modify or delete any messages. Outbound messaging requires explicit function calls and uses standard macOS APIs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file imessage_monitor-0.2.0.tar.gz.
File metadata
- Download URL: imessage_monitor-0.2.0.tar.gz
- Upload date:
- Size: 74.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88cf9f9061e4dff3f3dd65dbe11eef30b81050daaf2cf408c3dd7092bddbc433
|
|
| MD5 |
4ed4527e5d47ba2b7f91b6d8994a50fc
|
|
| BLAKE2b-256 |
098359004484f7d747cd860f901b5daca7d8d2cd75e2ed44444a3c42af503dec
|
Provenance
The following attestation bundles were made for imessage_monitor-0.2.0.tar.gz:
Publisher:
publish.yml on Brynhild-CHale/imessage_monitor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
imessage_monitor-0.2.0.tar.gz -
Subject digest:
88cf9f9061e4dff3f3dd65dbe11eef30b81050daaf2cf408c3dd7092bddbc433 - Sigstore transparency entry: 294995378
- Sigstore integration time:
-
Permalink:
Brynhild-CHale/imessage_monitor@00e68bdf61466649173e2cc0b81d1d81a945350b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Brynhild-CHale
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@00e68bdf61466649173e2cc0b81d1d81a945350b -
Trigger Event:
push
-
Statement type:
File details
Details for the file imessage_monitor-0.2.0-py3-none-any.whl.
File metadata
- Download URL: imessage_monitor-0.2.0-py3-none-any.whl
- Upload date:
- Size: 31.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9893d17164fb5d1ade96924b07ee6af74b5d1c762024e9534bb5557dc314b13
|
|
| MD5 |
a9337655db3d9713491d898c7be42246
|
|
| BLAKE2b-256 |
0219c63ed81041a8497103381688a0250cb805d5ad2d500167f3cc072ed2cf5c
|
Provenance
The following attestation bundles were made for imessage_monitor-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on Brynhild-CHale/imessage_monitor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
imessage_monitor-0.2.0-py3-none-any.whl -
Subject digest:
c9893d17164fb5d1ade96924b07ee6af74b5d1c762024e9534bb5557dc314b13 - Sigstore transparency entry: 294995391
- Sigstore integration time:
-
Permalink:
Brynhild-CHale/imessage_monitor@00e68bdf61466649173e2cc0b81d1d81a945350b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Brynhild-CHale
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@00e68bdf61466649173e2cc0b81d1d81a945350b -
Trigger Event:
push
-
Statement type: