REDL bot template

A Telegram bot that is yours to change. Out of the box it does what the panel's own Telegram mode does (a person writes to the bot, the message goes to your REDL team, the answer comes back to the same chat), and on top of that it has a place for your own logic: commands, buttons, menus, broadcasts.

Plain Node.js 18+, no dependencies, one process, a few hundred lines.

How it works

Telegram ──getUpdates──> bot.mjs ──> handlers/ (your logic: first one that takes the update wins)
                            │
                            └─ nobody took it, private chat ──POST──> TEAM_HOOK_URL (your team on REDL)
                                                                        │
              person <──── the panel sends the team's answer through the bot ────┘
  • Attachments, voice messages (with a transcript) and the formatting of answers are handled by the panel: the bot hands the Telegram message over as is.
  • Who may write to the team is decided on the team board (the person's Telegram name there), not in the bot.
  • Without TEAM_HOOK_URL the bot works alone: only your handlers answer.

Files

File What it is Edit it?
handlers/index.mjs the list of your handlers, in order yes, this is the place
handlers/start.mjs, menu.mjs, broadcast.mjs examples: commands, inline buttons, a broadcast yes, or delete
texts.mjs every phrase the bot says by itself, per language yes
bot.mjs plumbing: config, polling, retries, state, hand-over to the team rarely
.env secrets (mode 600, never in git) once
state/ offset, chats that pressed /start, your ctx.kv data no
test/run.mjs tests on a fake Telegram: node test/run.mjs add yours

Adding your own logic

A handler is an object in handlers/:

// handlers/price.mjs
export default [
  { name: "price", command: "price", async run(ctx) { await ctx.reply("From $5 a month."); } },
  // free text: your own test. Return false from run() to let the team have the message after all.
  { name: "hours", match: (ctx) => /working hours/i.test(ctx.text), async run(ctx) { await ctx.reply("9:00 to 18:00."); } },
];

Then add it to handlers/index.mjs. Restart the service. What ctx has: text, command, args, data (pressed button), from, chatId, chatType, isAdmin, T (phrases), reply(text, extra), answer(text), tg(method, body) (any Telegram Bot API method), kv.get/set (small persistent storage), chats.add/all/remove, forward() (hand this message to the team from inside a handler), log.

For anything bigger (a database, payments, a web page) add files and packages as in any Node project: this is a normal program on your machine.

Install on a machine

git clone https://git-1.redl.app/1themotiv/redl-bot-template.git /opt/redl-bot && cd /opt/redl-bot
sh install.sh                 # creates a private .env and a service that starts with the machine
# fill in .env (see below), then:
node bot.mjs --check          # must end with "all good"
systemctl restart redl-bot && journalctl -u redl-bot -n 20 --no-pager

A second bot on the same machine: clone into another directory and run sh install.sh <another-service-name>.

For a REDL agent: deploying this for a team channel

The client asks for "our bot as a project" for Telegram channel <channel> on machine <machine>:

  1. The channel must be wired to you on the team board. redl get /team/vault/<channel> returns bot_token and team_hook_url. Never print them: pipe them straight into the file on the machine.
  2. No machine: offer one with the price (the bot needs very little: the smallest plan is enough). Your own workspace is not a place for a permanent service.
  3. On the machine: git clone https://git-1.redl.app/1themotiv/redl-bot-template.git /opt/redl-bot (public, no key needed), then sh install.sh (Node 18+ must be there). The clone is the client's own copy: change it freely.
  4. Write the secrets without showing them (run in your workspace):
    redl get /team/vault/<channel> | python3 -c 'import json,sys; f=json.load(sys.stdin)["fields"]; print("BOT_TOKEN="+f["bot_token"]); print("TEAM_HOOK_URL="+f["team_hook_url"])' \
      | vds <machine> "umask 077 && cat > /opt/redl-bot/.env && chmod 600 /opt/redl-bot/.env"
    
    Add ADMIN_IDS=<the client's Telegram numeric ID> to that file if the client wants broadcasts.
  5. Switch the channel to "own program". While the panel receives the channel's messages itself, Telegram gives this bot nothing (the log says 409 from Telegram, --check says webhook: SET). Do it the standard way, never by removing the webhook yourself with the token:
    redl post /team/channel/<channel>/recv '{"mode":"own"}'
    
    The panel refuses (not_allowed, restricted_turn, not_wired): the board owner switches it with the button on the channel's card. Do this only when the board owner asked for it.
  6. The channel's setting "only from the panel's machine" must be off (it is, unless the owner turned it on).
  7. node bot.mjs --check must end with "all good". Then systemctl restart redl-bot.
  8. Prove it: ask the client to write to the bot; journalctl -u redl-bot -n 20 must show forwarded ... and the answer must arrive in Telegram.

Going back: stop the service (systemctl disable --now redl-bot), check that the process is gone (systemctl is-active redl-bot says inactive), then redl post /team/channel/<channel>/recv '{"mode":"panel","stopped":true}'. "stopped":true is your statement that the program no longer runs; the board owner gets a note about who switched the channel. Nothing else has to be undone.

Operating

  • Log: journalctl -u redl-bot -f. It has sender names and IDs, message numbers and outcomes; never message texts, the token or the team address.
  • A message is handed to the team once: retries for up to 10 minutes if the panel is down (the person is told the message is not lost), survives a restart of the bot, is never sent twice.
  • Token changed: put the new one into .env, restart.
  • "all good" in --check but no answers: the person's Telegram name is not on the team board (the bot tells them).
S
Description
REDL: шаблон Telegram-бота для канала команды (своя программа). Секретов нет: токен в .env на машине.
Readme
60 KiB
0 Stars 1 Watchers 0 Forks
Languages
JavaScript 96.1%
Shell 3.9%