Code: How to code a Telegram bot with Jennifer
How to code a Telegram bot with Jennifer: the code
The dice bot behind the slides and the fake Bot API it was run against. The bot uses the @deckyard/telegram deck, listed in deck.toml, so run jvc install once in this folder to fill vendor/. Run everything from this folder with Jennifer 0.27.0.
Nothing here talks to the real Telegram: fake-telegram.j stands in for the Bot API on a loopback port, and the bot is pointed at it with TELEGRAM_API. The token 123456:TEST-TOKEN is a placeholder the fake accepts; any other token gets a 401.
deck.toml: the jvc manifest with@deckyard/telegram("Set up").bot.j: the whole bot. getMe and the token check ("First contact: getMe"), the poll loop and the offset ("The loop and the offset"), the commands ("Commands: match on the first word"), the checked/roll("/roll: never trust the input"), the inline keyboard and button presses ("Buttons and callback queries"),sendLogo("Sending a photo or a file"),replywith flood control ("Errors: a struct, flood control, no token in logs"), the SIGTERM stop with the final poll ("Run it as a service, stop it cleanly") andTELEGRAM_API("Test without Telegram"). It logs each update as<- id command, and plain text only astext.DICE_SEED(optional) seeds the dice so a run can be repeated. Run against the real API (a token from @BotFather, never in git):TELEGRAM_BOT_TOKEN=123456:REPLACE-ME jennifer run bot.jfake-telegram.j: the fake Bot API onhttpd. It checks the token in the path, hands out six scripted updates (/start, a press of "Roll 2d6",/roll 3d20,/roll 900d6,/logo,hello), three pergetUpdates, logs every call astg: ..., and answers the fifthsendMessagewith a 429 andretry_after2. An empty poll is held 3 seconds instead of the full timeout ("What Telegram saw", "Test without Telegram"). Run:jennifer run fake-telegram.j 28780 &, thenTELEGRAM_BOT_TOKEN=123456:TEST-TOKEN TELEGRAM_API=http://127.0.0.1:28780 jennifer run bot.jrun.sh: starts the fake, runs the bot withDICE_SEED=649591(the rolls on the slides), stops it with SIGTERM after 10 seconds and prints both logs; the bot ends withstopped, next offset 7. Run:./run.sh(or./run.sh 30for a longer run)jennifer-logo.png: the picture/logouploads (5894 bytes on the wire with the form around it).dicebot.service.ini: the systemd unit from "Run it as a service", with[Unit]and[Install]added; install it as/etc/systemd/system/dicebot.servicewith the token in/etc/dicebot.env(TELEGRAM_BOT_TOKEN=123456:REPLACE-ME).
| File | Size | |
|---|---|---|
| LICENSE | 899 B | raw |
| README.md | 2.5 KiB | raw |
| bot.j | 5.7 KiB | raw |
| deck.toml | 261 B | raw |
| dicebot.service.ini | 515 B | raw |
| fake-telegram.j | 5.5 KiB | raw |
| jennifer-logo.png | 5.4 KiB | |
| run.sh | 644 B | raw |