From Phone Call to Formatted Email in 80 Lines of Python — AI Voice Memo Cleanup with Telnyx Telnyx has released an 80-line Python Flask webhook that turns a phone call into a formatted email, using its unified API for voice, AI inference, and messaging. The demo answers a call, gathers a spoken memo, cleans up the transcript with AI, and sends the result as an email, all with a single API key. The project showcases a state-machine webhook handler that processes Telnyx events to orchestrate the flow. AI Voice Memo to Email — an 80-line Flask webhook that answers a phone call, gathers a spoken memo, runs it through AI Inference to clean up grammar and extract structure, and delivers a formatted email. One API key for voice, AI, and messaging. No third-party services. Voice memos are the fastest way to capture a thought — you speak, you're done. But what you get is a rambling audio blob that nobody including you wants to read later. The raw transcript is worse: no punctuation, false starts, filler words, and no structure. You still have to manually clean it up before it's useful as an email, a status update, or a meeting summary. The existing solutions split the problem across multiple services. A transcription service converts audio to text. An LLM API cleans up the text. An email service sends the result. Three vendors, three API keys, three bills, three points of failure. The AI Voice Memo to Email example does all of it on one network — Telnyx Call Control handles the phone call, Telnyx AI Inference cleans up the transcript, and Telnyx Messaging delivers the email. One API key. One Flask file. About 80 lines of Python. You call a Telnyx number. The app answers, speaks a greeting, and starts listening. You dictate your memo — a status update, a meeting summary, a bug report, whatever — and press when you're done. The app sends the transcript to AI Inference with a prompt that returns structured JSON: a subject line, a formatted body, and a list of action items. The app sends that as an email to your default address and confirms back on the call: "Memo saved and emailed. Subject: inferred subject . Goodbye " | Step | Event | Action | |---|---|---| | 1 | call.initiated incoming | Answer the call, create session | | 2 | call.answered | TTS: "Voice memo. Speak your memo after the tone. Press pound when finished." | | 3 | call.speak.ended | Start speech gather 120s timeout, terminates | | 4 | call.gather.ended | Send transcript to AI Inference → get structured JSON → send email → TTS confirmation | | 5 | call.hangup | Clean up session | The memo is also stored in memory and accessible via GET /memos — so even if email delivery isn't configured, the formatted memo is still retrievable. Everything lives in one Flask file. No database, no Redis, no Celery. Call state is tracked in an in-memory dict keyed by call control id . Memos are stored in a list. A background thread cleans up expired sessions every 5 minutes 1-hour TTL . Caller dials your Telnyx number ↓ Telnyx sends call.initiated webhook → /webhooks/voice ↓ app calls answer → creates session in active calls ccid ↓ Telnyx sends call.answered → app calls speak with greeting ↓ Telnyx sends call.speak.ended → app calls gather input type="speech", terminating digit=" " ↓ Caller dictates memo, presses ↓ Telnyx sends call.gather.ended with speech transcript ↓ app sends transcript to AI Inference → gets JSON {subject, body, action items} ↓ app sends email via Telnyx Messaging API ↓ app calls speak with confirmation: "Memo saved and emailed. Subject: X. Goodbye " ↓ Telnyx sends call.hangup → app removes session The webhook handler is a state machine driven by Telnyx events. Each event triggers the next action: python @app.route "/webhooks/voice", methods= "POST" def handle voice : Verify the Telnyx Ed25519 signature before trusting the event. try: client.webhooks.unwrap request.get data as text=True , headers=dict request.headers except Exception: return jsonify {"error": "invalid signature"} , 401 payload = request.get json event type = payload.get "data", {} .get "event type" data = payload.get "data", {} p = data.get "payload", {} ccid = p.get "call control id" if event type == "call.initiated" and p.get "direction" == "incoming": active calls ccid = {"caller": p.get "from" , "raw text": , "start": time.time } client.calls.actions.answer ccid return jsonify {"status": "answering"} , 200 elif event type == "call.answered": client.calls.actions.speak ccid, payload="Voice memo. Speak your memo after the tone. Press pound when finished.", voice="female", language code="en-US" return jsonify {"status": "greeting"} , 200 elif event type == "call.speak.ended": client.calls.actions.gather ccid, input type="speech", end silence timeout secs=5, timeout secs=120, language code="en-US", terminating digit=" " return jsonify {"status": "recording"} , 200 elif event type == "call.gather.ended": call = active calls.get ccid speech = p.get "speech", {} .get "result", "" if call and speech: call "raw text" .append speech ... AI cleanup + email + confirmation return jsonify {"status": "processed"} , 200 elif event type == "call.hangup": active calls.pop ccid, None return jsonify {"status": "ended"} , 200 The state machine has five transitions, one per event. The call.initiated handler checks direction == "incoming" to avoid processing outbound call legs. The call.speak.ended handler is what advances from greeting to gathering — Telnyx fires this event when TTS playback finishes, so you know the caller has heard the greeting before the gather starts. The gather uses end silence timeout secs=5 — if the caller stops speaking for 5 seconds, the gather ends automatically. The timeout secs=120 caps the total gather at 2 minutes. The terminating digit=" " lets the caller explicitly signal "I'm done" by pressing pound. The core of the app is a single inference call that turns rambling speech into structured JSON: python def call inference messages, max tokens=400 : resp = requests.post INFERENCE URL, headers={"Authorization": f"Bearer {TELNYX API KEY}", "Content-Type": "application/json"}, json={"model": AI MODEL, "messages": messages, "max tokens": max tokens, "temperature": 0.3}, timeout=15 resp.raise for status return resp.json "choices" 0 "message" "content" The system prompt asks for three fields — subject, body, and action items: formatted = call inference {"role": "system", "content": "Clean up this voice memo into a well-formatted email. " "Fix grammar, add structure paragraphs, bullets if needed . " "Return JSON: subject string, inferred from content , " "body string, the formatted memo , " "action items list of strings ."}, {"role": "user", "content": speech} memo = json.loads formatted Temperature is 0.3 — low enough that the same memo produces roughly the same output every time, but high enough that the AI can infer a reasonable subject line from the content. The max tokens=400 cap is sufficient for a typical voice memo. If the AI response isn't valid JSON, the except block saves the raw speech and speaks a simpler confirmation — the caller still gets their memo saved, just without the email: except Exception: memos.append {"raw": speech, "caller": call "caller" , "timestamp": time.strftime "%Y-%m-%dT%H:%M:%SZ" } client.calls.actions.speak ccid, payload="Memo saved. Goodbye ", voice="female", language code="en-US" Graceful degradation — the call is never wasted. If AI fails, the raw transcript is preserved. If email fails, the formatted memo is preserved. The caller always gets a confirmation. After the memo is formatted, the app sends it as an email through the Telnyx Messaging API: python def send email to, subject, body : try: requests.post "https://api.telnyx.com/v2/messages", headers={"Authorization": f"Bearer {TELNYX API KEY}", "Content-Type": "application/json"}, json={"from": {"email address": f"memo@{MEMO NUMBER.replace '+','' }.telnyx.com"}, "to": {"email address": to} , "subject": subject, "body": body, "type": "email"}, timeout=15 except Exception as e: app.logger.error "Email send failed: %s", e The same TELNYX API KEY that answers the call and runs the AI inference also sends the email — one key, one bill, one network. The email send is wrapped in a try/except because email delivery may require additional Telnyx setup. If it fails, the memo is still saved and retrievable via GET /memos . Every Telnyx webhook is signed with an Ed25519 key. The app verifies the signature before processing the event: try: client.webhooks.unwrap request.get data as text=True , headers=dict request.headers except Exception: return jsonify {"error": "invalid signature"} , 401 The webhooks.unwrap method from the Telnyx Python SDK handles the Ed25519 verification internally — it reads the telnyx-signature-ed25519 and telnyx-timestamp headers, reconstructs the signed payload, and verifies the signature against the public key. The raw body is verified, not the parsed JSON — because JSON parsing is not canonical, and the signature would fail. git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/ai-voice-memo-to-email-python cp .env.example .env add TELNYX API KEY, MEMO NUMBER, DEFAULT EMAIL pip install -r requirements.txt python app.py starts on http://localhost:5000 Then: ngrok http 5000 Configure your Call Control Application webhook URL to https://