Text An AI

Published

December 24, 2023

My last AI post was back on October 14. It seems like a lifetime ago. Back then I was dipping my toe into the vast ocean created by the release of ChatGPT. At one point I had even canceled my subscription due to lack of use. Now I’m scuba diving daily, and this is one gem I found on the sea floor.

Sorry, I’m exaggerating. I only use ChatGPT or OpenAI on the desktop since apps aren’t really my thing. Recently while thinking of multi-channel engagement of AI powered chat bots, I wondered if I could text an LLM, and have it answer back. Easier said then done. The new texting law to cut down on spam - 10DLC or something - killed hobby use of SMS. I got booted off Openphone despite only using it for receiving SMS codes, and an attempt to create a new Twilio received a hard no. Increasingly the world of SMS is just for businesses.

There is a glimmer of hope, though, in the least unexpected of places. Here comes email to the rescue.

You can text people through email with their phone number and carrier. For example, 000-000-0000@txt.att.net for people using AT&T. The reverse is also true. You can send a text to an email address where it will appear as an email.

My first step was to figure out how to send and parse received email programmatically. (As a quick note you need to create an app password to do both with code.) Years ago I used an library called etext for going from email to text, but it doesn’t handle the reverse. I briefly thought about using the Gmail API, before remembering how much I loathe interacting with most of Google’s APIs.

A lot of googling reminded me that really only programmatic outbound mail seems to matter from a business perspective. Finally, consulting the classic book “Automate The Boring Things With Python”finally led me in the right direction, and an actual cursory understanding of how email works.

Long story short, the email protocol has two components: smtplib for sending, and imaplib for receiving. Both of which are standard Python libraries. Writing code for sending was a breeze. Doing the same for parsing received email was not. Eventually I got here, and text to my email:

“!oai what is the capital of france?”

“!oai” is the trigger phrase that tells the script to send the content to the OpenAI API instead of ignoring it. Then I ran the script and received the reply:

“The capital of France is Paris”

That was enough to satisfy my curiosity. For a full implementation I would deploy this on Modal polling every minute:

https://modal.com/docs/guide/cron#basic-scheduling

But as I said, I really only use AI on my computer. Here is the full code:

https://github.com/gfleetwood/automation/blob/root/python/email_sms_ai.py