Badger 2040W iMessaging

I made my Badger able to send iMessages by running a Python webhook server on a Mac (if someone else wants to try this, I think it has to be a Mac, not Windows or Linux):

import subprocess
import json
from flask import Flask, request, jsonify

def sendMessage(content: str, to: str):
    dict = {"content":content, "to":to}
    with open("/tmp/msg", "w") as file:
        json.dump(dict, file, indent=4)
    result = subprocess.run(["shortcuts", "run", "sendMessage", "-i", "/tmp/msg"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True).stdout
    if result != "":
        return "An error occurred while running the shortcut."

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def webhook():
    data = request.get_json()

    # Validate the required fields
    if not data or 'content' not in data or 'to' not in data:
        return jsonify({'error': 'Missing arguments content and/or to'}), 400

    content = data['content']
    to = data['to']

    sendMessage(content, to)

    return jsonify({'message': 'Webhook received!', 'content': content, 'to': to}), 200

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

that runs a Shortcut:


that sends the message.

I also had to code a keyboard for the badger. Each of the letter buttons has a letter shown above it on the screen. If you press the down or up buttons, then it will, respectively go to the next or the last letters in the alphabet.
Here is the Badger code (in MicroPython):

import badger2040
from time import sleep
import urequests

#Create Badger instance
badger = badger2040.Badger2040()

#Turn on network/WiFi
badger.connect()

#Make Badger update faster
badger.set_update_speed(badger2040.UPDATE_TURBO)

def clear():
    #Set background to white
    badger.set_pen(15)
    badger.clear()

    #Set text to black
    badger.set_pen(0)

#Draw letters for keyboard
def keyboardPage(page):
    if len(keyboard[page*3]) > 1: badger.text(keyboard[page*3], 21, 100)
    else: badger.text(keyboard[page*3], 41, 100)
    if len(keyboard[page*3+1]) > 1: badger.text(keyboard[page*3+1], 127, 100)
    else: badger.text(keyboard[page*3+1], 147, 100)
    if len(keyboard[page*3+2]) > 1: badger.text(keyboard[page*3+2], 233, 100)
    else: badger.text(keyboard[page*3+2], 253, 100)
    badger.update()
    
def keypress(key):
    global typed
    if key == "SEND":
        contact = selectContact()
        sendMessage(typed, contact)
        print("\nSending \""+typed+"\"!")
        typed =""
    elif key == "DELETE":
        typed = typed[:-1]
        print("\r                              \r"+typed,end="")
    else:
        if key == "SPACE":
            key = " "
        typed = typed + key
        print(key, end = "")
    update()
        
def update():
    clear()
    badger.text(typed, 20, 50)
    keyboardPage(currentPage)
    
def selectContact():
    clear()
    badger.update()
    badger.text("REPLACE THIS WITH PERSON ONE'S NAME", 21, 100)
    badger.text("REPLACE THIS WITH PERSON TWO'S NAME", 127, 100)
    badger.text("REPLACE THIS WITH PERSON THREE'S NAME", 233, 100)
    badger.text("REPLACE THIS WITH PERSON FOUR'S NAME", 243, 25)
    badger.update()
    badger.update()
    contact = ""
    while contact == "":
        if badger.pressed(badger2040.BUTTON_A): contact = "REPLACE THIS WITH PERSON ONE'S EMAIL ADDRESS"
        if badger.pressed(badger2040.BUTTON_B): contact = "REPLACE THIS WITH PERSON TWO'S EMAIL ADDRESS"
        if badger.pressed(badger2040.BUTTON_C): contact = "REPLACE THIS WITH PERSON THREE'S EMAIL ADDRESS"
        if badger.pressed(badger2040.BUTTON_UP): contact = "REPLACE THIS WITH PERSON FOUR'S EMAIL ADDRESS"
    return contact
        
def sendMessage(content, to):
    payload = {
        'content': content,
        'to': to
    }
    response = urequests.post("http://hemingway.local:5000/webhook", json=payload)
    response.close()

#Set font to bitmap8
badger.set_font("bitmap8")

currentPage = 0
keyboard = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ".", "SPACE", "DELETE", "SEND"]
    
clear()
keyboardPage(currentPage)
typed = ""
while True:
    badger.keepalive()
    
    if badger.pressed(badger2040.BUTTON_UP) and currentPage != 0:
        currentPage -= 1
        update()
    if badger.pressed(badger2040.BUTTON_DOWN) and currentPage != 9:
        currentPage += 1
        update()
    if badger.pressed(badger2040.BUTTON_A):
        keypress(keyboard[currentPage*3])
        sleep(0.2) #Prevent duplicate button presses
    if badger.pressed(badger2040.BUTTON_B):
        keypress(keyboard[currentPage*3+1])
        sleep(0.2) #Prevent duplicate button presses
    if badger.pressed(badger2040.BUTTON_C):
        keypress(keyboard[currentPage*3+2])
        sleep(0.2) #Prevent duplicate button presses
        
    badger.halt()

You could also use Pushcut instead of my macOS Python and Shortcuts setup, but you need to have the paid version to use the “Server” features.