Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update slack routes improvement #759

Merged
merged 11 commits into from
May 7, 2020
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.4.4 / 2020-05-06

## slack

- [fix] convert slack interactive message event to camelcase (#755).

# 1.4.3 / 2020-04-29

- [type] use string instead enum to compare.
Expand Down
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,23 @@ Here is the first one to get you started:

```js
// index.js
module.export = async function App(context) {
await context.sendText('Hello World');
};
```
const { router, text } = require('bottender/router');

```sh
npx bottender start --console
```
async function SayHi(context) {
await context.sendText('Hi!');
}

It creates and runs a bot that always replies with "Hello World" in the console.
async function Unknown(context) {
await context.sendText('Sorry, I don’t know what you say.');
}

module.export = function App(context) {
return router([
text('hi', SayHi),
text('*', Unknown),
]);
};
```

## Notable Features

Expand Down
76 changes: 26 additions & 50 deletions docs/advanced-guides-custom-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ title: Running Bottender with Custom Servers

Thus, You can find those lines in your generated `package.json`:

```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.
Expand All @@ -27,7 +21,7 @@ To run Bottender with the custom HTTP server, you need to prepare an HTTP server

## Express

### Creating A New Project with Custom Express Server
### 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](https://github.com/Yoctol/bottender/tree/master/examples/custom-server-express) to develop your project. There are four steps you could follow to create your project:

Expand All @@ -38,7 +32,7 @@ If you want to have a clean project with the custom express, you could start fro

If you want to have the folder structure we recommend, you could start with [create-bottender-app](getting-started.md#create-a-new-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
### Migrating an Existing Project to Custom Express Server

Suppose that you already have a project built from [create-bottender-app](getting-started.md#create-a-new-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.

Expand All @@ -64,13 +58,11 @@ const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();

server.use(
bodyParser.json({
verify: (req, _, buf) => {
req.rawBody = buf.toString();
},
})
);
const verify = (req, _, buf) => {
req.rawBody = buf.toString();
};
server.use(bodyParser.json({ verify }));
server.use(bodyParser.urlencoded({ extended: false, verify }));

// your custom route
server.get('/api', (req, res) => {
Expand All @@ -89,25 +81,19 @@ app.prepare().then(() => {
});
```

3. Modify scripts in the `package.json` to `nodemon server.js` and `node server.js` instead:
3. Modify `scripts` in the `package.json` to `nodemon server.js` and `node server.js` instead:

```json
{
...
```
"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
### 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](https://github.com/Yoctol/bottender/tree/master/examples/custom-server-koa) to develop your project. There are four steps you could follow to create your project:

Expand All @@ -118,7 +104,7 @@ If you want to have a clean project with the custom koa, you could start from [t

If you want to have the folder structure we recommend, you could start with [create-bottender-app](getting-started.md#create-a-new-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
### Migrating an Existing Project to Custom Koa Server

Suppose that you already have a project built from [create-bottender-app](getting-started.md#create-a-new-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.

Expand All @@ -143,6 +129,14 @@ const handle = app.getRequestHandler();

app.prepare().then(() => {
const server = new Koa();

server.use(bodyParser());
server.use((ctx, next) => {
ctx.req.body = ctx.request.body;
ctx.req.rawBody = ctx.request.rawBody;
return next();
});

const router = new Router();

router.get('/api', ctx => {
Expand All @@ -154,12 +148,6 @@ app.prepare().then(() => {
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 => {
Expand All @@ -169,25 +157,19 @@ app.prepare().then(() => {
});
```

3. Modify scripts in the `package.json` to `nodemon server.js` and `node server.js` instead:
3. Modify `scripts` in the `package.json` to `nodemon server.js` and `node server.js` instead:

```json
{
...
```
"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
### 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](https://github.com/Yoctol/bottender/tree/master/examples/custom-server-restify) to develop your project. There are four steps you could follow to create your project:

Expand All @@ -198,7 +180,7 @@ If you want to have a clean project with the custom restify, you could start fro

If you want to have the folder structure we recommend, you could start with [create-bottender-app](getting-started.md#create-a-new-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
### Migrating an Existing Project to Custom Restify Server

Suppose that you already have a project built from [create-bottender-app](getting-started.md#create-a-new-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.

Expand Down Expand Up @@ -243,18 +225,12 @@ app.prepare().then(() => {
});
```

3. Modify scripts in the `package.json` to `nodemon server.js` and `node server.js` instead:
3. Modify `scripts` in the `package.json` to `nodemon server.js` and `node server.js` instead:

```json
{
...
```
"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!
7 changes: 7 additions & 0 deletions docs/channel-slack-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function App() {
slack.message(HandleMessage),
slack.event('pin_added', HandlePinAdded),
slack.event('star_added', HandleStarAdded),
slack.event('*', HandleAnyEvent),
slack.command('/price', HandlePriceCommand),
slack.command('*', HandleAnySlashCommand),
slack.any(HandleSlack),
]);
}
Expand All @@ -21,6 +24,9 @@ function App() {
async function HandleMessage(context) {}
async function HandlePinAdded(context) {}
async function HandleStarAdded(context) {}
async function HandleAnyEvent(context) {}
async function HandlePriceCommand(context) {}
async function HandleAnySlashCommand(context) {}
async function HandleSlack(context) {}
```

Expand All @@ -29,3 +35,4 @@ All available routes in `slack` that recognize different kind of events:
- `slack.any` - triggers the action when receiving any Slack events.
- `slack.message` - triggers the action when receiving Slack message events.
- `slack.event` - triggers the action when receiving particular Slack events. See all event types in [Slack docs](https://api.slack.com/events).
- `slack.command` - triggers the action when receiving Slack slash command events.
12 changes: 5 additions & 7 deletions examples/custom-server-express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();

server.use(
bodyParser.json({
verify: (req, _, buf) => {
req.rawBody = buf.toString();
},
})
);
const verify = (req, _, buf) => {
req.rawBody = buf.toString();
};
server.use(bodyParser.json({ verify }));
server.use(bodyParser.urlencoded({ extended: false, verify }));

server.get('/api', (req, res) => {
res.json({ ok: true });
Expand Down
14 changes: 8 additions & 6 deletions examples/custom-server-koa/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const handle = app.getRequestHandler();

app.prepare().then(() => {
const server = new Koa();

server.use(bodyParser());
server.use((ctx, next) => {
ctx.req.body = ctx.request.body;
ctx.req.rawBody = ctx.request.rawBody;
return next();
});

const router = new Router();

router.get('/api', ctx => {
Expand All @@ -24,12 +32,6 @@ app.prepare().then(() => {
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 => {
Expand Down
2 changes: 1 addition & 1 deletion examples/messenger-persistent-menu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If you successfully start the server, you get a webhook URL in the format of `ht
While the server is running, you can run following command with `bottender` to set up the webhook with the webhook url you get from running `npm run dev`:

```sh
npx bottender messenger webhook set -w <YOUR_WEBHOOK_URL>
npx bottender messenger webhook set
```

> **Note:** You must fill `APP_ID`, `APP_SECRET` and `VERIFY_TOKEN` in your `.env` file before running this command.
Expand Down
3 changes: 3 additions & 0 deletions examples/telegram-game/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TELEGRAM_ACCESS_TOKEN=
GAME_NAME=
ROOT_PATH=
63 changes: 63 additions & 0 deletions examples/telegram-game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Telegram Game

## Install and Run

Download this example or clone [bottender](https://github.com/Yoctol/bottender).

```sh
curl https://codeload.github.com/Yoctol/bottender/tar.gz/master | tar -xz --strip=2 bottender-master/examples/telegram-game
cd telegram-game
```

Install dependencies:

```sh
npm install
```

You must fill `TELEGRAM_ACCESS_TOKEN`, `GAME_NAME` and `ROOT_PATH` in your `.env` file.

If you are not familiar with Telegram Bot, you may refer to Bottender's doc, [Telegram Setup](https://bottender.js.org/docs/channel-telegram-setup), to find detailed instructions.

After that, you can run the bot with this npm script:

```sh
npm run dev
```

This command starts a server listening at `http://localhost:5000` for bot development.

To set up ngrok for your server, run:

```sh
npx ngrok http 5000
```

If you successfully start the server, you get a webhook URL in the format of `https://xxxxxxxx.ngrok.io/webhooks/telegram` from your terminal.

## Set Webhook

While the server running, you can run following command with `bottender` to set up the webhook with the webhook URL you get from running `npm run dev`:

```sh
npx bottender telegram webhook set
```

> **Note:** You must fill in your `.env` file before running this command.

## Idea of This Example

This example is a game bot running on [Telegram](https://telegram.org/).

This example contains the following topics:

- send the game to the user
- set game score of the user
- share the game to a different chat

You must create a new Telegram game by following [Telegram Bot API#Games](https://core.telegram.org/bots/api#games).

## Related Examples

- [telegram-hello-world](../telegram-hello-world)
- [telegram-poll](../telegram-poll)
9 changes: 9 additions & 0 deletions examples/telegram-game/bottender.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
channels: {
telegram: {
enabled: true,
path: '/webhooks/telegram',
accessToken: process.env.TELEGRAM_ACCESS_TOKEN,
},
},
};
Loading