-
-
Save nosqd/867eff5235b0e73addcf484dfb0de3aa to your computer and use it in GitHub Desktop.
Telegraf + Express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # .env | |
| TOKEN=<TOKEN> | |
| PORT=80 | |
| HOST=0.0.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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