Skip to content

Instantly share code, notes, and snippets.

@udf
Last active December 3, 2021 01:28
Show Gist options
  • Save udf/331a567dee7c888eb36a1e8ae5edb85b to your computer and use it in GitHub Desktop.
Save udf/331a567dee7c888eb36a1e8ae5edb85b to your computer and use it in GitHub Desktop.
Hacky way to prevent new message handlers from firing while in conversation (_on_edit, _on_read, and _check_custom are left as an exercise for the reader)
import functools
from telethon import events
from telethon.tl.custom import Conversation
HANDLED_BY_CONV_ATTR = '_handled_by_conv'
@client.on(events.NewMessage)
async def on_msg(event):
if getattr(event.original_update, HANDLED_BY_CONV_ATTR, False):
raise events.StopPropagation
def attr_setter_wrapper(func):
@functools.wraps(func)
def wrapper(self, response):
func(self, response)
if self._incoming and self._incoming[-1] is response.message:
setattr(response.original_update, HANDLED_BY_CONV_ATTR, True)
return wrapper
Conversation._on_new_message = attr_setter_wrapper(Conversation._on_new_message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment