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

example: telegram inline keyboard #754

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion examples/telegram-game/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 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 -w <YOUR_WEBHOOK_URL>
npx bottender telegram webhook set
```

> **Note:** You must fill in your `.env` file before running this command.
Expand Down
2 changes: 1 addition & 1 deletion examples/telegram-hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 the following command with `bottender` to set up the webhook with the webhook URL you got from running `npm run dev`:

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

> **Note:** You must fill in your `.env` file before running this command.
Expand Down
1 change: 1 addition & 0 deletions examples/telegram-inline-keyboard/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TELEGRAM_ACCESS_TOKEN=
52 changes: 52 additions & 0 deletions examples/telegram-inline-keyboard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Telegram Inline Keyboard

## 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-inline-keyboard
cd telegram-inline-keyboard
```

Install dependencies:

```sh
npm install
```

You must fill `TELEGRAM_ACCESS_TOKEN` 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.

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 is running, you can run the following command with `bottender` to set up the webhook with the webhook URL you got 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 bot about inline keyboard running on [Telegram](https://telegram.org/).

This example contains the following topics:

- show the inline keyboard to the user
- build a multiple layer inline keyboard

For more information, check [Telegram Bots#Inline Keyboards](https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating)

9 changes: 9 additions & 0 deletions examples/telegram-inline-keyboard/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,
},
},
};
94 changes: 94 additions & 0 deletions examples/telegram-inline-keyboard/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const { router, telegram, text } = require('bottender/router');

function generateInlineKeyboard(table) {
return {
inlineKeyboard: table.map(row => {
return row.map(cell => {
return {
text: cell,
callbackData: cell,
};
});
}),
};
}

const mainMenu = {
text: 'This is main menu, please click an option.',
replyMarkup: generateInlineKeyboard([
['A', 'B'],
['C', 'D'],
]),
};

const submenuA = {
text: 'This is submenu A.',
replyMarkup: generateInlineKeyboard([
['A1', 'A2'],
['A3', 'A4'],
['< back to main menu'],
]),
};

const submenuB = {
text: 'This is submenu B.',
replyMarkup: generateInlineKeyboard([
['B1', 'B2'],
['B3', 'B4'],
['< back to main menu'],
]),
};

const submenuC = {
text: 'This is submenu C.',
replyMarkup: generateInlineKeyboard([
['C1', 'C2'],
['C3', 'C4'],
['< back to main menu'],
]),
};

const submenuD = {
text: 'This is submenu D.',
replyMarkup: generateInlineKeyboard([
['D1', 'D2'],
['D3', 'D4'],
['< back to main menu'],
]),
};

const menuMapping = {
'< back to main menu': mainMenu,
A: submenuA,
B: submenuB,
C: submenuC,
D: submenuD,
};

async function DefaultAction(context) {
await context.sendText('Please type "show keyboard" to show the keyboard.');
}

async function ShowKeyboard(context) {
await context.sendText(mainMenu.text, { replyMarkup: mainMenu.replyMarkup });
}

async function AnswerKeyboard(context) {
const callbackQuery = context.event.callbackQuery;
const messageId = callbackQuery.message.messageId;
const data = callbackQuery.data;
const menu = menuMapping[data];
if (menu) {
context.editMessageText(messageId, menu.text, { replyMarkup: menu.menu });
} else {
context.editMessageText(messageId, `Your final choice is ${data}.`);
}
}

module.exports = async function App(context) {
return router([
text('show keyboard', ShowKeyboard),
telegram.callbackQuery(AnswerKeyboard),
telegram.any(DefaultAction),
]);
};
9 changes: 9 additions & 0 deletions examples/telegram-inline-keyboard/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"scripts": {
"dev": "bottender dev",
"start": "bottender start"
},
"dependencies": {
"bottender": "latest"
}
}
2 changes: 1 addition & 1 deletion examples/telegram-inline-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 the following command with `bottender` to set up the webhook with the webhook URL you got from running `npm run dev`:

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

> **Note:** You must fill in your `.env` file before running this command.
Expand Down
2 changes: 1 addition & 1 deletion examples/telegram-poll/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ If you successfully start the server, you get a webhook URL in the format of `ht
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 -w <YOUR_WEBHOOK_URL>
npx bottender telegram webhook set
```

> **Note:** You must fill in your `.env` file before running this command.
Expand Down
2 changes: 1 addition & 1 deletion examples/telegram-reply-keyboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 the following command with `bottender` to set up the webhook with the webhook URL you got from running `npm run dev`:

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

> **Note:** You must fill in your `.env` file before running this command.
Expand Down
2 changes: 1 addition & 1 deletion examples/viber-hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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 the following command with `bottender` to set up the webhook with the webhook URL you got from running `npm run dev`:

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

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