Example code is available on GitHubAny application interface that defines logic based on events and supports special commands can work easily with Honcho. Here’s how to use Honcho with Telegram as an interface. If you’re not familiar with Telegram bot development, the python-telegram-bot docs would be a good place to start.
Message Handling
Most Telegram bots have async functions that handle incoming messages. We can use Honcho to store messages by user and session based on the chat context. Take the following function definition for example:python-telegram-bot
that processes incoming messages. We use a helper function validate_message()
to check if the message should be processed.
Helper Functions
The code uses several helper functions to keep the main logic clean and readable. Let’s examine each one:Message Validation
- Private chats: Always respond
- Group chats: Only respond when mentioned or when replying to the bot’s messages
- Bot prevention: Never respond to the bot’s own messages
Message Sanitization
Peer ID Generation
LLM Integration
to_openai()
method to automatically convert the session context into the format expected by OpenAI’s chat completions API.
Message Sending
Honcho Integration
The new Honcho peer/session API makes integration much simpler:add_messages()
method. The peer.message()
creates a message from the user, while assistant.message()
creates a message from the assistant.
Commands
Telegram bots support slash commands natively. Here’s how to implement the/dialectic
command using Honcho’s dialectic feature:
/start
command for user onboarding:
Setup and Configuration
The bot requires several environment variables and setup:honcho_client
: The main Honcho clientassistant
: A peer representing the bot/assistantopenai
: OpenAI client configured to use OpenRouter
Application Setup
Register your handlers with the Telegram application:Environment Variables
Your bot needs these environment variables:Chat Types and Behavior
The bot handles different Telegram chat types intelligently:Private Chats
- Behavior: Responds to all messages
- Session ID: Uses the private chat ID
- Memory: Maintains conversation history per user
Group Chats
- Behavior: Only responds when mentioned or replied to
- Session ID: Uses the group chat ID (shared across all members)
- Memory: Maintains group conversation context
Recap
The new Honcho peer/session API makes Telegram bot integration much simpler and more intuitive. Key patterns we learned:- Peer/Session Model: Users are represented as peers, conversations as sessions
- Chat Type Handling: Different validation logic for private vs group chats
- Automatic Context Management:
session.get_context().to_openai()
automatically formats chat history - Message Storage:
session.add_messages()
stores both user and assistant messages - Dialectic Queries:
peer.chat()
enables querying conversation history - Command System: Native Telegram command support with
/start
and/dialectic
- Message Splitting: Automatic handling of Telegram’s character limits
- Helper Functions: Clean code organization with focused helper functions