Skip to main content

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 ❤️, 👍, 👎, 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

  1. Open System PreferencesSecurity & PrivacyPrivacy
  2. Select Full Disk Access
  3. Add your terminal application (Terminal.app, iTerm2, etc.)
  4. 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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests: pytest
  5. 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-magic is installed
  • Enable with show_ascii_art=True parameter
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

imessage_monitor-0.2.1.tar.gz (75.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

imessage_monitor-0.2.1-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file imessage_monitor-0.2.1.tar.gz.

File metadata

  • Download URL: imessage_monitor-0.2.1.tar.gz
  • Upload date:
  • Size: 75.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for imessage_monitor-0.2.1.tar.gz
Algorithm Hash digest
SHA256 90c37b6b284e51d45d6720f38e1ccc4b3615cdffadb3716705c0e420eee6f6c3
MD5 e82f180aebe37c730122382c8c30cdbd
BLAKE2b-256 789b44a6c1d35da09fffb275f0e76d499b7c43afa101cd601710d172ace5c3a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for imessage_monitor-0.2.1.tar.gz:

Publisher: publish.yml on Brynhild-CHale/imessage_monitor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file imessage_monitor-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for imessage_monitor-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2d4e2206c9aeadf9a2414354bb6512090430e7d29f57940b1372e883a942c62a
MD5 c2e9f2c84652a08a8c92210daddf3ece
BLAKE2b-256 d8f8c3892bc9cb55fc63b04e8563c6472cf4678c1e9d0de73dcc6f9e9f2e8cfc

See more details on using hashes here.

Provenance

The following attestation bundles were made for imessage_monitor-0.2.1-py3-none-any.whl:

Publisher: publish.yml on Brynhild-CHale/imessage_monitor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page