Building a ChatGPT Bot for iMessage Using BlueBubbles
A bot you can create for your iMessage chats
A Little Background
A handful of years ago, I started an endeavor to create an open-source alternative to AirMessage, called BlueBubbles. If you aren’t familiar with the apps, they basically enable the use of iMessage on Android, Windows, or Linux, via a macOS proxy device. Now, I won’t go into all the details, but throughout the years, I’ve been adding features to BlueBubbles allowing users to build their own ecosystem of integrations.
The first step was creating a REST API to allow users to interact with iMessage easily, without having to learn AppleScript or Objective-C to access the “Private API” on macOS. The second step was creating “listeners” to trigger events that the app could emit to its clients such as new messages, group updates, and more. These were the building blocks that enabled us to create clients to interact with iMessage at a high level. Thus, BlueBubbles was born.
But I didn’t stop there. I took it a step further and added support for registering webhooks with the app, so that users could easily integrate with the events the app emits. This is the foundation for the rest of this post.
Shout out to our contributors who have helped make this all possible!
Overview
Now, I will detail how I was able to create a Chat GPT bot for iMessage, using BlueBubbles. For this, I could have written my own simple HTTP server, and written a bunch of custom code to handle the webhooks, API calls, and responses. Instead, I opted to utilize an existing project called n8n, which allowed me to build a workflow using a nice UI, webhook integration, and some pre-existing “nodes”.
My goal here was to create a bot that would respond to specific commands, within specific chats.
Setup
Pre-requisites
Here is what you’ll need to replicate this project:
- A macOS device (virtual or physical) with iMessage support
- BlueBubbles installed on the macOS device
- n8n install on the macOS device or on the same network
- An OpenAI (Chat GPT) API Key
n8n Workflow Template
Luckily for you all, I’ve created a simple “template” for n8n that you can import into your n8n Desktop instance as a guide.
The AppleScript version will be the “default” as the Private API version requires some additional setup on macOS, but enables subjects, replies, faster sending, send effects, and more.
- GitHub: n8n — BlueBubbles Chat GPT Bot (AppleScript)
- GitHub n8n — BlueBubbles Chat GPT Bot (Private API)
Once imported, it should look like this:
The basic flow is as follows:
Wait for a new message -> Check if the message is a “command” -> Query Chat GPT for a response -> Send a text back to the chat
Configuring the Workflow
You’ll see this workflow is made up of 6 nodes. I will explain what each one of them does:
- Wait for BlueBubbles Message: This node creates a webhook URL (provided by n8n), and waits for a message to be POST’d to it.
- Check Command: This node checks to make sure the message that was sent is a “command message”. In this case, starting with
!
. - Check Chat: This node checks to make sure the message was sent in a “whitelisted” chat. I don’t want the bot to be active for all my chats…
- Get Query: This node simply strips the
!
that the message started with. - OpenAI: This node sends the query to the Chat GPT API, and gets a response to the query.
- Send Text: This node sends the Chat GPT response back to the chat the message came from.
Modifying the Command
If you want to modify the command (to trigger the bot), double-click on the Check Command
node, and modify the condition.
The default just requires the message to start with an exclamation character as shown below. But you can really make it whatever you want.
For instance !gpt
. Removing the Check Command
node will send any message to the Chat GPT (assuming the other checks pass).
Note, if you change the command, you will also need to edit the Get Query
node to account for the new command.
Whitelisting Chats
The Check Chat
node handles chat whitelisting. That’s to say, I don’t want this bot to react to messages coming from all my chats, I just want it to react to messages coming from these specific chats.
By double-clicking the node, you can use the Add Condition
button to add a String
condition, checking if the following expression Equals
a value
{{ $node["Wait for BlueBubbles Message"].json.body.data.chats[0].guid }}
The value that the expression should equal looks like this for DMs, iMessage;-;+1<address>
or iMessage;+;chat<id>
for group chats.
If you do not know what the Chat GUID is, skip ahead to Configuring the Workflow with BlueBubbles to test the workflow webhook and get a sample event from the chat you want to use the bot in.
It should look similar to this:
Configuring your OpenAI (Chat GPT) API Key
If you do not have a Chat GPT API key, you can purchase a plan via the OpenAI website.
To configure your API key, double-click the OpenAI node, and click the pencil
icon next to the Credential to connect
with field. Then just paste your key into the provided field. You do not need to enter an Organization ID
. It should look like this
Configuring the Response Text
Lastly, you will need to setup the HTTP request to send a text back to the chat that the message originated from.
Double click the Send Text
node to configure it. For the URL
field, you will need to replace the IP address and port with the IP address (or localhost
if running on your macOS device). If you are not running n8n on your macOS device, and you do not know your IP address, you can follow these steps to get it:
- Open Terminal on your macOS device
- Type
ifconfig
and hit enter - Look for a section labeled
en0
(or similar) - Wihtin that section look for a line prefixed with
inet
. The value followinginet
should be your local IP address
Your IP address should look similar to 192.168.50.x
or 10.0.0.x
for certain home networks.
Next, you will need to configure your BlueBubbles server credentials. Click on the pencil
icon next to the Credential for Query Auth
field. For the Value
field, enter your BlueBubbles Server password.
Everything else should be able to be left untouched.
Configuring the Workflow with BlueBubbles
Once your n8n workflow is complete, you will need to configure it with BlueBubbles. Open the BlueBubbles Server on your macOS device
Navigate through the walkthrough if you haven’t already. You do not need to set up the Google Firebase configuration.
Getting your Webhook URL from n8n
To get your Webhook URL from n8n, double click on the Wait for BlueBubbles Message
node and copy the Test URL
it provides.
Note, for production use, copy the Production URL
Click on the API & Webhooks
tab. Then click the Manage
drop down, selecting Add Webhook
from the list of options.
Adding the Webhook to BlueBubbles
In the Add Webhook
popup, paste your n8n webhook URL into the URL
field.
Next, select the event subscriptions that you want your webhook to be triggered for. For this example, just select New Messages
Lastly, save the webhook configuration.
Testing your n8n Workflow
Now that the webhook configuration is saved, go back to n8n and click on the Execute Workflow
button.
The workflow should run, waiting for a message to be sent.
Send a message in your desired chat (it doesn’t need to be a command). If everything is configured correctly, you should see a green check show next to the Wait for BlueBubbles Message
node.
If you do not see the green checkmark, check your BlueBubbles server for any errors regarding a failed Webhook.
Now that you know your workflow receives the webhook, you can send a command message to your desired chat to make sure the rest of the workflow is operating properly.
If you have not whitelisted chats at this point, I advise looking at the output data for the Wait for BlueBubbles
node, and grabbing the Chat GUID from the data.
If everything is working in your test environment. Re-configure the BlueBubbles Webhook with the n8n Production URL
.
Results
As you can imagine, the results are exactly what you’d expect!
Conclusion
This is just one example of a bot you can create for your iMessage chats. The possibilities are seemingly endless, and I’m sure you can get other ideas from the numerous Discord bots that already exist.
I hope you all found this post informative and entertaining!
Shameless Plugs
BlueBubbles is a free and open-source ecosystem of apps aimed to bring the iMessage experience to non-Apple devices! BlueBubbles is available on Android, PC, and the Web. All the source code can be found on our GitHub below:
You can find us on the Google Play Store.
We have a bunch of documentation on our help center here:
If you would like to support the project & contributors, head over to our website.
If you would like to join our Discord community to interact with users & devs (including myself), join here:
Credits: I am the maintainer of the BlueBubbles ecosystem