Add your agent to Telegram
You can add your agent to Telegram as a bot
TelegramTransport Mixin (opens in a new tab) to connect your agent to a Telegram Bot.
A full working example is here (opens in a new tab).
You can copy and paste this agent into your
api.py file or use it as a reference.
A Mixin is just a way to add a bundle of functionality to your
AgentService. Mixins can include new API endpoints, async processing pipelines, and webhooks.
TelegramTransportto the static
USED_MIXIN_CLASSESlist in your AgentService
bot_tokenconfiguration parameter to your agent:
class ExampleTelegramAgent(AgentService): class ExampleTelegramAgent(Config): bot_token: str = Field(description="The secret token for your Telegram bot") @classmethod def config_cls(cls) -> Type[Config]: return ExampleTelegramAgent.ExampleTelegramAgent
TelegramTransportin your AgentService
self.add_mixin(TelegramTransport( self.client, config=TelegramTransportConfig(bot_token=self.config.bot_token), self, self.agent ))
To get a Telegram Bot Token:
- Contact the @BotFather (opens in a new tab) on Telegram
- Run the
- Name your bot
- Copy the bot token that you are given
Python SDK Version
Version 2.17.18 or later of the Steamship SDK is required for this feature.
To use this version, update your `requirements.txt` file and then run `pip install -r requirements.txt`
You do not need to deploy your agent to test the telegram connection -- you can test with Telegram directly from your local machine. Just follow these steps:
- Create a
config.jsonfile that contains the configuration of your AgentService. This will now include a required
bot_tokenfield that contains your Telegram bot token.
ship run local -c config.jsonto run a local copy of your Agent with the provided configuration.
An ngrok account is required to run
ship run local. You can connect your account by running:
ngrok config add-authtoken YOUR_NGROK_AUTH_TOKEN
It will automatically connect to Telegram using a public NGROK URL.
Then enter the telegram_token.
You can find instructions for how to generate a Telegram bot key from the field in the UI.