diff --git a/examples/custom-server-express-typescript/.env b/examples/custom-server-express-typescript/.env new file mode 100644 index 000000000..6f0706fd0 --- /dev/null +++ b/examples/custom-server-express-typescript/.env @@ -0,0 +1,15 @@ +MESSENGER_PAGE_ID= +MESSENGER_ACCESS_TOKEN= +MESSENGER_APP_ID= +MESSENGER_APP_SECRET= +MESSENGER_VERIFY_TOKEN= + +LINE_ACCESS_TOKEN= +LINE_CHANNEL_SECRET= + +TELEGRAM_ACCESS_TOKEN= + +SLACK_ACCESS_TOKEN= +SLACK_VERIFICATION_TOKEN= + +VIBER_ACCESS_TOKEN= diff --git a/examples/custom-server-express-typescript/bottender.config.js b/examples/custom-server-express-typescript/bottender.config.js new file mode 100644 index 000000000..a093b31a5 --- /dev/null +++ b/examples/custom-server-express-typescript/bottender.config.js @@ -0,0 +1,38 @@ +module.exports = { + channels: { + messenger: { + enabled: true, + path: '/webhooks/messenger', + pageId: process.env.MESSENGER_PAGE_ID, + accessToken: process.env.MESSENGER_ACCESS_TOKEN, + appId: process.env.MESSENGER_APP_ID, + appSecret: process.env.MESSENGER_APP_SECRET, + verifyToken: process.env.MESSENGER_VERIFY_TOKEN, + }, + line: { + enabled: true, + path: '/webhooks/line', + accessToken: process.env.LINE_ACCESS_TOKEN, + channelSecret: process.env.LINE_CHANNEL_SECRET, + }, + telegram: { + enabled: true, + path: '/webhooks/telegram', + accessToken: process.env.TELEGRAM_ACCESS_TOKEN, + }, + slack: { + enabled: true, + path: '/webhooks/slack', + accessToken: process.env.SLACK_ACCESS_TOKEN, + verificationToken: process.env.SLACK_VERIFICATION_TOKEN, + }, + viber: { + enabled: true, + path: '/webhooks/viber', + accessToken: process.env.VIBER_ACCESS_TOKEN, + sender: { + name: 'xxxx', + }, + }, + }, +}; diff --git a/examples/custom-server-express-typescript/index.js b/examples/custom-server-express-typescript/index.js new file mode 100644 index 000000000..aaa66797e --- /dev/null +++ b/examples/custom-server-express-typescript/index.js @@ -0,0 +1 @@ +module.exports = require('./dist').default; diff --git a/examples/custom-server-express-typescript/package.json b/examples/custom-server-express-typescript/package.json new file mode 100644 index 000000000..524b4e990 --- /dev/null +++ b/examples/custom-server-express-typescript/package.json @@ -0,0 +1,18 @@ +{ + "scripts": { + "build": "tsc", + "dev": "nodemon --exec ts-node src/server.ts", + "start": "cross-env NODE_ENV=production node dist/server.js" + }, + "dependencies": { + "body-parser": "^1.19.0", + "bottender": "latest", + "cross-env": "^6.0.3", + "express": "^4.17.1" + }, + "devDependencies": { + "nodemon": "^2.0.1", + "ts-node": "^8.8.2", + "typescript": "^3.8.3" + } +} diff --git a/examples/custom-server-express-typescript/src/index.ts b/examples/custom-server-express-typescript/src/index.ts new file mode 100644 index 000000000..ea0d97be7 --- /dev/null +++ b/examples/custom-server-express-typescript/src/index.ts @@ -0,0 +1,18 @@ +import { + LineContext, + MessengerContext, + SlackContext, + TelegramContext, + ViberContext, +} from 'bottender'; + +export default async function App( + context: + | MessengerContext + | LineContext + | SlackContext + | TelegramContext + | ViberContext +): Promise { + await context.sendText('Hello World'); +} diff --git a/examples/custom-server-express-typescript/src/server.ts b/examples/custom-server-express-typescript/src/server.ts new file mode 100644 index 000000000..6050c8ca7 --- /dev/null +++ b/examples/custom-server-express-typescript/src/server.ts @@ -0,0 +1,36 @@ +import bodyParser from 'body-parser'; +import express from 'express'; +import { bottender } from 'bottender'; + +const app = bottender({ + dev: process.env.NODE_ENV !== 'production', +}); + +const port = Number(process.env.PORT) || 5000; + +const handle = app.getRequestHandler(); + +app.prepare().then(() => { + const server = express(); + + server.use( + bodyParser.json({ + verify: (req, _, buf) => { + (req as any).rawBody = buf.toString(); + }, + }) + ); + + server.get('/api', (req, res) => { + res.json({ ok: true }); + }); + + server.all('*', (req, res) => { + return handle(req, res); + }); + + server.listen(port, err => { + if (err) throw err; + console.log(`> Ready on http://localhost:${port}`); + }); +}); diff --git a/examples/custom-server-express-typescript/tsconfig.json b/examples/custom-server-express-typescript/tsconfig.json new file mode 100644 index 000000000..cc30c3e77 --- /dev/null +++ b/examples/custom-server-express-typescript/tsconfig.json @@ -0,0 +1,19 @@ +{ + "include": ["src/**/*"], + "exclude": ["**/__tests__", "**/*.{spec,test}.ts"], + "compilerOptions": { + "target": "es2016", + "lib": ["es2017", "es2018", "es2019", "esnext.asynciterable"], + "module": "commonjs", + "skipLibCheck": true, + "moduleResolution": "node", + "esModuleInterop": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "resolveJsonModule": true, + "isolatedModules": true, + "rootDir": "./src", + "outDir": "./dist", + "types": ["node"] + } +}