matrix-nio
matrix-nio copied to clipboard
message event callbacks not firing in E2E room
I am logging into my account using username and password credentials with the AsyncClient
.
I have installed libolm
and am using the latest matrix-nio[e2e]
installed via pip.
Then, I am subscribing for message events like:
client.add_event_callback(on_message, (nio.RoomMessageText,))
This works for messages in non-encrypted rooms, but in encrypted rooms the events never fire. I have tried logging into the account using Riot, and the messages are indeed there and visible to the bot user. However, when accessed through nio, the messages are not read.
I'm also experiencing this issue. I started an unencrypted chat with my bot, sent a message, which appeared properly, then enabled encryption, sent a message, and it was not received.
Here is the code for my bot (basically the tutorial code with some stuff removed and rearranged). https://github.com/wasv/matrix-bot/tree/82b7c8d016f2bf4406c9f24108b8a991941da81a
Here is the event that did work:
{
"type": "m.room.message",
"sender": "@me",
"content": {
"msgtype": "m.text",
"body": "Hello"
},
"origin_server_ts": 1592346468678,
"unsigned": {
"age": 5090,
"transaction_id": "m1592346468537.0"
},
"event_id": "$uUN17...",
"room_id": "!gNCZ..."
}
Here is the one that didn't:
{
"type": "m.room.encrypted",
"sender": "@me",
"content": {
"algorithm": "m.megolm.v1.aes-sha2",
"sender_key": "Gqhc...",
"ciphertext": "AwgAEo...",
"session_id": "PyQQU...",
"device_id": "ZNU..."
},
"origin_server_ts": 1592346603355,
"unsigned": {
"age": 62,
"transaction_id": "m1592346602863.1"
},
"event_id": "$zJ7u-...",
"room_id": "!gNCZ..."
}
Decrypted payload:
{
"type": "m.room.message",
"content": {
"msgtype": "m.text",
"body": "Ping"
}
}
Hey @wasv, I went to check out your code but it looks like the repo is private. I saw similar behaviour when writing the manual encryption tutorial and it ended up that sync-timing was critical in resolving the issue. If you can share your code that'd be handy to help y'all debug this.
Whoops. Forgot I had it private. It's public now.
I'm also experiencing this issue. I started an unencrypted chat with my bot, sent a message, which appeared properly, then enabled encryption, sent a message, and it was not received.
You'll need to call self.load_store()
in your login method when you restore your login here. This method is called automatically when you login. We should really add a method called restore_login(user_id, device_id, access_token)
which does the same.
We should really add a method called
restore_login(user_id, device_id, access_token)
which does the same.
I can fork and do a small PR for this.
I tried the suggested fix (using load_store
), but I am still not seeing events fire in E2E rooms.
Update: It does now work in new E2E rooms, but does not work in rooms that were joined earlier. Is there a way to resolve this without recreating the rooms?
Update: It does now work in new E2E rooms, but does not work in rooms that were joined earlier. Is there a way to resolve this without recreating the rooms?
Could you also post your project or at least a snippet? Depending on what your device might have been created after you joined so you won't be able to get to the old messages in the previously joined rooms. New messages should arrive no matter when you joined the room.
Here is my source: beak.zip
@xdrie I took a look at your source but I didn't see anything that stands out as particularly funky.
I tried running it but it never seemed to actually receive messages, whether in a new or old room or encrypted or not. I'm not sure if that's because I didn't configure something or what.
At the same time, another quick bot written in nio was able to see these messages. I think your posted code either isn't pulling events correctly, or I wired it up wrong when I tried it out.
can someone fix this?
Fix what exactly? The problems here were around events not getting decrypted. If the event isn't decrypted the type of the event won't be RoomMessageText
. Events might not get decrypted for multiple reasons and they will be emitted as a MegolmEvent
.
Is there any simple examples on how to handle encrypted messages??
Depends on what you exactly want/need. Handle how? Retry decryption? Send them? Set up nio to correctly support encryption? Something else?
I have a similar issue. When I use login() everything works fine, but when I try to restore login it stops getting new messages from encrypted rooms.
Additionally, when I call load_store() whether manually or using restore_login() I can't even send messages to old rooms (No such room with id .. found)