2017-02-15 08:17:55 +03:00
|
|
|
/**
|
|
|
|
* This example demonstrates setting up a webook, and receiving
|
|
|
|
* updates in your express app
|
|
|
|
*/
|
2017-06-26 12:10:18 +03:00
|
|
|
/* eslint-disable no-console */
|
2017-02-15 08:17:55 +03:00
|
|
|
|
|
|
|
const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';
|
|
|
|
const url = 'https://<PUBLIC-URL>';
|
|
|
|
const port = process.env.PORT;
|
|
|
|
|
2017-12-07 12:30:44 +03:00
|
|
|
const TelegramBot = require('../..');
|
2017-02-15 08:17:55 +03:00
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
|
|
|
|
// No need to pass any parameters as we will handle the updates with Express
|
|
|
|
const bot = new TelegramBot(TOKEN);
|
|
|
|
|
|
|
|
// This informs the Telegram servers of the new webhook.
|
|
|
|
bot.setWebHook(`${url}/bot${TOKEN}`);
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
// parse the updates to JSON
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
|
|
// We are receiving updates at the route below!
|
|
|
|
app.post(`/bot${TOKEN}`, (req, res) => {
|
|
|
|
bot.processUpdate(req.body);
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start Express Server
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Express server is listening on ${port}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Just to ping!
|
|
|
|
bot.on('message', msg => {
|
|
|
|
bot.sendMessage(msg.chat.id, 'I am alive!');
|
|
|
|
});
|