-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastify-server.ts
54 lines (47 loc) · 1.07 KB
/
fastify-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Fastify, {
FastifyInstance,
FastifyRequest,
FastifyReply,
} from "fastify";
import { twiml } from "twilio";
import { i18nPlugin } from "@twilio-labs/twiml-i18n";
import en from "./locale/en.json";
import de from "./locale/de.json";
const server: FastifyInstance = Fastify({
logger: false,
});
const port: number = +(process.env.PORT || "") || 3000;
server.register(require("@fastify/formbody"));
server.register(i18nPlugin, {
fallbackLng: "en",
resources: {
en,
de,
},
});
//@ts-ignore
server.post(
"/webhook",
async (
request: FastifyRequest<{
Body: {
From?: string;
};
}>,
reply: FastifyReply,
) => {
const twimlResponse = new twiml.MessagingResponse();
twimlResponse.message(request.t("hello"));
twimlResponse.message(
request.t("placeholder", { number: request.body.From }),
);
reply.type("text/xml").send(twimlResponse.toString());
},
);
server.listen({ port }, function (err) {
if (err) {
server.log.error(err);
process.exit(1);
}
console.info(`Server started on ${port}`);
});