Telegram Bot to Track IPv4 Blacklists: Setup Guide
In this article
You can build a Telegram bot that checks whether an IPv4 address is listed on public DNS-based blacklists (DNSBLs) and reports back in chat. The bot does not need to be a full server-monitoring platform: a small script on any always-on machine (a VPS, a Raspberry Pi, or a spare PC) can query a handful of DNSBL zones over DNS, parse the response, and send a message through the Telegram Bot API. First, you need a Telegram account, a bot token from @BotFather, Python 3.8 or newer, and outbound DNS access. No paid service is required, though a machine that stays online is necessary if you want scheduled checks.
What You'll Need
- A Telegram account and the Telegram app on desktop or mobile.
- Python 3.8+ installed on the machine that will run the bot, with pip available.
- The
python-telegram-botlibrary and thednspythonlibrary (install with pip). - Outbound DNS access (UDP port 53) from that machine to public resolvers, or a local resolver that can query external zones.
- A text editor and basic comfort running commands in a terminal.
- Optionally, a scheduler such as cron (Linux/macOS) or Task Scheduler (Windows) for periodic checks.
Step-by-Step
- Create the bot and get its token. In Telegram, open @BotFather, send
/newbot, choose a display name and a username ending inbot. BotFather replies with an HTTP API token. Treat this token like a password: anyone with it can control the bot. Do not paste it into public code repositories. - Decide how the bot will receive commands. The simplest reliable method is long polling, where your script asks Telegram for new updates. Webhooks are possible but require a public HTTPS endpoint, which adds complexity. For a personal blacklist checker, long polling is enough.
- Install the libraries. In a terminal, run
pip install python-telegram-bot dnspython. If you use a virtual environment, activate it first. Confirm the install by runningpython -c "import telegram, dns.resolver; print('ok')". - Write the DNSBL check function. A DNSBL query works by reversing the IPv4 octets and appending the zone. For example, to check
192.0.2.10againstzen.spamhaus.org, query10.2.0.192.zen.spamhaus.orgfor an A record. If an A record exists, the address is listed; the returned address often encodes the listing reason. If the query returns NXDOMAIN, the address is not listed on that zone. Handle timeouts and SERVFAIL separately from NXDOMAIN so you do not report a network error as a clean result. - Choose your blacklist zones. Common public DNSBLs include
zen.spamhaus.org,bl.spamcop.net,b.barracudacentral.org, anddnsbl.sorbs.net. Some require registration or have usage limits; check each provider's terms before querying at volume. Start with two or three zones and expand only if you need them. - Build the command handler. With python-telegram-bot, define a handler for a command such as
/checkthat reads the argument after the command, validates it as an IPv4 address (four octets, each 0–255), runs the DNSBL queries, and replies with a formatted result. Reject anything that is not a valid IPv4 address with a short error message. - Format the reply clearly. For each zone, show the zone name and one of: listed, not listed, or check failed. Include the returned A record when listed, because some zones use it to indicate the reason. Keep the message under Telegram's length limit; if you check many zones, split into multiple messages or summarise.
- Add rate limiting and input checks. Limit how often a single chat can run
/check(for example, one request every few seconds) to avoid hammering DNSBL providers and hitting their limits. Validate the input before any DNS query so malformed input cannot trigger unexpected lookups. - Run the bot and test it. Start the script, open your bot in Telegram, and send
/startthen/check 192.0.2.10. Use a documentation address such as 192.0.2.10 for testing; it is reserved and will not affect real mail flow. Confirm the bot replies and that the output matches what a manual DNS query returns. - Add scheduled monitoring if you want alerts. Write a separate script or a scheduled job that runs the same check against your own addresses and sends a Telegram message only when the result changes from clean to listed. Use cron on Linux/macOS or Task Scheduler on Windows. Store the bot token in an environment variable or a file with restricted permissions rather than in the script itself.
- Keep it running. On Linux, run the script under
systemdor a process manager so it restarts after a reboot or crash. On Windows, use Task Scheduler with a trigger at logon. Check the logs periodically for DNS timeouts, which are the most common cause of missed alerts.
Common Mistakes
Treating NXDOMAIN and a timeout as the same result is the most frequent error: a timeout means the check failed, not that the address is clean. Reversing the octets incorrectly is another common bug; the order must be reversed, not just the string split. Using a blacklist zone that forbids automated queries can get your resolver blocked, so read each provider's usage policy. Hard-coding the bot token in a public repository exposes the bot to anyone who finds it. Finally, checking only one blacklist gives a false sense of security; different zones list different addresses for different reasons.
Tips
Cache results for a short period (for example, 10–15 minutes) so repeated checks do not hit DNSBL providers unnecessarily. Log every query and result with a timestamp so you can tell a real listing from a transient DNS failure. Use a resolver you control or a reputable public resolver; a misconfigured local resolver can return false negatives. Keep the zone list in a configuration file rather than in code, so you can add or remove zones without editing the script. If you monitor your own mail server, check the sending IP, not the domain, because DNSBLs list addresses.
Troubleshooting
If the bot does not respond at all, confirm the script is running and that the token is correct; a wrong token usually produces an unauthorized error in the logs. If commands are ignored, check that the handler is registered for the exact command name and that the bot is not stuck in a webhook mode you did not configure. If every check returns "failed," test DNS resolution from the same machine with a tool such as dig or nslookup against the same zone; if that also fails, the problem is outbound DNS, not your code. If results differ from a web-based checker, remember that some checkers query different zones or use a different resolver, and listing status can change between queries. If Telegram replies stop after a while, look for rate-limit errors in the logs and slow down scheduled checks.
Frequently Asked Questions
Do I need a server to run the bot?
Not strictly, but the bot only works while the script is running. A machine that stays online is needed for reliable scheduled checks.
Can the bot check IPv6 addresses?
DNSBLs are primarily designed for IPv4. Some support IPv6 through separate zones, but support is inconsistent, so treat IPv4 as the main case.
Is a listing on a DNSBL permanent?
No. Listings can expire or be removed after the cause is fixed, and some providers offer delisting procedures. The bot reports the current state only.
Why does a web checker show a different result?
Different checkers query different zones and resolvers, and listings change over time. Compare the specific zones rather than the overall verdict.
Can I check many addresses at once?
Yes, but respect each provider's query limits and add delays between lookups. Bulk querying without limits can get your resolver blocked.
Is the bot token safe to share?
No. Anyone with the token can control the bot. Store it in an environment variable or a restricted file and revoke it through BotFather if it leaks.