Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

feat: ✨ trigger the bot by mentioning it #81

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

HarHarLinks
Copy link
Contributor

@HarHarLinks HarHarLinks commented Nov 17, 2021

example:

    match = botlib.MessageMatch(room, message, bot, conf.prefix)

    if match.is_not_from_this_bot() and (match.prefix() or match.mention()):
        if match.command('help') or match.command('h') or (match.mention() and match.command('?')):
            me = await bot.async_client.get_displayname()
            me = me.displayname
            help = f"use `!help {me}` or `{me}: help` to display detailed help."
            if match.mention() or (len(match.args()) > 0 and match.args()[0].lower() == me.lower()):
                help += "\n"
                help += f"you can interact with me by sending a message prefixed with `{conf.prefix}`, my name (`{me}`), or my full matrix ID.\n"
            await bot.api.send_markdown_message(room.room_id, help, 'm.notice')

image

works with pills, displayname, disambiguated disaplyname, full matrix ID.

However as-is, it needs a full sync to be able to resolve this (users can be empty otherwise):
https://github.com/KrazyKirby99999/simple-matrix-bot-lib/blob/33776bad9855c4337557d3badfe3af74e9c9af1a/simplematrixbotlib/match.py#L85

I don't think that's optimal since a full sync on startup can take long when the account/room reached a certain lifetime. Is it possible to sync only the members list of the required room on demand?

todo

  • full sync?
  • doc
  • example on top of doc?
  • tests

@ghost
Copy link

ghost commented Nov 25, 2021

I don't think that's optimal since a full sync on startup can take long when the account/room reached a certain lifetime. Is it possible to sync only the members list of the required room on demand?

Perhaps create a new issue for this?

@ghost
Copy link

ghost commented Nov 25, 2021

Fixed messagematch.mention bug - e73f24b

"body" is now a tuple instead of a list
@ghost ghost force-pushed the call-by-mention branch from f5ec850 to e73f24b Compare November 25, 2021 00:49
@ghost
Copy link

ghost commented Nov 25, 2021

Accidentally included a change undescribed by commit message, fixed.

@ghost
Copy link

ghost commented Nov 25, 2021

2d63cbe should improve readability. Please test if this broke anything.

Copy link
Contributor Author

@HarHarLinks HarHarLinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introduces an exception, needs to be changed

simplematrixbotlib/match.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@HarHarLinks HarHarLinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smart idea using a loop, somehow I missed that.
however, some changes required

simplematrixbotlib/match.py Outdated Show resolved Hide resolved
simplematrixbotlib/match.py Outdated Show resolved Hide resolved
simplematrixbotlib/match.py Outdated Show resolved Hide resolved
@HarHarLinks
Copy link
Contributor Author

I don't think that's optimal since a full sync on startup can take long when the account/room reached a certain lifetime. Is it possible to sync only the members list of the required room on demand?

Perhaps create a new issue for this?

The trouble is that this is required for this PR, hence it's included.

@@ -39,7 +39,7 @@ async def main(self):


resp = await self.async_client.sync(timeout=65536,
full_state=False) #Ignore prior messages
full_state=True) #Ignore prior messages
Copy link
Contributor Author

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 and self.room.users which may be empty otherwise from testing. I hope there is a better way to do it than just syncing everything.

Copy link

@ghost ghost Nov 25, 2021

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.room is a Dict[str, MatrixUser]. MatrixUser contains display_name and disambiguated_name which we need for mention() matches

Copy link
Contributor Author

@HarHarLinks HarHarLinks Nov 25, 2021

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.

Copy link

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.

Copy link

@ghost ghost Nov 25, 2021

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

async with aiohttp.ClientSession() as session:
            async with session.get(f'{self.creds.homeserver}/_matrix/client/r0/account/whoami?access_token={self.creds.access_token}') as response:
                device_id = ast.literal_eval((await response.text()).replace(":false,", ":\"false\","))['device_id']
                user_id = ast.literal_eval((await response.text()).replace(":false,", ":\"false\","))['user_id']
            
            self.async_client.device_id, self.creds.device_id = device_id, device_id
            self.async_client.user_id, self.creds.user_id = user_id, user_id

Copy link

@ghost ghost Nov 25, 2021

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 ?

Copy link
Contributor Author

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

Copy link
Contributor Author

@HarHarLinks HarHarLinks Nov 28, 2021

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is acceptable.

@ghost
Copy link

ghost commented Nov 25, 2021

57147ec looks good.

happens when calling args before command
HarHarLinks and others added 3 commits November 26, 2021 15:39
even when no prefix is given
Refactor MessageMatch class to use _body_without_mention and _split_body. No longer checks formatted_body for mention.
Fix messagematch tests. match9 and match10 in test_mention are disabled, but will need to be enabled to test switching between body and formatted_body
@@ -149,11 +155,12 @@ def mention(self):
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]:
for id in [self._own_disambiguated_name, self._own_display_name, self._own_user_id, self._own_display_name_colon]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps _own_display_name_colon could be removed.


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:]
Copy link
Contributor Author

@HarHarLinks HarHarLinks Nov 30, 2021

Choose a reason for hiding this comment

The 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?
why create member variables that are only ever used 2 lines later?
mention() is executed twice unnecessarily

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention() sets ._mention_id_length, which is neccesary for calculating ._body_without_mention

Copy link

@ghost ghost Dec 28, 2021

Choose a reason for hiding this comment

The 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.

elif self.prefix():
body = self._body_without_prefix
else:
body = self.event.body
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use distinct member variables? they can't both apply at once

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reword this?


if not body_without_prefix:
return []
if not (self._body_without_prefix and self._body_without_mention):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use len(self._split_body) instead

@HarHarLinks HarHarLinks changed the title add bot trigger: mention feature: ✨ trigger the bot by mentioning it Dec 1, 2021
@HarHarLinks HarHarLinks changed the title feature: ✨ trigger the bot by mentioning it feat: ✨ trigger the bot by mentioning it Dec 1, 2021
@ghost ghost force-pushed the master branch 2 times, most recently from a711bef to e95148d Compare December 29, 2021 17:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant