2018-04-05 21:14:22 +03:00
|
|
|
import re
|
|
|
|
|
2022-01-28 13:34:16 +03:00
|
|
|
from .base import EventBuilder
|
2021-09-12 14:27:13 +03:00
|
|
|
from .._misc import utils
|
|
|
|
from .. import _tl
|
2021-09-13 21:37:29 +03:00
|
|
|
from ..types import _custom
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
|
2022-01-28 16:12:32 +03:00
|
|
|
class NewMessage(EventBuilder, _custom.Message):
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2022-01-28 13:34:16 +03:00
|
|
|
Represents the event of a new message. This event can be treated
|
|
|
|
to all effects as a `Message <telethon.tl.custom.message.Message>`,
|
|
|
|
so please **refer to its documentation** to know what you can do
|
|
|
|
with this event.
|
|
|
|
|
|
|
|
Members:
|
|
|
|
message (`Message <telethon.tl.custom.message.Message>`):
|
|
|
|
This is the only difference with the received
|
|
|
|
`Message <telethon.tl.custom.message.Message>`, and will
|
|
|
|
return the `telethon.tl.custom.message.Message` itself,
|
|
|
|
not the text.
|
|
|
|
|
|
|
|
See `Message <telethon.tl.custom.message.Message>` for
|
|
|
|
the rest of available members and methods.
|
|
|
|
|
|
|
|
pattern_match (`obj`):
|
|
|
|
The resulting object from calling the passed ``pattern`` function.
|
|
|
|
Here's an example using a string (defaults to regex match):
|
|
|
|
|
|
|
|
>>> from telethon import TelegramClient, events
|
|
|
|
>>> client = TelegramClient(...)
|
|
|
|
>>>
|
|
|
|
>>> @client.on(events.NewMessage(pattern=r'hi (\\w+)!'))
|
|
|
|
... async def handler(event):
|
|
|
|
... # In this case, the result is a ``Match`` object
|
|
|
|
... # since the `str` pattern was converted into
|
|
|
|
... # the ``re.compile(pattern).match`` function.
|
|
|
|
... print('Welcomed', event.pattern_match.group(1))
|
|
|
|
...
|
|
|
|
>>>
|
2020-02-20 12:18:26 +03:00
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
from telethon import events
|
|
|
|
|
|
|
|
@client.on(events.NewMessage(pattern='(?i)hello.+'))
|
|
|
|
async def handler(event):
|
|
|
|
# Respond whenever someone says "Hello" and something else
|
|
|
|
await event.reply('Hey!')
|
|
|
|
|
|
|
|
@client.on(events.NewMessage(outgoing=True, pattern='!ping'))
|
|
|
|
async def handler(event):
|
|
|
|
# Say "!pong" whenever you send "!ping", then delete both messages
|
|
|
|
m = await event.respond('!pong')
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
await client.delete_messages(event.chat_id, [event.id, m.id])
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2022-01-28 13:34:16 +03:00
|
|
|
def __init__(self, message):
|
|
|
|
self.__dict__['_init'] = False
|
|
|
|
super().__init__(chat_peer=message.peer_id,
|
|
|
|
msg_id=message.id, broadcast=bool(message.post))
|
2018-06-20 12:49:18 +03:00
|
|
|
|
2022-01-28 13:34:16 +03:00
|
|
|
self.pattern_match = None
|
|
|
|
self.message = message
|
2018-06-20 12:49:18 +03:00
|
|
|
|
2018-07-19 02:47:32 +03:00
|
|
|
@classmethod
|
2022-01-28 13:34:16 +03:00
|
|
|
def _build(cls, update, others, self_id, entities, client):
|
2018-04-05 21:14:22 +03:00
|
|
|
if isinstance(update,
|
2021-09-12 13:16:02 +03:00
|
|
|
(_tl.UpdateNewMessage, _tl.UpdateNewChannelMessage)):
|
|
|
|
if not isinstance(update.message, _tl.Message):
|
2018-04-05 21:14:22 +03:00
|
|
|
return # We don't care about MessageService's here
|
2021-12-11 15:31:38 +03:00
|
|
|
msg = update.message
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(update, _tl.UpdateShortMessage):
|
2021-12-11 15:31:38 +03:00
|
|
|
msg = _tl.Message(
|
2018-04-05 21:14:22 +03:00
|
|
|
out=update.out,
|
|
|
|
mentioned=update.mentioned,
|
|
|
|
media_unread=update.media_unread,
|
|
|
|
silent=update.silent,
|
|
|
|
id=update.id,
|
2021-09-12 13:16:02 +03:00
|
|
|
peer_id=_tl.PeerUser(update.user_id),
|
|
|
|
from_id=_tl.PeerUser(self_id if update.out else update.user_id),
|
2018-04-05 21:14:22 +03:00
|
|
|
message=update.message,
|
|
|
|
date=update.date,
|
|
|
|
fwd_from=update.fwd_from,
|
|
|
|
via_bot_id=update.via_bot_id,
|
2020-10-01 13:22:55 +03:00
|
|
|
reply_to=update.reply_to,
|
2021-02-23 21:42:09 +03:00
|
|
|
entities=update.entities,
|
|
|
|
ttl_period=update.ttl_period
|
2021-12-11 15:31:38 +03:00
|
|
|
)
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(update, _tl.UpdateShortChatMessage):
|
2021-12-11 15:31:38 +03:00
|
|
|
msg = _tl.Message(
|
2018-04-05 21:14:22 +03:00
|
|
|
out=update.out,
|
|
|
|
mentioned=update.mentioned,
|
|
|
|
media_unread=update.media_unread,
|
|
|
|
silent=update.silent,
|
|
|
|
id=update.id,
|
2021-09-12 13:16:02 +03:00
|
|
|
from_id=_tl.PeerUser(self_id if update.out else update.from_id),
|
|
|
|
peer_id=_tl.PeerChat(update.chat_id),
|
2018-04-05 21:14:22 +03:00
|
|
|
message=update.message,
|
|
|
|
date=update.date,
|
|
|
|
fwd_from=update.fwd_from,
|
|
|
|
via_bot_id=update.via_bot_id,
|
2020-10-01 13:22:55 +03:00
|
|
|
reply_to=update.reply_to,
|
2021-02-23 21:42:09 +03:00
|
|
|
entities=update.entities,
|
|
|
|
ttl_period=update.ttl_period
|
2021-12-11 15:31:38 +03:00
|
|
|
)
|
2018-04-05 21:14:22 +03:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2022-02-04 13:46:57 +03:00
|
|
|
return cls._new(client, msg, entities, None)
|