Онлайн компилятор 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!")
This code defines a simple Telegram bot that fetches recent updates and replies "Hello!" to each message. It uses the `requests` library to call the Telegram Bot API, with `get_updates()` retrieving new messages and `send_message()` posting a reply. The loop processes each update and sends a response to the chat where the message originated.

- The bot only processes updates that exist at the moment the script runs. To keep it running continuously, you would need to wrap the loop in a `while True` block, but be careful about repeatedly fetching the same updates—consider using an offset parameter to mark processed updates.
- The code assumes every update contains a `message` key, but updates can include other types (like edited messages or callback queries). Add a check like `if "message" in update:` before accessing the chat ID to avoid errors.