Skip to content

Commit

Permalink
update express-sms-autoresponder to Typescript (#22)
Browse files Browse the repository at this point in the history
* update express-sms-autoresponder to typescript

* add github action workflow
  • Loading branch information
lucasassisrosa authored Oct 15, 2024
1 parent 6c2286c commit f3ae014
Show file tree
Hide file tree
Showing 10 changed files with 3,396 additions and 61 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/express-sms-autoresponder.yml
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
9 changes: 9 additions & 0 deletions express-sms-autoresponder/.eslintrc.js
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',
},
};
1 change: 1 addition & 0 deletions express-sms-autoresponder/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.17.0
2 changes: 2 additions & 0 deletions express-sms-autoresponder/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodejs 20.17.0
npm 10.8.1
12 changes: 6 additions & 6 deletions express-sms-autoresponder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Sample application demonstrating Telnyx-Node SMS autoresponse

</div>


## Documentation & Tutorial

The full documentation and tutorial is available on [developers.telnyx.com](https://developers.telnyx.com/docs/v2/development/dev-env-setup?lang=dotnet&utm_source=referral&utm_medium=github_referral&utm_campaign=cross-site-link)
Expand All @@ -17,21 +16,22 @@ The full documentation and tutorial is available on [developers.telnyx.com](http

- Complete the steps outlined on the [Messaging > Quickstarts > Portal Setup](https://developers.telnyx.com/docs/v2/messaging/quickstarts/portal-setup#mission-control-portal-set-up) developer page
- You will:
- Create a Telnyx account
- Purchase an SMS-capable phone number with Telnyx
- Create a messaging profile
- Assign your phone number to your messaging profile
- Create a Telnyx account
- Purchase an SMS-capable phone number with Telnyx
- Create a messaging profile
- Assign your phone number to your messaging profile

## Setup Localhost App

- Clone this repository and change directory to this project folder
- Run `npm install`
- Run `cp .env.sample .env` and record the [Telynx API Key](https://portal.telnyx.com/#/app/api-keys) in the `.env` file
- Start the server `node index.js`
- Start the server `npm run start`

## Setup Reverse Proxy to Localhost

For your localhost app to receive webhooks from Telnyx, we recommend the use of [ngrok](https://ngrok.com/):

- Sign up for a free account and follow the ngrok 'Setup & Installation' guide. When running the `ngrok` tool, be sure to change the port value from `80` to `5000` (the port our app is listening on).
- Once the `ngrok` process is running on your localhost, copy the forwarding https address (e.g., https://4f7e5039ecb9.ngrok.io)
- On your [Telnyx Messaging Profile](https://portal.telnyx.com/#/app/messaging), update the Inbound Settings to "Send a webhook to this URL" with the copied forwarding url and append the defined webhooks path (e.g., https://4f7e5039ecb9.ngrok.io/webhooks)
Expand Down
49 changes: 0 additions & 49 deletions express-sms-autoresponder/index.js

This file was deleted.

55 changes: 55 additions & 0 deletions express-sms-autoresponder/index.ts
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}`
);
});
Loading

0 comments on commit f3ae014

Please sign in to comment.