Chat with AI over SMS using 46elks
15th of November 2024, 10:30-12:00
Getting started
- Create a
46elks account
using this link.
Important if you want to get credits and a free number.
- Get a virtual number.
You’ll receive a phone number on your account for free to use during the workshop. View your numbers here.
Can't see a number? Ask an elk and we'll help!
- Set up a server for your code:
- Bring your own server (e.g., via
ngrok
)
- Or use our live server by texting your first name to +46766865445
What we're doing today
- Connect to your server
- Send your first SMS with Python
- Set up a Flask app to receive SMS and send back a reply
- Send your SMS through OpenAI
Send Your First SMS with Python
- Log in to your server:
Use the command below to access our live server. Replace username
with the username you received in your SMS, you'll find the password there as well.
$ ssh username@workshop.46elks.com
- Create a Python file:
You can use any terminal based editor, but nano
is often seen as a beginner friendly option.
$ nano send_sms.py
- Add the following code to send an SMS to yourself from your new number:
import requests
response = requests.post(
'https://api.46elks.com/a1/SMS',
auth=('YOUR_API_USERNAME', 'YOUR_API_PASSWORD'),
data={
'from': 'YOUR_46ELKS_NUMBER',
'to': 'YOUR_MOBILE_NUMBER',
'message': 'Hello from 46elks workshop!'
}
)
print(response.text)
- Replace
YOUR_API_USERNAME
and YOUR_API_PASSWORD
with your 46elks API credentials. You can find them here
- Replace
YOUR_46ELKS_NUMBER
with your 46elks virtual number, and YOUR_MOBILE_NUMBER
with your physical phone number.
- The phone numbers must be in E.164 format (e.g., +46766861004). Using leading zeroes, spaces or dashes will not work.
- Save and exit!
In nano
, this is done by pressing CTRL + X
, followed by y
to confirm.
- Run your code:
$ python3 send_sms.py
- If you received a friendly SMS to your mobile phone, great job! 🎉
Didn't get anything? Let us know and we'll help you out!
Receive SMS to Your Server
- In your server, set up a Python virtual environment:
$ mkdir smsai_app && cd smsai_app
$ python3 -m venv venv
$ . venv/bin/activate
$ pip install Flask requests openai
- Create an
app.py
file:
$ nano app.py
- Set up the /sms route in
app.py
to handle incoming SMS:
Replace YOUR_PORT
with the port you received in your SMS.
from flask import Flask, request
app = Flask(__name__)
@app.route('/sms', methods=['POST'])
def sms():
message = request.form.get('message')
sender = request.form.get('from')
print(f"Received SMS from {sender}: {message}")
return "Hello Pycon", 200
if __name__ == '__main__':
app.run(host="0.0.0.0", port=YOUR_PORT)
- Run your app:
$ python3 app.py
You now have a Flask app up and running, handling incoming SMS sent to /sms on your server!
- Configure your virtual number to point to your server’s URL:
You can find the page below by going to your numbers in the dashboard, and clicking edit on your number.
Replace YOUR_PORT
with your port. Remember to add /sms
to the end of your server URL and save your changes!
Incoming SMS to your virtual number will now be sent to your app!
- Send an SMS to your virtual phone number. If you receive your message back as a response, everything works! 🎉
Not getting any responses? Let us know and we'll take a look!
Send Your SMS to OpenAI
- In
app.py
, update the sms() function to forward the SMS to OpenAI and get a response.
Feel free to use our OpenAI project key included in the code for this workshop!
Docs: OpenAI Python Library
from flask import Flask, request
from openai import OpenAI
client = OpenAI(api_key="REMOVED_FOR_SECURITY_REASONS - ADD YOUR OWN KEY HERE")
app = Flask(__name__)
@app.route('/sms', methods=['POST'])
def sms():
message = request.form.get('message')
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant. Answer with max 160 characters and use only characters in the GSM 03.38 charset"},
{
"role": "user",
"content": message
}
]
)
response = completion.choices[0].message
print(response)
return (str(response.content), 200)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=YOUR_PORT)
- Remember to use a good system prompt! This is the most important step for getting your AI responses to behave the way you want. In our example, we make sure the AI only sends one SMS to save on costs.
- Ask a question to OpenAI by sending an SMS to your virtual number. If you receive a response back, you're done! 🎉
Let us know if you encounter any issues!
Congratulations!
- You now have an AI chatbot right in the SMS app on your phone! This is a very barebones implementation, but one that has sparked our imaginations. Maybe you could be the one to turn this idea into something even greater?
What to do next
Here are some suggestions on what to do next, feel free to pick one (or more) of these or come up with your own ideas!
- Add memory to your AI by storing the conversation history
- Add a frontend where you can send SMS messages using your virtual number
Docs: Send an SMS
- Add a frontend where you can see the conversation history
Docs: SMS History
- Add a voice option so you can chat with your AI over voice
Docs: Make a phone call
- Send an SMS to your virtual number and get an AI generated MMS image response
Docs: Send an MMS