Getting started

  1. Create a 46elks account using this link.
    Important if you want to get credits and a free number.
  2. 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!
  3. 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
  4. SMS account

What we're doing today

Send Your First SMS with Python

  1. 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
  2. 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
  3. 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.
  4. Save and exit!
    In nano, this is done by pressing CTRL + X, followed by y to confirm.
  5. Run your code:
    $ python3 send_sms.py
  6. 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

  1. 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
  2. Create an app.py file:
    $ nano app.py
  3. 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)
            
  4. Run your app:
    $ python3 app.py
    You now have a Flask app up and running, handling incoming SMS sent to /sms on your server!
  5. 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!
    Callback URLs Incoming SMS to your virtual number will now be sent to your app!
  6. 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

  1. 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.
  2. 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!

What to do next