Running Bottender with Custom Servers
The Concept
bottender dev
and bottender start
are the default npm scripts generated by create-bottender-app.
Thus, You can find those lines in your generated package.json
:
{
...
"scripts": {
"dev": "bottender dev",
"start": "bottender start",
...
},
...
}
When executing one of bottender dev
or bottender start
command, under the hood, it sets up a default express server for Bottender and load index.js
from the project root directory as the root action of the chatbot.
To run Bottender with the custom HTTP server, we need to prepare an HTTP server, delegate the webhook routes to Bottender, and modify the setting of package.json
.
Express
Creating A New Project with Custom Express Server
If you want to have a clean project with the custom express, you could start from this example to develop your project. There are four steps you could follow to create your project:
- Download the code from this example.
- Run
yarn
ornpm install
command to install all dependencies. - Fill the
.env
file with correct value. - Run
yarn dev
ornpm run dev
to start the dev server.
If you want to have the folder structure we recommend, you could start with create-bottender-app command and migrate it to the custom express server by following the migration instructions below.
Migrating An Existing Project to Custom Express Server
Suppose that you already have a project built from create-bottender-app, and you want to develop some additional APIs using express server. In this case, you need to write a custom express server and delegate all chatbot's webhook requests to the Bottender app.
To achieve that, you could follow the instructions, as shown below:
- Run
yarn add express body-parser nodemon
ornpm install express body-parser nodemon
command to install dependencies we need. - Create a
server.js
file in the root directory of the project and copy the following code into it.
const bodyParser = require('body-parser');
const express = require('express');
const { bottender } = require('bottender');
const app = bottender({
dev: process.env.NODE_ENV !== 'production',
});
const port = Number(process.env.PORT) || 5000;
// the request handler of the bottender app
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.use(
bodyParser.json({
verify: (req, _, buf) => {
req.rawBody = buf.toString();
},
})
);
// your custom route
server.get('/api', (req, res) => {
res.json({ ok: true });
});
// route for webhook request
server.all('*', (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
- Modify scripts in the
package.json
tonodemon server.js
andnode server.js
instead:
{
...
"scripts": {
"dev": "nodemon server.js",
"start": "node server.js",
...
},
...
}
That's all you need to do, and you can have you bot hosting on the custom express server!
Koa
Creating A New Project with Custom Koa Server
If you want to have a clean project with the custom koa, you could start from this example to develop your project. There are four steps you could follow to create your project:
- Download the code from this example.
- Run
yarn install
command to install dependencies. - Fill the
.env
file with correct value. - Run
yarn dev
to start server.
If you want to have the folder structure we recommend, you could start with create-bottender-app command and migrate it to the custom koa server by following the migration instructions below.
Migrating An Existing Project to Custom Koa Server
Suppose that you already have a project built from create-bottender-app, and you want to develop some additional APIs using koa server. In this case, you need to write a custom koa server and delegate all chatbot's webhook requests to the Bottender app.
To achieve that, you could follow the instructions, as shown below:
- Run
yarn add koa koa-router koa-bodyparser nodemon
ornpm install koa koa-router koa-bodyparser nodemon
command to install dependencies we need. - Create a
server.js
file in the root directory of the project and copy the following code into it.
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const { bottender } = require('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 = new Koa();
const router = new Router();
router.get('/api', (ctx) => {
ctx.response.body = { ok: true };
});
router.all('*', async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
});
server.use(bodyParser());
server.use((ctx, next) => {
ctx.req.body = ctx.request.body;
ctx.req.rawBody = ctx.request.rawBody;
return next();
});
server.use(router.routes());
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
- Modify scripts in the
package.json
tonodemon server.js
andnode server.js
instead:
{
...
"scripts": {
"dev": "nodemon server.js",
"start": "node server.js",
...
},
...
}
That's all you need to do, and you can have you bot hosting on the custom koa server!
Restify
Creating A New Project with Custom Restify Server
If you want to have a clean project with the custom restify, you could start from this example to develop your project. There are four steps you could follow to create your project:
- Download the code from this example.
- Run
yarn install
command to install dependencies. - Fill the
.env
file with correct value. - Run
yarn dev
to start server.
If you want to have the folder structure we recommend, you could start with create-bottender-app command and migrate it to the custom restify server by following the migration instructions below.
Migrating An Existing Project to Custom Restify Server
Suppose that you already have a project built from create-bottender-app, and you want to develop some additional APIs using restify server. In this case, you need to write a custom restify server and delegate all chatbot's webhook requests to the Bottender app.
To achieve that, you could follow the instructions, as shown below:
- Run
yarn add restify nodemon
ornpm install restify nodemon
command to install dependencies we need. - Create a
server.js
file in the root directory of the project and copy the following code into it.
const restify = require('restify');
const { bottender } = require('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 = restify.createServer();
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.get('/api', (req, res) => {
res.send({ ok: true });
});
server.get('*', (req, res) => {
return handle(req, res);
});
server.post('*', (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
- Modify scripts in the
package.json
tonodemon server.js
andnode server.js
instead:
{
...
"scripts": {
"dev": "nodemon server.js",
"start": "node server.js",
...
},
...
}
That's all you need to do, and you can have you bot hosting on the custom restify server!