Build an iMessage AI assistant with Python
A complete, practical guide to sending, receiving, and replying to iMessages with Python. Learn how to stream inbound messages, send images and files, and wire in OpenAI to ship a helpful assistant fast.
Build an iMessage AI assistant with Python
Many ideas work better inside iMessage: you get replies, not bounces. In this tutorial you will send your first message, listen for inbound texts, and wire an AI reply using the SDK.
Prerequisites
- macOS Sonoma or Sequoia
- Python 3.11+
- Messages enabled and signed into your iCloud account
- Full Disk Access and Automation permissions for your terminal or IDE
Send your first message
from imessage_sdk import IMessageSendersender = IMessageSender()sender.send_text(phone_number="+15551234567", text="Hello from Python!")
This uses AppleScript under the hood so you do not need any private API. SIP stays on.
Stream inbound messages
Use the client to listen for new events and route them to your logic.
from imessage_sdk import IMessageClientfrom imessage_sdk.models import IMessagedef on_message(msg: IMessage):if msg.is_from_me or not msg.content:returnprint(f"{msg.sender_phone or msg.chat_guid}: {msg.content}")client = IMessageClient()client.start_listening(callback=on_message)
Add an AI reply
from imessage_sdk import IMessageClient, IMessageSenderfrom imessage_sdk.models import IMessagefrom openai import OpenAIllm = OpenAI()send = IMessageSender()def on_message(msg: IMessage):if msg.is_from_me or not msg.content:returnreply = llm.responses.create(model="gpt-5",input=f"Reply helpfully to: {msg.content}").output_textif msg.chat_guid:send.send_text(chat_guid=msg.chat_guid, text=reply)elif msg.sender_phone:send.send_text(phone_number=msg.sender_phone, text=reply)client = IMessageClient()client.start_listening(callback=on_message)
Send images and files
from imessage_sdk import IMessageSendersender = IMessageSender()sender.send_file(phone_number="+15551234567",file_path="./samples/product.jpg",caption="New drop. Thoughts?",)
Deploy on cloud Macs
You can run this locally or on Mac virtual machines such as AWS EC2 Mac and MacStadium. Sign into iCloud, enable Messages, grant permissions once, then run your service. SIP stays on.
Next steps
- Add a simple router to handle multiple flows
- Use group chat GUIDs to work inside teams
- Store transcripts in your database for analytics
When you are ready to ship, get the SDK and start building today.