Telegram webhook integration to express app - node.js

So on the Telegraf api they have this example.
const Telegraf = require('telegraf')
const express = require('express')
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.on('text', ({ replyWithHTML }) => replyWithHTML('<b>Hello</b>'))
bot.telegram.setWebhook('https://----.localtunnel.me/secret-path')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.use(bot.webhookCallback('/secret-path'))
app.listen(3000, () => {
console.log('Example app listening on port 3000!')
})
And it clearly work fine because i can see that i get post requests on /secret-path route.
But what i don't understand is how do i work with those requests.
As far as i understand how this is working is that app.use(bot.webhookCallback('/secret-path')) creates this route in my express app.
Anyway, the question is, how do i work with requests that come to this route e.g. if i want to console.log request.body and so on.

The solution seems to be in this function. If body-parser.json() is used.
app.post(`/secret-path`, (req, res) => {
console.log(req.body)
return bot.handleUpdate(req.body, res)
})
Correct me if i'm wrong.

Related

How to get messages by using Webhooks such as the Getupdeats method in the Bot Telegram Documentation?

I would like to create a telegram bot with Express JS and Telegraph JS and get messages from the user in the way
I created a host using ngrok
const { Telegraf } = require('telegraf');
const bot = new Telegraf(config.get('Customer.BOT_CONFIG.TOKEN'))
const app = require('express')()
app.use(bot.webhookCallback('/https://c2b4-5-237-204-80.eu.ngrok.io'))
bot.telegram.setWebhook(`https://api.telegram.org/TOKEN/setwebhook?url=https://c2b4-5-237-204-80.eu.ngrok.io`)
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(3000, (c) => {
console.log('server is running on port 3000');
})

My node.js server is up, but discord.js bot is offline

I'm using discord.js to make a discord bot on replit. I have a keep_alive which makes the server remain online with no problem, but the bot goes offline after a while.
Currently, uptimerobot is keeping the server alive, but the bot goes down in a few hours. I tried to following code to detect ratelimits:
client.on('debug', (a)=>{
if(a.startsWith(`Hit a 429`)){
process.kill(1)
}
});
client.on("rateLimit", data => {
process.kill(1)
})
client.on('rateLimited', () => {
process.kill(1);
});
And here is my code for the keep_alive:
var http = require('http');
http.createServer(function (req, res) {
res.write("I'm alive"); res.end();
}).listen(8080);
I've also tried using:
//https://github.com/mrmotchy/keep-alive
const express = require('express');
const server = express();
server.all('/', (req, res) => {
res.send(`OK`)
})
function keepAlive() {
server.listen(3000, () => { console.log("Server is Ready!!" + Date.now()) });
}
And
//https://replit.com/talk/learn/How-to-keep-your-discordant-bot-alive-in-Replit-2020/46432
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
And also other websites similar to uptimerobot, all of these combinations yield the same result.

nodejs express router doesn't catch get request to /mypage.html

My goal is
To print a message to my console when a user enters a specific page
To make it possible to access a page without having to write the .html extension.
If i use the following, where test.html is not an existing page, i will see
the expected message in my console when the user tries to access /test or /test.html page.
router.get(/^\/test(\.html)?$/, async (req, res) => {
console.log('User trying to access test.html page')
res.send('welcome to test page')
})
But if i do the same for an existing page (/dashboard.html)
router.get(/^\/dashboard(\.html)?$/, async (req, res) => {
console.log('User trying to access dashboard.html page')
res.send('welcome to dashboard page')
})
I will see the expected message in my console when the user tries to access /dashboard but when he tries to access /dashboard.html the page will just load without seeing any message in my console.
Why is this happening?
I think the problem is that you are telling your app to use the static files before you tell your app to use your router.
I mean, if you do this (let´s say we have in the public folder the dashboard.html file):
const express = require("express");
const app = express();
const router = express.Router();
const port = 3000;
router.get(/^\/test(\.html)?$/, async (req, res) => {
console.log("User trying to access test.html page");
res.send("welcome to test page");
});
router.get(/^\/dashboard(\.html)?$/, async (req, res) => {
console.log("User trying to access dashboard.html page");
res.send("welcome to dashboard page");
});
app.use("/", router);
app.use(express.static("public"));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
It should work as you expected.
However it seems you are placing the app.use(express.static...) before the app.use router. Something like this:
const express = require("express");
const app = express();
const router = express.Router();
const port = 3000;
app.use(express.static("public"));
router.get(/^\/test(\.html)?$/, async (req, res) => {
console.log("User trying to access test.html page");
res.send("welcome to test page");
});
router.get(/^\/dashboard(\.html)?$/, async (req, res) => {
console.log("User trying to access dashboard.html page");
res.send("welcome to dashboard page");
});
app.use("/", router);
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
So in this case when you type the exact path for the dashboard.html it won´t use the router to resolve the content and it will simply take it from the public folder.
It is just a matter of order for the app.uses(...) in the code

Can get('/', function (req, res){} can't be called server

I was working on fetching data from mysql and using it to create d3js graph on node.js. I learnt that server can only be made from modules 'http' and 'request'. If I am not using them and using
app.get('/', function (req, res) {
res.render('index', {
//
});
});
app.listen(port, function () {
console.log('running server on port ' + port)
});
They are solving the same purpose.
What concept of server am I missing? What are the limitations of fetching data this way?
use express web framework :
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
let port=3000;
app.listen(port, function () {
console.log('running server on port ' + port)
});

Multiple routes matched at the same time - Strange Express behavior

I am wondering if the following express behavior is valid :
In this example, making a GET request "GET /count" will trigger/execute both routes /count and /:mail.
var express = require('express')
var app = express()
app.get('/count', function (req, res) {
res.send('Hello World!1')
})
app.get('/:mail', function (req, res) {
res.send('Hello World!2')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
When the /:mail is triggered, the output will be : "Error: Can't set headers after they are sent."
My question is : Isn't it obvious for express to match only one single route at once ?

Resources