Skip to content

Instantly share code, notes, and snippets.

@nosqd
Created May 30, 2022 08:19
Show Gist options
  • Select an option

  • Save nosqd/867eff5235b0e73addcf484dfb0de3aa to your computer and use it in GitHub Desktop.

Select an option

Save nosqd/867eff5235b0e73addcf484dfb0de3aa to your computer and use it in GitHub Desktop.
Telegraf + Express
# .env
TOKEN=<TOKEN>
PORT=80
HOST=0.0.0.0
<!-- views/index.html -->
<!doctype html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Telegraf + Express</title>
</head>
<body>
<form action="/" method="get">
<input type="text" name="firstbuttonname" id="firstbuttonname" >
<input type="text" name="secondbuttonname" id="secondbuttonname" >
<input type="submit" value="Сохранить">
</form>
<script>
document.addEventListener('DOMContentLoaded', function(event) {
document.getElementById('firstbuttonname').value = "{{ keyboard.firstbutton }}";
document.getElementById('secondbuttonname').value = "{{ keyboard.secondbutton }}";
});
</script>
</body>
</html>
// npm install express telegraf dotenv nunjucks
// index.js
const express = require("express");
const { Telegraf, Markup } = require('telegraf');
const dotenv = require("dotenv");
const nunjucks = require("nunjucks");
dotenv.config();
const app = express();
const bot = new Telegraf(process.env.TOKEN);
let keyboard = {
firstbutton: "Кнопка 1",
secondbutton: "Кнопка 2"
}
nunjucks.configure('views', {
autoescape: true,
express: app
});
bot.command("start", ctx => ctx.reply(
"Привет.",
Markup.keyboard(Object.values(keyboard))
.resize()
));
app.get("/", (req, res) => {
console.log(req.query);
if (req.query.firstbuttonname) {
keyboard.firstbutton = req.query.firstbuttonname;
keyboard.secondbutton = req.query.secondbuttonname;
}
res.render("index.html", {keyboard});
});
app.listen(parseInt(process.env.PORT), process.env.HOST, () => {
console.log(`Express запущен на порту ${process.env.PORT}`);
bot.launch();
})
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment