|
1 | | -const express = require('express'); |
2 | | -const logger = require('morgan'); |
3 | | -const httpsms = require('fix-esm').require('httpsms'); |
4 | | -const jwt = require('jsonwebtoken'); |
| 1 | +const express = require("express"); |
| 2 | +const logger = require("morgan"); |
| 3 | +const httpsms = require("fix-esm").require("httpsms"); |
| 4 | +const jwt = require("jsonwebtoken"); |
5 | 5 |
|
6 | 6 | const app = express(); |
7 | 7 |
|
8 | | -app.use(logger('dev')); |
| 8 | +app.use(logger("dev")); |
9 | 9 | app.use(express.json()); |
10 | 10 |
|
11 | | -app.get('/', (request, response) => { |
12 | | - response.status(200).json({ status: 'ok' }); |
| 11 | +app.get("/", (request, response) => { |
| 12 | + response.status(200).json({ status: "ok" }); |
13 | 13 | }); |
14 | 14 |
|
15 | 15 | // Webhook endpoint to receive incoming messages |
16 | | -app.post('/httpsms/webhook', (request, response) => { |
| 16 | +app.post("/httpsms/webhook", (request, response) => { |
17 | 17 | // Verify auth token |
18 | 18 | if (process.env.HTTPSMS_WEBHOOK_SIGNING_KEY) { |
19 | 19 | try { |
20 | | - const token = request.header('Authorization').replace("Bearer ", ""); |
| 20 | + const token = request.header("Authorization").replace("Bearer ", ""); |
21 | 21 | const claims = jwt.verify(token, process.env.HTTPSMS_WEBHOOK_SIGNING_KEY); |
22 | | - console.info('Authorization token verified from httpsms.com'); |
| 22 | + console.info("Authorization token verified from httpsms.com"); |
23 | 23 | console.debug(JSON.stringify(claims, null, 2)); |
24 | 24 | } catch (error) { |
25 | | - console.error('Invalid Authorization token', error); |
26 | | - return response.status(401).json({ error: 'Unauthorized' }); |
| 25 | + console.error("Invalid Authorization token", error); |
| 26 | + return response.status(401).json({ error: "Unauthorized" }); |
27 | 27 | } |
28 | 28 | } |
29 | 29 |
|
30 | 30 | const event = request.body; |
31 | | - console.info(`httpsms.com webhook event received with type [${request.header('X-Event-Type')}]`); |
| 31 | + console.info( |
| 32 | + `httpsms.com webhook event received with type [${request.header("X-Event-Type")}]`, |
| 33 | + ); |
32 | 34 | console.info(`decoded [${event.type}] with id [${event.id}`); |
33 | 35 | console.debug(JSON.stringify(event.data, null, 2)); |
34 | 36 |
|
35 | | - response.json({ status: 'success' }); |
| 37 | + response.json({ status: "success" }); |
36 | 38 | }); |
37 | 39 |
|
38 | | - |
39 | 40 | // Send a sample text message |
40 | | -app.get('/httpsms/send', async (request, response) => { |
41 | | - const httpsmsClient = new httpsms('' /* Get the API Key from https://httpsms.com/settings */); |
| 41 | +app.get("/httpsms/send", async (request, response) => { |
| 42 | + const httpsmsClient = new httpsms( |
| 43 | + "" /* Get the API Key from https://httpsms.com/settings */, |
| 44 | + ); |
42 | 45 |
|
43 | 46 | const message = await httpsmsClient.messages.postSend({ |
44 | | - content: 'This is a sample text message', |
45 | | - from: '+18005550199', // Put the correct phone number here |
46 | | - to: '+18005550100', // Put the correct phone number here |
| 47 | + content: "This is a sample text message", |
| 48 | + from: "+18005550199", // Put the correct phone number here |
| 49 | + to: "+18005550100", // Put the correct phone number here |
47 | 50 | encrypted: false, |
48 | | - }) |
| 51 | + }); |
49 | 52 |
|
50 | 53 | console.info(`message sent successfully with ID [${message.id}]`); |
51 | | - response.status(200).json({ status: 'ok' }); |
| 54 | + response.status(200).json({ status: "ok" }); |
52 | 55 | }); |
53 | 56 |
|
54 | | - |
55 | | - |
56 | 57 | module.exports = app; |
0 commit comments