-
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-sms-autoresponder to Typescript (#22)
* update express-sms-autoresponder to typescript * add github action workflow
- Loading branch information
1 parent
6c2286c
commit f3ae014
Showing
10 changed files
with
3,396 additions
and
61 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-sms-autoresponder | ||
|
||
on: | ||
push: | ||
branches: [master] | ||
paths: | ||
- "express-sms-autoresponder/**" | ||
pull_request: | ||
branches: [master] | ||
paths: | ||
- "express-sms-autoresponder/**" | ||
|
||
jobs: | ||
type-check: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: express-sms-autoresponder | ||
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,55 @@ | ||
import "dotenv/config"; | ||
import Telnyx from "telnyx"; | ||
import express from "express"; | ||
|
||
const telnyx = new Telnyx(String(process.env.TELNYX_API_KEY || "")); | ||
const app = express(); | ||
const port = 5000; | ||
|
||
app.use(express.json()); | ||
|
||
const preparedReplies = new Map([ | ||
["pizza", "Chicago pizza is the best"], | ||
["ice cream", "I prefer gelato"], | ||
]); | ||
const defaultReply = | ||
"Please send either the word 'pizza' or 'ice cream' for a different response"; | ||
|
||
const processWebhook = (webhookBody: Telnyx.events.InboundMessageEvent) => { | ||
const eventType = webhookBody.data?.event_type; | ||
const payload = webhookBody.data?.payload; | ||
const direction = payload?.direction; | ||
|
||
if (eventType === "message.received" && direction === "inbound" && payload) { | ||
const smsMessage = payload.text?.replace(/\s+/g, " ").trim().toLowerCase(); | ||
const replyToTN = payload.from?.phone_number!; | ||
const telnyxSMSNumber = payload.to?.at(0)?.phone_number; | ||
const preparedReply = preparedReplies.get(smsMessage!) || defaultReply; | ||
|
||
return telnyx.messages.create({ | ||
from: telnyxSMSNumber, | ||
to: replyToTN, | ||
text: preparedReply, | ||
use_profile_webhooks: false, | ||
auto_detect: false, | ||
}); | ||
} | ||
}; | ||
|
||
app.post("/webhooks", async (req, res) => { | ||
try { | ||
console.log(req.body); | ||
const result = await processWebhook(req.body); | ||
console.log(result); | ||
} catch (e) { | ||
console.log(e); | ||
} finally { | ||
res.status(200).end(); | ||
} | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log( | ||
`Telnyx SMS autoresponder quickstart app is listening at http://localhost:${port}` | ||
); | ||
}); |
Oops, something went wrong.