mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-22 09:57:10 +00:00
References: * BR: https://github.com/yagop/node-telegram-bot-api/issues/418 * PR: https://github.com/yagop/node-telegram-bot-api/pull/449
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
/**
|
|
* 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://<PUBLIC-URL>';
|
|
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 });
|
|
});
|
|
|
|
// 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}`);
|
|
});
|