mautrix / whatsapp

A Matrix-WhatsApp puppeting bridge
https://maunium.net/go/mautrix-whatsapp
GNU Affero General Public License v3.0
1.25k stars 172 forks source link

Relaybot not working at all, "invites" and "management" vanishes from config.yml #572

Closed jacotec closed 1 year ago

jacotec commented 1 year ago

I'm suffering getting the relaybot working. Unfortunately there is not a really good documentation, the only one I found was this: https://mau.dev/mautrix/docs/-/blob/9ca3e82a4fba6e6e36e40fc1aca4c987835bb430/bridges/go/whatsapp/relaybot.md

However, the "relaybot:" config section seems to be renamed to "relay:" when I look into the sample config.yml. So my config looks like this:

    # Settings for relay mode
    relay:
        # Whether relay mode should be allowed. If allowed, `!wa set-relay` can be used to turn any
        # authenticated user into a relaybot for that chat.
        enabled: true
        # Should only admins be allowed to set themselves as relay users?
        admin_only: true
        management: !gwILrLJxxxxxxxSnue:mydomain.de
        invites: ['@user1:mydomain.de', '@user2@mydomain.de']
        # The formats to use when sending messages to WhatsApp via the relaybot.
        message_formats:
            m.text: "<b>{{ .Sender.Displayname }}</b>: {{ .Message }}"
            m.notice: "<b>{{ .Sender.Displayname }}</b>: {{ .Message }}"
            m.emote: "* <b>{{ .Sender.Displayname }}</b> {{ .Message }}"
            m.file: "<b>{{ .Sender.Displayname }}</b> sent a file"
            m.image: "<b>{{ .Sender.Displayname }}</b> sent an image"
            m.audio: "<b>{{ .Sender.Displayname }}</b> sent an audio file"
            m.video: "<b>{{ .Sender.Displayname }}</b> sent a video"
            m.location: "<b>{{ .Sender.Displayname }}</b> sent a location"

After restarting the bridge, my "management" and "invites" lines are simply magically deleted out of the config. Which leads to a non-working relay.

When I create a management room in my secret relay user, inviting user1, user2 and then the bridge bot, I can login my WA account as expected.

And now?

When I then write a WA message to the connected number from a different account, only my secret relay user gets an invitation to the portal. My two users who should handle the WA account do not get any invitation at all (due to the magically purged "invites" configuration). Also it's not clear to me how the two users can create a new conversation to a Whatsapp number using the "relay WA account".

tulir commented 1 year ago

The relay mode docs are at https://docs.mau.fi/bridges/general/relay-mode.html

The new system doesn't use a management room and doesn't support inviting extra users, such features can be implemented outside the bridge by having a bot run the relay account.

jacotec commented 1 year ago

@tulir What means "a bot run the relay account"?

I'm sorry, but the mentioned doc does not give me any idea at all

Try to see it from the user side, not from the side of the bridge dev ;-) You know how all this works, we do not. It looked perfectly clear with the "old version" of the bot - but I have no idea how this should work with the new version.

Where can I find this information?

hipur commented 1 year ago

I had the same problem as @jacotec. How I solved was making an account to login to the bridge (Bot) and running a Maubot instance on that account. It has two main functions:

Whenever that account (Bot) is invited to a room, it sends the relay commands and auto-invites other Matrix users to it, setting also the power levels for them.

To open a new WA chat I have a room with the Bot and the Matrix users, and binded a command to make the bot repeat the Matrix user message with the new chat command: User sends: !newchat phonenumber Bot replies with: !wa pm phonenumber A new room is created and the invite routine makes all relay procedures.

jacotec commented 1 year ago

@hipur Sounds good, but where do I get this "Maubot" doing this? I tried to do pretty much the same with Node Red, but unfortunately the Matrix plugin does not give me an event when the account is invited into a new room ... so I can't trigger the sequence here.

jacotec commented 1 year ago

@hipur Would you share your Maubot doing this with me?

wlongo commented 1 year ago

@hipur exactly like @jacotec, i am also interested in your implementation of the Maubot to do exactly what you described above. I solved this in a very cumbersome way before: I forked the mautrix-whatsapp and implemented some "new" features on the relay mode directly on the bridge. The problem with this, is that is old and not up-to-date with the mautrix-whatapp anymore. Since time is being rare these days, i decided to go to something like your approach that is much more clean and easy to maintain. Thank you !

hipur commented 1 year ago

Sure, here's the code I'm using. Since I always add the same two users to rooms, I've hardcoded their IDs...

import asyncio
from time import sleep
from typing import Dict
from maubot import Plugin, MessageEvent
from maubot.handlers import command, event
from mautrix.client.api.events import EventMethods
from mautrix.client.api.rooms import RoomMethods
from mautrix.types import EventType, MessageType, RoomID, Membership, StateEvent

class PukoBot(Plugin):

    @event.on(EventType.ROOM_HISTORY_VISIBILITY)
    async def invitebot(self, evt: StateEvent) -> None:
            await self.client.send_markdown(
                evt.room_id, "!wa set-relay", allow_html=True
            )
            sleep(0.8)
            await self.client.send_markdown(
                evt.room_id, "!wa set-pl 100", allow_html=True
            )
            sleep(0.8)
            await self.client.invite_user(room_id=evt.room_id, user_id="@user1")
            sleep(0.8)
            await self.client.invite_user(room_id=evt.room_id, user_id="@user2")