-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update express-call-control to Typescript (#25)
* update express-call-control to Typescript * update instructions * update package deps
- Loading branch information
1 parent
b07714a
commit e28d961
Showing
10 changed files
with
3,432 additions
and
3,177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: CI express-call-control | ||
|
||
on: | ||
push: | ||
branches: [master] | ||
paths: | ||
- "express-call-control/**" | ||
pull_request: | ||
branches: [master] | ||
paths: | ||
- "express-call-control/**" | ||
|
||
jobs: | ||
type-check: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: express-call-control | ||
strategy: | ||
matrix: | ||
node-version: [18.x, 20.x, 22.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm ci | ||
- run: npm run type-check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
}, | ||
rules: { | ||
'new-cap': 'off', | ||
'no-console': 'off', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
20.17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nodejs 20.17.0 | ||
npm 10.8.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Telnyx Call Control Demo | ||
|
||
// Set up environmental variables from .env file | ||
import "dotenv/config"; | ||
|
||
// Library Requirements | ||
import express from "express"; | ||
import bodyParser from "body-parser"; | ||
import nunjucks from "nunjucks"; | ||
|
||
// Set up telnyx library with user API Key | ||
import Telnyx from "telnyx"; | ||
const telnyx = new Telnyx(process.env.TELNYX_API_KEY || ""); | ||
|
||
// Fire up express app and settings | ||
const app = express(); | ||
app.use(express.json()); | ||
app.use( | ||
bodyParser.urlencoded({ | ||
extended: true, | ||
}) | ||
); | ||
|
||
// Set default express engine and extension | ||
app.engine("html", nunjucks.render); | ||
app.set("view engine", "html"); | ||
|
||
// configure nunjucks engine | ||
nunjucks.configure("templates/views", { | ||
autoescape: true, | ||
express: app, | ||
}); | ||
|
||
// Simple page that can send a phone call | ||
app.get("/", function (request, response) { | ||
response.render("messageform"); | ||
}); | ||
|
||
app.post("/outbound", async (request, response) => { | ||
const to_number = request.body.to_number; | ||
|
||
try { | ||
const { data: call } = await telnyx.calls.create({ | ||
connection_id: process.env.TELNYX_CONNECTION_ID || "", | ||
to: to_number, | ||
from: process.env.TELNYX_NUMBER || "", | ||
timeout_secs: 0, | ||
time_limit_secs: 0, | ||
answering_machine_detection: "premium", | ||
media_encryption: "disabled", | ||
sip_transport_protocol: "UDP", | ||
stream_track: "inbound_track", | ||
send_silence_when_idle: false, | ||
webhook_url_method: "POST", | ||
record_channels: "single", | ||
record_format: "wav", | ||
record_max_length: 0, | ||
record_timeout_secs: 0, | ||
enable_dialogflow: false, | ||
transcription: false, | ||
}); | ||
|
||
response.render("messagesuccess"); | ||
console.log(call?.call_control_id); | ||
} catch (e) { | ||
response.send(e); | ||
} | ||
}); | ||
|
||
type CallControlEvent = | ||
| Telnyx.events.CallHangupEvent | ||
| Telnyx.events.CallInitiatedEvent | ||
| Telnyx.events.CallAnsweredEvent | ||
| Telnyx.events.CallSpeakEndedEvent; | ||
|
||
app.post("/call_control", async (request, response) => { | ||
response.sendStatus(200); | ||
|
||
const data = (request.body as CallControlEvent).data!; | ||
try { | ||
const callControlId = data.payload!.call_control_id!; | ||
|
||
if (data.event_type == "call.hangup") { | ||
console.log("Call has ended."); | ||
} else if (data.event_type == "call.initiated") { | ||
telnyx.calls.answer(callControlId, { | ||
stream_track: "inbound_track", | ||
send_silence_when_idle: false, | ||
webhook_url_method: "POST", | ||
transcription: false, | ||
}); | ||
} else if (data.event_type == "call.answered") { | ||
telnyx.calls.speak(callControlId, { | ||
payload: | ||
"Hello, Telnyx user! Welcome to this call control demonstration.", | ||
voice: "male", | ||
language: "en-US", | ||
payload_type: "text", | ||
service_level: "premium", | ||
}); | ||
} else if (data.event_type == "call.speak.ended") { | ||
console.log("Speak has ended."); | ||
telnyx.calls.hangup(callControlId, {}); | ||
} | ||
} catch (error) { | ||
console.log("Error issuing call command"); | ||
console.log(error); | ||
} | ||
}); | ||
|
||
// Fire up the app on port specified in env | ||
app.listen(process.env.TELNYX_APP_PORT); | ||
console.log(`Server listening on port ${process.env.TELNYX_APP_PORT}`); |
Oops, something went wrong.