Skip to main content
Example code is available on GitHub
Any 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:
Let’s break down what this code is doing…
This is how you define a message handler in 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

This function centralizes all the logic for determining whether the bot should respond to a message. It handles different chat types:
  • 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

This helper removes the bot’s mention from the message content, leaving just the actual user input.

Peer ID Generation

This creates a unique peer identifier for each Telegram user by prefixing their Telegram user ID.

LLM Integration

This function handles the LLM interaction. It uses Honcho’s built-in to_openai() method to automatically convert the session context into the format expected by OpenAI’s chat completions API.

Message Sending

This function handles sending messages to Telegram, automatically splitting long responses into multiple messages to stay within Telegram’s 4096 character limit. It also includes a typing indicator to show the bot is processing.

Honcho Integration

The new Honcho peer/session API makes integration much simpler:
Here we create a peer object for the user and a session object using the Telegram chat ID. This automatically handles user and session management across both private chats and group conversations.
After generating the response, we save both the user’s input and the bot’s response to the session using the 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:
You can also add a /start command for user onboarding:

Setup and Configuration

The bot requires several environment variables and setup:
  • honcho_client: The main Honcho client
  • assistant: A peer representing the bot/assistant
  • openai: 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.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
This approach provides a clean, maintainable structure for building Telegram bots with conversational memory and context management across both private conversations and group chats.