Онлайн компилятор Python

import requests TOKEN = "YOUR_BOT_TOKEN" URL = f"https://api.telegram.org/bot{TOKEN}/" def get_updates(): r = requests.get(URL + "getUpdates") return r.json()["result"] def send_message(chat_id, text): requests.post(URL + "sendMessage", json={"chat_id": chat_id, "text": text}) # Poll for updates for update in get_updates(): chat_id = update["message"]["chat"]["id"] send_message(chat_id, "Hello!") (me)hello
The code defines a Telegram bot that fetches updates and replies "Hello!" to each message. It uses the `requests` library to call the Telegram API. The loop runs once over the current updates, so it will not keep listening for new messages.

- The `get_updates` function retrieves all pending updates, but the loop only processes them once. To keep the bot active, you need to repeatedly call `get_updates` in a loop, and also track which updates have already been handled (using the `update_id` field) to avoid replying to the same message multiple times.
- The `send_message` function posts a message, but it does not check the response. If the API returns an error (e.g., invalid token or chat ID), the bot will silently fail. Consider checking `r.status_code` or `r.json()["ok"]` to handle errors.