twilio-opencode

A lightweight express server to handle Twilio voice and messaging webhooks for prompting Opencode.
git clone https://unitehenry.com/git/twilio-opencode.git
Log | Files | Refs | README | LICENSE

commit 867fa3f3473c56fa78294a602a5a517618bd8ca3
parent 6cdd472fa4d2928a53ebad8eaa1ea785cef85557
Author: Henry Unite <unitehenry@gmail.com>
Date:   Thu, 26 Feb 2026 07:46:04 +0000

chore: typecheck

Diffstat:
Mapp.ts | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mscripts/install.sh | 5+++--
Ascripts/serve.sh | 2++
Mscripts/typecheck.sh | 2+-
4 files changed, 124 insertions(+), 8 deletions(-)

diff --git a/app.ts b/app.ts @@ -1,9 +1,122 @@ -function logMessage(message: string) { - console.log(message); +import { createOpencodeClient } from "@opencode-ai/sdk" +import express, { Request, Response } from 'express'; + +const app = express(); + +app.use(express.urlencoded()); + +interface PromptResult { + sessionId: string; + text: string; +} + +async function prompt(message: string, sessionId: string | null): Promise<PromptResult> { + console.log(`Session ID Param: ${sessionId}`); + + const client = createOpencodeClient({ + baseUrl: 'http://127.0.0.1:36967', + }); + + const id: string = await (async (): Promise<string> => { + if (sessionId) { + return sessionId; + } + + const sessionResult = await client.session.create(); + + return sessionResult.data.id; + })(); + + console.log(`Session ID: ${id}`); + + const result = await client.session.prompt({ + path: { id }, + body: { + parts: [{type: "text", text: message }] + } + }); + + const text: string = result.data.parts + .filter(part => part.type === 'text') + .reduce((response: string, part) => response + ' ' + part.text, ''); + + return { sessionId: id, text }; +} + +function agentResponse(res: Response, message: string): void { + res.set('Content-Type', 'text/xml'); + + res.send(`<?xml version="1.0" encoding="UTF-8"?> + <Response> + <Gather input="speech" action="/" method="POST"> + <Say>${message}</Say> + </Gather> + </Response> + `); +} + +function smsResponse(res: Response, message: string): void { + res.set('Content-Type', 'text/xml'); + + res.send(`<?xml version="1.0" encoding="UTF-8"?> + <Response> + <Message> + <Body>${message}</Body> + </Message> + </Response> + `); } -function main() { - logMessage(42); +function hangup(res: Response): void { + res.set('Content-Type', 'text/xml'); + + res.send('<?xml version="1.0" encoding="UTF-8"?><Response><Hangup/></Response>'); } -main(); +app.post('/voice', async (req: Request, res: Response): Promise<void> => { + const callId: string = req.body.CallSid; + + console.log(`Call ID: ${callId}`); + + const speechResult: string = req.body.SpeechResult; + + if (speechResult) { + console.log('User message:', speechResult); + + const { sessionId, text } = await prompt(speechResult, app.get(callId) as string | null); + + app.set(callId, sessionId); + + console.log(text); + + agentResponse(res, text); + } else { + agentResponse(res, 'Please say something.'); + } +}); + +app.post('/message', async (req: Request, res: Response): Promise<void> => { + const messageId: string = req.body.MessageSid; + + console.log(`Message ID: ${messageId}`); + + const messageResult: string = req.body.Body; + + if (!messageResult) return; + + console.log('User message:', messageResult); + + const { text } = await prompt(messageResult, null); + + console.log(text); + + smsResponse(res, text); +}); + +app.get('/health', (req, res) => res.sendStatus(200)); + +const port: number = parseInt(process.env.PORT || '3000', 10); + +app.listen(port, (): void => { + console.log(`Server running on port ${port}`); +}); diff --git a/scripts/install.sh b/scripts/install.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash -npm install express -npm install @opencode-ai/sdk +npm install express \ + @types/express \ + @opencode-ai/sdk diff --git a/scripts/serve.sh b/scripts/serve.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +node --no-warnings app.ts diff --git a/scripts/typecheck.sh b/scripts/typecheck.sh @@ -1,2 +1,2 @@ #!/usr/bin/env bash -npx tsc --noEmit *.ts +npx tsc --noEmit --target ES2022 --module NodeNext *.ts