From 2428afd3db587b11a3060e5c98c82c4c5e47d5f7 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 21 Jan 2026 14:54:17 -0800 Subject: [PATCH] Add SIP registration error handling with Twilio API - Catch all SIP error codes (not just 403) in registration failures - Query Twilio API on error to get detailed account status - Show helpful message like "Twilio account suspended - add funds" - Retry registration after 2 minutes instead of exiting - Add restart() method to Baresip for clean reconnection Requires TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN env vars for detailed error messages. Co-Authored-By: Claude Opus 4.5 --- src/phone.ts | 25 ++++++++++++++++------- src/sip.ts | 21 +++++++++++++++---- src/utils/twilio.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/utils/twilio.ts diff --git a/src/phone.ts b/src/phone.ts index 0a29c6b..9be461c 100644 --- a/src/phone.ts +++ b/src/phone.ts @@ -18,6 +18,7 @@ import GPIO from "./pins" import { Agent } from "./agent" import { searchWeb } from "./agent/tools" import { ring } from "./utils" +import { getTwilioAccountInfo, formatTwilioError } from "./utils/twilio" import { getSound, WaitingSounds } from "./utils/waiting-sounds" type CancelableTask = () => void @@ -123,14 +124,24 @@ const startBaresip = async (phoneService: PhoneService, hook: GPIO.Input, ringer phoneService.send({ type: "error", message: error.message }) }) - baresip.error.on(async ({ message }) => { - log.error("🐻 error:", message) - phoneService.send({ type: "error", message }) - for (let i = 0; i < 4; i++) { - await ring(ringer, 500) - await sleep(250) + baresip.error.on(async ({ message, statusCode, reason }) => { + let errorMessage = message + + if (statusCode) { + const twilioInfo = await getTwilioAccountInfo() + if (twilioInfo && twilioInfo.status !== "active") { + errorMessage = formatTwilioError(twilioInfo) + } else { + errorMessage = `Registration failed: ${statusCode} ${reason}` + } } - process.exit(1) + + log.error("🐻 error:", errorMessage) + // Don't send error to state machine - we're retrying, not giving up + + log("🔄 Retrying registration in 2 minutes...") + await sleep(2 * 60 * 1000) + baresip.restart() }) return baresip diff --git a/src/sip.ts b/src/sip.ts index 46c26de..4626d84 100644 --- a/src/sip.ts +++ b/src/sip.ts @@ -8,7 +8,7 @@ export class Baresip { callEstablished = new Emitter<{ contact: string }>() callReceived = new Emitter<{ contact: string }>() hungUp = new Emitter() - error = new Emitter<{ message: string }>() + error = new Emitter<{ message: string; statusCode?: string; reason?: string }>() registrationSuccess = new Emitter() constructor(baresipArgs: string[]) { @@ -52,6 +52,7 @@ export class Baresip { this.callReceived.removeAllListeners() this.hungUp.removeAllListeners() this.registrationSuccess.removeAllListeners() + this.error.removeAllListeners() } kill() { @@ -61,6 +62,14 @@ export class Baresip { this.process = undefined } + async restart() { + if (this.process) { + this.process.kill() + this.process = undefined + } + await this.connect() + } + #parseLine(line: string) { log.debug(`📞 Baresip: ${line}`) const callEstablishedMatch = line.match(/Call established: (.+)/) @@ -91,10 +100,14 @@ export class Baresip { this.registrationSuccess.emit() } - const registrationFailedMatch = line.match(/reg: sip:\S+ 403 Forbidden/) + const registrationFailedMatch = line.match(/reg: sip:\S+ .*?(\d{3}) (\w+)/) const socketInUseMatch = line.match(/tcp: sock_bind:/) - if (registrationFailedMatch || socketInUseMatch) { - log.error(`⁉️ NOT HANDLED: Registration failed with "${line}"`) + if (registrationFailedMatch) { + const [, statusCode, reason] = registrationFailedMatch + log.error(`Registration failed: ${statusCode} ${reason}`) + this.error.emit({ message: line, statusCode, reason }) + } else if (socketInUseMatch) { + log.error(`Registration failed: socket in use`) this.error.emit({ message: line }) } } diff --git a/src/utils/twilio.ts b/src/utils/twilio.ts new file mode 100644 index 0000000..ea95066 --- /dev/null +++ b/src/utils/twilio.ts @@ -0,0 +1,50 @@ +const accountSid = process.env.TWILIO_ACCOUNT_SID +const authToken = process.env.TWILIO_AUTH_TOKEN + +type AccountStatus = "active" | "suspended" | "closed" + +interface TwilioAccountInfo { + status: AccountStatus + balance?: string + currency?: string +} + +export async function getTwilioAccountInfo(): Promise { + if (!accountSid || !authToken) { + return undefined + } + + const credentials = Buffer.from(`${accountSid}:${authToken}`).toString("base64") + const headers = { Authorization: `Basic ${credentials}` } + + const [accountRes, balanceRes] = await Promise.all([ + fetch(`https://api.twilio.com/2010-04-01/Accounts/${accountSid}.json`, { headers }), + fetch(`https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Balance.json`, { headers }), + ]) + + if (!accountRes.ok) { + return undefined + } + + const account = (await accountRes.json()) as { status: AccountStatus } + const info: TwilioAccountInfo = { status: account.status } + + if (balanceRes.ok) { + const balance = (await balanceRes.json()) as { balance: string; currency: string } + info.balance = balance.balance + info.currency = balance.currency + } + + return info +} + +export function formatTwilioError(info: TwilioAccountInfo): string { + if (info.status === "suspended") { + const balanceInfo = info.balance ? ` (balance: ${info.balance} ${info.currency})` : "" + return `Twilio account suspended${balanceInfo} - add funds at twilio.com/console` + } + if (info.status === "closed") { + return "Twilio account is closed" + } + return `Twilio account status: ${info.status}` +}