From a3268164f580f2aa8dd5c94b456a80259a5f4611 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 26 Jun 2017 12:10:18 +0300 Subject: [PATCH] examples: Add game example References: * PR: https://github.com/yagop/node-telegram-bot-api/pull/342 * PR-by: @MCSH --- examples/expressWebhook.js | 1 + examples/game/game.html | 140 +++++++++++++++++++++++++++++++++++++ examples/game/game.js | 53 ++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 examples/game/game.html create mode 100644 examples/game/game.js diff --git a/examples/expressWebhook.js b/examples/expressWebhook.js index e564e65..200b3df 100644 --- a/examples/expressWebhook.js +++ b/examples/expressWebhook.js @@ -2,6 +2,7 @@ * This example demonstrates setting up a webook, and receiving * updates in your express app */ +/* eslint-disable no-console */ const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN'; const url = 'https://'; diff --git a/examples/game/game.html b/examples/game/game.html new file mode 100644 index 0000000..0158133 --- /dev/null +++ b/examples/game/game.html @@ -0,0 +1,140 @@ + + + + + + + + +

+

Use the ACCELERATE button to stay in the air

+

How long can you stay alive?

+ + + diff --git a/examples/game/game.js b/examples/game/game.js new file mode 100644 index 0000000..2180863 --- /dev/null +++ b/examples/game/game.js @@ -0,0 +1,53 @@ +/** + * This example demonstrates using HTML5 games with Telegram. + */ +/* eslint-disable no-console */ + +const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN'; +const gameName = process.env.TELEGRAM_GAMENAME || 'YOUR_TELEGRAM_GAMENAME'; +// Specify '0' to use ngrok i.e. localhost tunneling +let url = process.env.URL || 'https://'; +const port = process.env.PORT || 8080; + +const TelegramBot = require('../..'); +const express = require('express'); +const path = require('path'); + +const bot = new TelegramBot(TOKEN, { polling: true }); +const app = express(); + +// Basic configurations +app.set('view engine', 'ejs'); + +// Tunnel to localhost. +// This is just for demo purposes. +// In your application, you will be using a static URL, probably that +// you paid for. :) +if (url === '0') { + const ngrok = require('ngrok'); + ngrok.connect(port, function onConnect(error, u) { + if (error) throw error; + url = u; + console.log(`Game tunneled at ${url}`); + }); +} + +// Matches /start +bot.onText(/\/start/, function onPhotoText(msg) { + bot.sendGame(msg.chat.id, gameName); +}); + +// Handle callback queries +bot.on('callback_query', function onCallbackQuery(callbackQuery) { + bot.answerCallbackQuery(callbackQuery.id, url, true, { url }); +}); + +// Render the HTML game +app.get('/', function requestListener(req, res) { + res.sendFile(path.join(__dirname, 'game.html')); +}); + +// Bind server to port +app.listen(port, function listen() { + console.log(`Server is listening at http://localhost:${port}`); +});