Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: A Poll Bot with Buttons

Build a poll bot for Telegram and Discord: create polls, handle button callbacks and custom_id values, allow one changeable vote per user, draw result bars and close polls.

A poll is a perfect button-driven project. Someone posts a question with several options, each option is a button, people tap to vote, and the message updates live with bars showing the results. It teaches the part of bot programming that command-only bots skip: callbacks from buttons, shared state that many users change at once, and editing an existing message instead of sending a new one.

The two platforms name the pieces differently, but the flow is identical:

Step Telegram Discord
Create /poll Question? ; A ; B ; C slash command with question and options
Buttons inline keyboard, callback_data (max 64 bytes) button components, custom_id (max 100 characters)
A tap arrives as callback_query update interaction type 3 (message component)
You answer with answerCallbackQuery then editMessageText response type 7 (update message)

The core below handles both with a neutral Poll service. Adapters turn button taps into vote(pollId, userId, optionIndex) calls.

Encoding button data

A button has no memory, so everything the bot needs travels inside its data string: action, poll ID and option. Keep it short and parse it strictly, because a tap can be forged (anyone can craft a callback with any string).

Node.js Encode and decode button data safely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Never put whole texts or secrets in callback data. Put an ID and look the rest up in your state.

The poll service

Rules we want: between 2 and 6 options; each user has one vote that they can change by tapping another option, or withdraw by tapping the same option again; only the creator (or an admin) can close the poll; closed polls reject votes.

Node.js Poll service with one vote per user

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Vote storage is a Map of user to option, which makes "one vote per user" automatic: setting a vote for the same user simply replaces the old one. Tallying counts from that map each time means there is no second counter that could drift out of sync.

Handling the tap

Both platforms require you to acknowledge a button tap within a few seconds, or the user sees a spinner or an error. The handler follows a fixed shape: decode, run the action, answer the platform, then update the message.

Node.js Button callbacks on both platforms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two lessons from this shape. First, all rules live in onButton; the adapters only translate. Second, custom_id and callback_data come from the client, so the poll service must re-check everything: the poll exists, it is open, the option is in range, and the closer is the creator. Never trust that "only the creator sees the Close button": anyone who can see the message can press it.

Platform limits to remember

  • Telegram callback data is at most 64 bytes; Discord custom_id at most 100 characters; a Discord action row holds up to five buttons and a message up to five rows.
  • Telegram also has a native poll type (sendPoll) with anonymous and quiz modes. A custom button poll is worth building when you want live editing, changeable votes, or rules the native poll cannot express.
  • Many people tapping at once means many edits of the same message, and both platforms rate-limit edits. Coalesce: mark the poll "dirty" and refresh the message at most once or twice a second (rate limits).
  • Store polls in SQLite, with a votes table keyed by (poll_id, user_id), so they survive restarts (SQLite storage).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why must button data be validated even if the bot created the buttons?

    A crafted request can contain arbitrary strings; parse strictly and re-check permissions.
  2. 2. How does the Map of userId to option enforce one vote per user?

    A Map has one value per key, so a new vote overwrites the earlier one.
  3. 3. Which Discord response type updates the message a button was on?

    Type 7 (UPDATE_MESSAGE) edits the original message; type 4 sends a new message.
  4. 4. Why coalesce poll message updates?

    Refreshing once or twice per second keeps the display current without exceeding edit limits.

Summary

  • Encode a small, strictly parsed identifier in button data and keep the real state on the server.
  • Keep rules in one platform-neutral service; adapters translate callback queries and component interactions.
  • One vote per user is a Map keyed by user; tallies are computed, not stored separately.
  • Re-check permission, state and range on every tap, and coalesce message edits.

Next: Project: a moderation bot