mautrix / telegram

A Matrix-Telegram hybrid puppeting/relaybot bridge
https://matrix.to/#/#telegram:maunium.net
GNU Affero General Public License v3.0
1.32k stars 174 forks source link

Bot tries to join wrong matrix channel on bridging #739

Open Geosearchef opened 2 years ago

Geosearchef commented 2 years ago

I'm trying to set up the relaybot using the bridge command. I obtained the channel ID from the telegram bot using /id.

When I use the !tg bridge [id] command in a matrix channel, I get an Unhandled error.

The following can be found in the log:

Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: [2022-01-14 11:46:27,324] [DEBUG@mau.mx] Received Matrix event $vxyXM2Mstj2Gf-4R9ZkK3UH-9gZpqsJkj6QZTmsa5eE from @...:... in !kyHdAwaKTsDLXSgBqw:......
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: [2022-01-14 11:46:27,400] [ERROR@mau.commands] Unhandled error while handling command bridge -????????? from @......:....... (ref: 1642160766)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: Traceback (most recent call last):
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/appservice/api/intent.py", line 336, in ensure_joined
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     await self.join_room(room_id, max_retries=0)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/appservice/api/intent.py", line 83, in wrapper
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     return await __method(*args, **kwargs)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/client/api/rooms.py", line 269, in join_room
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     content = await self.api.request(Method.POST, Path.join[room_id_or_alias],
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/api.py", line 259, in request
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     return await self._send(method, full_url, content, query_params, headers or {})
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/api.py", line 180, in _send
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     raise make_request_error(http_status=response.status,
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: mautrix.errors.request.MUnknown: No known servers
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: The above exception was the direct cause of the following exception:
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: Traceback (most recent call last):
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/bridge/commands/handler.py", line 409, in handle
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     await self._run_handler(handler, evt)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix_telegram/commands/handler.py", line 121, in _run_handler
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     return await handler(evt)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/bridge/commands/handler.py", line 296, in __call__
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     return await self._handler(evt)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix_telegram/commands/portal/bridge.py", line 76, in bridge
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     if not await user_has_power_level(portal.mxid, evt.az.intent, evt.sender, "unbridge"):
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix_telegram/commands/portal/util.py", line 68, in user_has_power_level
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     await intent.get_power_levels(room_id)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/appservice/api/intent.py", line 194, in get_power_levels
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     await self.ensure_joined(room_id)
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:   File "/usr/lib/python3.9/site-packages/mautrix/appservice/api/intent.py", line 358, in ensure_joined
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]:     raise IntentError(f"Failed to join room {room_id} as {self.mxid}") from e
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: mautrix.errors.base.IntentError: Failed to join room !xPxxbaQFhdzYvpwknA:matrix.bayern as @telegrambot:matrix.bayern
Jan 14 11:46:27 geosearchef-infrastructure1 matrix-mautrix-telegram[283768]: [2022-01-14 11:46:27,456] [DEBUG@mau.mx] IntentError('Failed to join room !xPxxbaQFhdzYvpwknA:....... as @telegrambot:.......')

As can be seen, the bridge attempts to join xPxxbaQFhdzYvpwknA when trying to figure out if I have permissions in this channel, even though the command was sent in kyHdAwaKTsDLXSgBqw.

The error is thrown in this line: https://github.com/mautrix/telegram/blob/master/mautrix_telegram/commands/portal/bridge.py#L57 My guess would be on the wrong room being obtained in https://github.com/mautrix/telegram/blob/master/mautrix_telegram/commands/portal/bridge.py#L50 .

It then propagates throgh https://github.com/mautrix/telegram/blob/8f61be76f97c554498792d79cf7131fc7e125a96/mautrix_telegram/commands/portal/util.py#L68, https://github.com/mautrix/python/blob/master/mautrix/appservice/api/intent.py#L261, https://github.com/mautrix/python/blob/00e6c242befea07bc888a450d520f3638c11bf75/mautrix/appservice/api/intent.py#L511 and throws an MUnknown likely due to the room not existing.

Any idea what could cause this? How can I modify the code for testing / workaround purposes? I'm running the bridge using the ansible playbook in a docker container, as soon as I modify the code, I'll need to restart the python process which causes the container to terminate and reset.

Guest0x0 commented 2 years ago

I've got the same issue. On my server it seems to be caused by mautrix-telegram holding some outdated portal information (I messed up the state of mautrix recently). Manually deleting mautrix's sqlite database and restart mautrix works for me, but I am not sure if this is a recommended/safe operation.

schafskopf commented 1 year ago

I had the same problem. My workaround was to delete the DB from the bridge and start from scratch. Any other solutions yet?