Telegram
This document describes how to create a Telegram Bot through BotFather, send messages to Tencent Cloud EdgeOne Makers via Webhook, and have the Agent generate replies and return them to the original chat.
Prerequisites and Scope of Application
1. You already have a Telegram account and can access the official BotFather.
2. Makers has been activated.
3. Python 3 has been installed locally to run the Webhook registration script described in this document.
4. This document covers private chat text and group targeted commands. It does not cover channel posts, payments, attachments, or Telegram Mini Apps.
Creating a Bot and Obtaining a Token
1. Open the official BotFather in Telegram and confirm that the username is `@BotFather`.
2. Send `/newbot` and enter a display name as prompted, such as `Makers Assistant`.
3. Enter a unique Bot username. As required by BotFather, the username must end with `bot`.
4. After successful creation, copy the returned Bot Token and store it securely.
5. Record the Bot username, which will be used later to open private chats or specify the recipients of group commands.
Determining Permissions and Session Scope
Telegram does not use an OAuth Scope table. This document uses the following minimal configuration:
Scenario | Required Action | Description |
Private chat with user | The user first opens the Bot and clicks **Start** or sends `/start`. | Bot cannot proactively initiate a private chat with users who have never contacted it. |
Group-targeted command | Add the Bot to a test group and send `/ask@<Bot username> question`. | Keep Privacy Mode enabled. Bot does not require administrator privileges. |
Reply to messages | Allow the Bot to send messages in its group. | Cannot be in a muted or message-prohibited state. |
If the Bot cannot join groups, use `/setjoingroups` in BotFather to check and allow it to join groups. For the receiving scope of Privacy Mode, see Bots FAQ.
Deploying the Makers Template
1. Open the Makers Agent for IM template.
2. In the project environment variable settings, fill in the following variables (copy .env.example to .env).
Environment Variable | Required | Description |
TELEGRAM_BOT_TOKEN | Yes | Token returned by BotFather |
TELEGRAM_WEBHOOK_SECRET_TOKEN | Yes | Generated Webhook Secret |
3. Save the configuration and deploy it.
4. Copy the production HTTPS domain name, and append /telegram to the end.
5. Note that the service should be deployed in a global AZ so that the telecom service can be accessed correctly.
Registering a Webhook
1. Run the following complete command in a local terminal to create and run the registration script. The script reads the Token and Webhook Secret through hidden input.
2. The Secret you enter must match the `TELEGRAM_WEBHOOK_SECRET_TOKEN` in Makers. The script will set the current Bot's message receiving address to the URL you enter and query the registration result.
3. Use the test Bot you just created.
cat > register-telegram-webhook.py <<'PY'import getpassimport jsonimport reimport urllib.errorimport urllib.parseimport urllib.requestbot_token = getpass.getpass('Bot Token (hidden input):').strip()secret = getpass.getpass('Webhook Secret (must match Makers):').strip()webhook_url = input('Full HTTPS callback URL:').strip()if not re.fullmatch(r'[A-Za-z0-9_-]{1,256}', secret):raise SystemExit('Webhook Secret can only contain letters, digits, underscores, and hyphens, with a length of 1 to 256 characters.')if not webhook_url.startswith('https://') or not webhook_url.endswith('/telegram'):raise SystemExit('Please enter the production HTTPS domain name followed by /telegram.')def api(method, fields):request = urllib.request.Request('https://api.telegram.org/bot' + bot_token + '/' + method,data=urllib.parse.urlencode(fields).encode(),method='POST',)try:with urllib.request.urlopen(request, timeout=30) as response:result = json.load(response)except urllib.error.HTTPError as error:raise SystemExit(f'Telegram HTTP {error.code}. Check the Token and network.') from Noneexcept urllib.error.URLError:raise SystemExit('Unable to connect to the Telegram Bot API. Check your network.') from Noneif not result.get('ok'):raise SystemExit('Telegram rejected the request. Check the URL, certificate, and parameters.')return result['result']api('setWebhook', {'url': webhook_url,'secret_token': secret,'allowed_updates': json.dumps(['message']),})info = api('getWebhookInfo', {})print('Webhook URL:', info.get('url'))print('Pending updates:', info.get('pending_update_count', 0))print('Last error time:', info.get('last_error_date', 'none'))print('Registration complete. Send a message to verify.')PYpython3 register-telegram-webhook.py
Sending a Message and Verifying It
1. Open the Bot using the link provided by BotFather, and click Start.
2. Send `Please introduce Tencent Cloud EdgeOne Makers in one sentence.`.
3. Confirm that `Thinking…` appears in the chat, and then the message is updated with the final reply.
4. Send `Please shorten the previous answer to no more than ten words.` again, and confirm the second-round reply.

