I’m building a chatbot for Telegram using the Bot Framework with .NET and I’m having trouble with event handling. I need to detect when users block my bot or when they start chatting for the first time. These events work perfectly when I test with Web Chat, but they don’t seem to work at all with Telegram.
I’m using Microsoft.Bot.Builder version 4.22.2 and none of these event handlers are getting called:
- OnMembersAddedAsync
- OnMembersRemovedAsync
- OnCommandActivityAsync
- OnMessageUpdateActivityAsync
- OnConversationUpdateActivityAsync
- OnEventActivityAsync
- OnEndOfConversationActivityAsync
- OnInstallationUpdateRemoveAsync
- OnUnrecognizedActivityTypeAsync
- OnInstallationUpdateAddAsync
- OnEventAsync
- OnTokenResponseEventAsync
Is this normal behavior for Telegram bots? Does the Bot Framework have limitations when working with Telegram that prevent these events from working? I haven’t been able to find clear documentation about what events are actually supported for different channels.
Yes, this situation is quite common when working with Telegram and the Bot Framework. The limitations stem from Telegram’s API, which doesn’t support many standard events that the Bot Framework provides. Specifically, you won’t receive webhooks for user blocks or changes in conversation state, meaning that methods like OnMembersRemovedAsync and OnInstallationUpdateRemoveAsync won’t be triggered.
I encountered similar issues while transitioning a bot from Web Chat to Telegram recently. Essentially, Telegram relies on basic message exchanges, so you’ll likely find yourself using OnMessageActivityAsync for most interactions. To handle first-time users, I had to implement a custom solution to track user IDs in a database, given that OnMembersAddedAsync doesn’t occur. Unfortunately, the Bot Framework documentation does not provide enough details on these channel-specific limitations, which adds to the troubleshooting difficulty.
The challenges you’re facing with Telegram and the Bot Framework are indeed frustrating. Telegram’s webhook system is limited to handling messages, inline queries, and callback queries, leaving out many of the membership and conversation events that are typically available on other platforms.
I’ve experienced similar issues where event handlers fail to trigger due to Telegram’s restrictive API. For instance, you won’t be notified of blocked users or changes in membership. So, for managing user blocks, it’s essential to implement error handling to capture specific error codes returned when trying to send messages to blocked users. As for recognizing first-time users, you’ll need to maintain your own user state – checking your database each time a user sends a message. It’s unfortunate that the documentation from Microsoft doesn’t clarify these channel-specific limitations, creating a disparity in expected functionality.
yeah, telegram’s pretty limited with events. had the same issue recently - ended up switching to polling instead of relying on bot framework events. the telegram api is honestly pretty basic and mostly just handles message interactions.