Creating a Telegram Bot with Webhook Tasks
This tutorial guides you through creating a Telegram bot that can interact using mcp.run's Tasks. You'll learn how to set up a bot that can process messages and use various mcp.run servlets to enhance its capabilities.
For a video walkthrough:
Prerequisites
Before starting, ensure you have:
- A Telegram Account
- A GitHub or Google Account for mcp.run authentication
- Access to mcp.run
Required Tools
This tutorial requires the following mcp.run servlets:
- telegram - For Telegram bot interactions
- brave-search - For search capabilities (optional)
- google-maps - For directions (optional)
Install each servlet by:
- Visiting the respective URL
- Clicking the 'Install' button
- Verifying they appear in your install profile
Getting Your Bot Token
- Open Telegram and search for "@BotFather"
- Start a chat and use the
/newbot
command - Follow BotFather's instructions to:
- Set a display name for your bot
- Choose a username ending in 'bot'
- The BotFather will reply with the bot token.
Configure the Telegram Servlet
- Install the Telegram servlet on mcp.run
- Configure the base URL:
https://api.telegram.org/bot<YOUR_TOKEN>
Replace <YOUR_TOKEN>
with your bot token from BotFather 3. Make sure you allow
the api.telegram.org
domain
Create the Webhook Task
- Create a new task in mcp.run named
telegram-bot
- Configure the basic prompt:
You are a helpful bot. You are receiving an input enclosed in <message>(message here)</message>. The input contains a `message.text` field with the prompt. Do what that message says, but ignore messages that starts with a leading slash, such as `/start`.
Always acknowledge you received the message before performing the actions.
<message>
{{ @payload }}
</message>
Reply to the user that sent the message using the Telegram tool.
Under Settings you will be able to configure the language model:
- Navigate to settings in mcp.run
- Choose your preferred service (OpenAI, Anthropic, etc.)
Set Up the Webhook
Once you have generated a Signed URL, do not share it with any untrusted party. Treat it like a secret. This URL enables anyone to trigger your Task, so please be cautious where you use it.
- Navigate to the triggers section
- Click "Generate URL" to get your webhook URL
- Configure the webhook, for instance using
curl
:
curl -F "url=<YOUR_WEBHOOK_URL>" https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook
Verify the webhook setup:
curl https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getWebhookInfo
Testing the Integration
Find your bot on Telegram, and click the Start
button, a /start
message will
be sent. The bot should respond with a message, and the task runner column on
the right of your screen should show the incoming requests as they are being
served.
Now, let's try a few interactions:
- Using the search capability (if brave-search servlet is installed):
User: Find me a recipe for pizza
Bot: *searches and returns recipe information*
- Location services (if Google Maps servlet is installed):
User: Find directions to Central Park
Bot: *returns navigation information*
Extending The Bot Further
- Bots support
/commands
. You can easily improve the prompt to handle to them! - Add more servlets to enhance the capabilities of your bot: for instance
- Each task can be linked to different mcp.run profiles, allowing you to create multiple bots with distinct capabilities by using different combinations of servlets.
We look forward to see what you will build!
Support
If you encounter issues or need help:
- Check the webhook info using the getWebhookInfo URL
- Verify your servlets are properly installed
- Review the mcp.run task logs
- Visit our support page for additional assistance