-
Notifications
You must be signed in to change notification settings - Fork 16
feat: ✨ trigger the bot by mentioning it #81
base: master
Are you sure you want to change the base?
Changes from 14 commits
33776ba
d6a3a34
e73f24b
2d63cbe
57147ec
9a2e924
809186e
c2414d3
25c8ce8
45599af
3a5d621
6b8a0d9
2e380d5
deecc36
cbd843d
708eb2b
a2bde24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Union, Optional | ||
|
||
class Match: | ||
""" | ||
Class with methods to filter events | ||
|
@@ -83,7 +85,27 @@ def __init__(self, room, event, bot, prefix="") -> None: | |
super().__init__(room, event, bot) | ||
self._prefix = prefix | ||
|
||
def command(self, command=None): | ||
"""Forms of identification""" | ||
self._own_user_id = room.own_user_id | ||
self._own_nio_user = self.room.users[self._own_user_id] | ||
self._own_disambiguated_name = self._own_nio_user.disambiguated_name | ||
self._own_display_name = self._own_nio_user.display_name | ||
self._own_display_name_colon = f"{self._own_display_name}:" | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
self._own_pill = f"<a href=\"https://matrix.to/#/{self._own_user_id}\">" | ||
|
||
self.mention() # Set self._mention_id_length | ||
self._body_without_prefix = self.event.body[len(self._prefix):] | ||
self._body_without_mention = self.event.body[self._mention_id_length:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why initialize these here while it's unknown whether prefix or mention is used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mention() sets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it may be a good idea to look at bot libraries for other networks to see how they handle matching. |
||
|
||
if self.mention(): | ||
body = self._body_without_mention | ||
elif self.prefix(): | ||
body = self._body_without_prefix | ||
else: | ||
body = self.event.body | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why use distinct member variables? they can't both apply at once There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you reword this? |
||
self._split_body = body.split() | ||
|
||
def command(self, command: Optional[str] = None) -> Union[bool, str]: | ||
""" | ||
Parameters | ||
---------- | ||
|
@@ -99,18 +121,19 @@ def command(self, command=None): | |
Returns the string after the prefix and before the first space if no arg is passed to this method. | ||
""" | ||
|
||
if self._prefix == self.event.body[0:len(self._prefix)]: | ||
body_without_prefix = self.event.body[len(self._prefix):] | ||
else: | ||
body_without_prefix = self.event.body | ||
|
||
if not body_without_prefix: | ||
return [] | ||
if not (self._body_without_prefix and self._body_without_mention): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
"""Body is empty after removing prefix or mention""" | ||
if command is None: | ||
return "" | ||
elif command: | ||
return False | ||
else: | ||
return True | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
|
||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
if command: | ||
return body_without_prefix.split()[0] == command | ||
if command is not None: | ||
return self._split_body[0] == command | ||
else: | ||
return body_without_prefix.split()[0] | ||
return self._split_body[0] | ||
|
||
def prefix(self): | ||
""" | ||
|
@@ -121,18 +144,35 @@ def prefix(self): | |
Returns True if the message begins with the prefix, and False otherwise. If there is no prefix specified during the creation of this MessageMatch object, then return True. | ||
""" | ||
|
||
return self.event.body.startswith(self._prefix) | ||
return self.event.body[self._mention_id_length:].startswith(self._prefix) | ||
|
||
def mention(self): | ||
""" | ||
|
||
Returns | ||
------- | ||
boolean | ||
Returns True if the message begins with the bot's username, MXID, or pill targeting the MXID, and False otherwise. | ||
""" | ||
|
||
for id in [self._own_disambiguated_name, self._own_display_name, self._own_user_id, self._own_display_name_colon]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will always match _own_display_name and never _own_display_name_colon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps |
||
if self.event.body.startswith(id): | ||
self._mention_id_length = len(id)+1 | ||
return True | ||
self._mention_id_length = 0 | ||
|
||
return False | ||
|
||
def args(self): | ||
""" | ||
|
||
Returns | ||
------- | ||
list | ||
Returns a list of strings that are the "words" of the message, except for the first "word", which would be the command. | ||
Returns a list of strings that are the "words" of the message, except for the first "word", which would be the prefix/mention + command. | ||
""" | ||
|
||
return self.event.body.split()[1:] | ||
return self._split_body[1:] | ||
|
||
def contains(self, string): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems to be required to reliably load
self.room.own_user_id
andself.room.users
which may be empty otherwise from testing. I hope there is a better way to do it than just syncing everything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does self.room.own_user_id follow the structure of @username:homeserver ? If so, it would not be neccesary to do anything with self.room.users to obtain it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.room
is aDict[str, MatrixUser]
.MatrixUser
containsdisplay_name
anddisambiguated_name
which we need formention()
matchesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this https://matrix-nio.readthedocs.io/en/latest/nio.html#nio.rooms.MatrixRoom.user_name is good enough instead? I don't think so as it does the same: if room members haven't been synced yet, it just fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we need the user_id, then whoami should solve that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 57-63 of api.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the user id not be stored in bot.async_client.user_id ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would. the issue is more about getting the displayname though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think doing a full sync is actually ok if we enable storage in this PR (store is needed for #79)
https://github.com/poljar/matrix-nio/blob/a4fb83fd515568e269646d2111dc68e17cc251c6/nio/client/async_client.py#L368-L380
then only the very first time would be a "big" sync
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is acceptable.