Skip to content

Commit

Permalink
example: add custom-server-express-typescript/ (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin authored Apr 15, 2020
1 parent 4404437 commit ea26d89
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/custom-server-express-typescript/.env
Original file line number Diff line number Diff line change
@@ -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=
38 changes: 38 additions & 0 deletions examples/custom-server-express-typescript/bottender.config.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
},
};
1 change: 1 addition & 0 deletions examples/custom-server-express-typescript/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist').default;
18 changes: 18 additions & 0 deletions examples/custom-server-express-typescript/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
18 changes: 18 additions & 0 deletions examples/custom-server-express-typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
LineContext,
MessengerContext,
SlackContext,
TelegramContext,
ViberContext,
} from 'bottender';

export default async function App(
context:
| MessengerContext
| LineContext
| SlackContext
| TelegramContext
| ViberContext
): Promise<void> {
await context.sendText('Hello World');
}
36 changes: 36 additions & 0 deletions examples/custom-server-express-typescript/src/server.ts
Original file line number Diff line number Diff line change
@@ -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}`);
});
});
19 changes: 19 additions & 0 deletions examples/custom-server-express-typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}

0 comments on commit ea26d89

Please sign in to comment.