Skip to content

Conversation

@alexmcgil
Copy link

This PR adds comprehensive streaming support and message handling improvements to the Telegram channel integration. The changes include message streaming with debouncing, message aggregation for
handling rapid user inputs, and enhanced safety for API calls.

Key Features

Streaming Support

  • New TelegramStreamProcessor for handling streamed text responses with configurable debouncing
  • MessageDebouncer to batch rapid message updates and reduce API calls
  • TelegramStreamProcessorFactory for custom streaming behavior
  • Support for StreamReactions interface with say(Stream) method
  • Automatic message splitting at optimal points for long messages

Message Aggregation

  • New UserMessageAggregator to combine consecutive messages from the same user
  • Configurable wait time before processing combined messages
  • Helps handle cases where Telegram splits long messages or users send multiple short messages

Enhanced Safety

  • Automatic fallback when message sending fails with parse mode (retries without parse mode)
  • Helper functions: downloadFileByUrl, findOptimalSplitPoint
  • Improved error handling in message sending and editing

API Updates

  • Migrated to latest Telegram Bot API version (kotlin-telegram-bot 6.2.0)
  • Updated dispatcher handlers to use suspend functions
  • Replaced Gson with Jackson for JSON serialization
  • Added support for new Telegram API parameters: protectContent, allowSendingWithoutReply, messageThreadId

Core Integration

  • Added WithDispatcher interface support to BotEngine
  • Uses coroutine dispatcher from BotApi if available
  • Factory methods updated for JAICP compatibility with dispatcher support

Technical Changes

Dependencies (channels/telegram/build.gradle.kts)

  • Updated kotlin-telegram-bot to 6.2.0
  • Added Jackson dependencies (replacing Gson)
  • Added test dependencies: kotlinx-coroutines-test, JUnit 5, MockK

New Files

  • TelegramStreamProcessor.kt - core streaming implementation
  • TelegramStreamProcessorFactory.kt - factory interface for custom processors
  • UserMessageAggregator.kt - message aggregation logic
  • MessageDebouncer.kt - debouncing mechanism
  • MessageState.kt - streaming state management
  • helpers/downloadFileByUrl.kt - file download utility
  • helpers/findOptimalSplitPoint.kt - message splitting logic
  • TelegramStreamProcessorTest.kt - comprehensive test suite

Breaking Changes

  • TelegramChannel constructor now requires requestDispatcher: CoroutineDispatcher parameter
  • Dispatcher handlers changed from blocking to suspend functions
  • JSON serialization changed from Gson to Jackson

Testing

  • New test suite for TelegramStreamProcessor with 8 test cases covering:
    • Basic streaming functionality
    • Message splitting
    • Debouncing behavior
    • Error handling
    • Empty stream handling

@alexmcgil alexmcgil requested a review from morfeusys October 16, 2025 14:16
@alexmcgil alexmcgil self-assigned this Oct 16, 2025
@vercel
Copy link

vercel bot commented Oct 16, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
jaicf-kotlin Ready Ready Preview Comment Nov 20, 2025 9:17am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants