mautrix / whatsapp

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

Allow plumbing an existing Matrix room to a WhatsApp group chat #202

Open mrjohnson22 opened 4 years ago

mrjohnson22 commented 4 years ago

Since relaybot support exists, it would be helpful if an existing Matrix room could be bridged to a WhatsApp group chat. That way, an existing discussion on Matrix can effectively invite WhatsApp users.

It seems like the only way to connect a Matrix room to a WA group is to let the bridge create the Matrix room (either manually with join/open, or automatically in response to a received WA message) and set it up as a portal room.

Would it be possible for the bridge to simply mark an existing Matrix room with the JID of an existing WA group, and use it as a portal to that WA group?

olmari commented 4 years ago

At the moment you can make the room as you do now and then change said room in database to match already existing one. This is how I did the odd few rooms we need WA-relay for. Otherwise excellent suggestion.

mrjohnson22 commented 4 years ago

Oh, great to hear that works.

Then maybe all that's needed are !wa bridge/unbridge <jid> <room_id> commands for acting as a nicer front-end for making that kind of database change.

mrjohnson22 commented 4 years ago

I tried out tweaking the database with UPDATE portal SET mxid='<room_id>' WHERE jid='<jid>', but hit the following roadblocks:

goroutine 213 [running]: github.com/Rhymen/go-whatsapp.(Conn).encryptBinaryMessage(0xc000252000, 0xd9e85b, 0x6, 0xc0007df680, 0xc64d00, 0xc000476d80, 0x7f673bf82e98, 0x8, 0x18, 0xc000476d80, ...) /home/mr_johnson22/go/pkg/mod/github.com/tulir/go-whatsapp@v0.3.4/write.go:152 +0x88 github.com/Rhymen/go-whatsapp.(Conn).writeBinary(0xc000252000, 0xd9e85b, 0x6, 0xc0007df680, 0xc64d00, 0xc000476d80, 0x8010, 0xc0004ea560, 0x14, 0x0, ...) /home/mr_johnson22/go/pkg/mod/github.com/tulir/go-whatsapp@v0.3.4/write.go:50 +0xe6 github.com/Rhymen/go-whatsapp.(Conn).sendProto(0xc000252000, 0xc00030a000, 0x0, 0xddc7c0, 0xc000847778) /home/mr_johnson22/go/pkg/mod/github.com/tulir/go-whatsapp@v0.3.4/message.go:127 +0x22e github.com/Rhymen/go-whatsapp.(Conn).SendRaw(0xc000252000, 0xc00030a000, 0xc00058e840) /home/mr_johnson22/go/pkg/mod/github.com/tulir/go-whatsapp@v0.3.4/message.go:29 +0x55 created by main.(*Portal).HandleMatrixMessage /home/mr_johnson22/git/mautrix-whatsapp/portal.go:1916 +0x29b


End-to-bridge encryption is disabled, if that makes a difference.
This happened on a test deployment with a minimally-altered config:

homeserver: address: http://localhost:8008 domain: localhost appservice: address: http://localhost:29318 hostname: 127.0.0.1 port: 29318 database: type: sqlite3 uri: mautrix-whatsapp.db max_open_conns: 20 max_idle_conns: 2 provisioning: prefix: /_matrix/provision/v1 shared_secret: disable id: whatsapp bot: username: whatsappbot displayname: WhatsApp bridge bot avatar: remove as_token: bvJQYnWzUKspJPjfdrpUyZl9nRdGfhe6OTpsiY4WxzzR2Aa2DBQAEYVEm7oLzLta hs_token: aJkTDO1nOIsZU7VVJM37KygweufBXvHUWxxihqE2JkY2OrKGFJSkLom3VfPTLP8C metrics: enabled: false listen: 127.0.0.1:8001 whatsapp: device_name: Mautrix-WhatsApp bridge Test short_name: mx-wa bridge: usernametemplate: whatsapp{{.}} displayname_template: '{{if .Notify}}{{.Notify}}{{else}}{{.Jid}}{{end}} (WA)' communitytemplate: whatsapp{{.Localpart}}={{.Server}} connection_timeout: 20 fetch_message_on_timeout: false delivery_receipts: true login_qr_regen_count: 2 max_connection_attempts: 3 connection_retry_delay: -1 report_connection_retry: true chat_list_wait: 30 portal_sync_wait: 600 call_notices: start: true end: true initial_chat_sync_count: 0 initial_history_fill_count: 0 initial_history_disable_notifications: false recovery_chat_sync_count: -1 recovery_history_backfill: false sync_max_chat_age: 259200 sync_with_custom_puppets: true login_shared_secret: "" invite_own_puppet_for_backfilling: true private_chat_portal_meta: false resend_bridge_info: false whatsapp_thumbnail: false allow_user_invite: true command_prefix: '!wa' encryption: allow: false default: false permissions: '': 5 '@xj:localhost': 100 localhost: 10 relaybot: enabled: true management: "!yJHNDgVSRDEsTOrflF:localhost" invites: [] message_formats: m.audio: {{ .Sender.Displayname }} sent an audio file m.emote: ' {{ .Sender.Displayname }} {{ .Message }}' m.file: {{ .Sender.Displayname }} sent a file m.image: {{ .Sender.Displayname }} sent an image m.location: {{ .Sender.Displayname }} sent a location m.notice: '{{ .Sender.Displayname }}: {{ .Message }}' m.text: '{{ .Sender.Displayname }}: {{ .Message }}' m.video: {{ .Sender.Displayname }} sent a video logging: directory: ./logs file_name_format: '{{.Date}}-{{.Index}}.log' file_date_format: "2006-01-02" file_mode: 384 timestamp_format: Jan _2, 2006 15:04:05 print_level: debug

luxus commented 3 years ago

I tried to hack the database and did UPDATE portal SET mxid='<room_id>' WHERE jid='<jid>' where jid is the existing WA room and room_id is the new room I want to have WhatsApp bridged to. but WhatsApp bot didn't join the room and messages are not synchronized

ruifm commented 3 years ago

@luxus it worked for me but if the existing matrix room is encrypted, you need to enable encryption on the bridge bot.

It also goes without saying that you need to enable the relaybot first.

Asklios commented 2 years ago

The temporary solution which we use and which works:

There is already a Whatsapp group with an invitation link (e.g. https://chat.whatsapp.com/...) and the matrix room (e.g. !existingroom:example.com) to be bridged to. The encryption of the bridge (E2B) is activated.

  1. create a portal over the management room: join https://chat.whatsapp.com/...
  2. check which id the new matrix room has (e.g. !newroom:example.com)
  3. invite the bridgebot to the existing room (!existingroom:example.com) and give it invite rights
  4. update the database: UPDATE portal SET mxid = '!existingroom:example.com' WHERE mxid = '!newroom:example.com'.
  5. if necessary !wa set-relay in !existingroom:example.com. !newroom:example.com can be deleted.
Gredin67 commented 1 year ago

Is there some more work required to merge #512 ?

wehlutyk commented 1 year ago

Dear all,

I've been setting up my own whatsappbot bridge to make sure I could try out the proposal by Asklios, and indeed it works wonderfully! (Unless I remove the original Matrix channel created by WhatsApp, in which case the connection to the WhatsApp channel becomes lost.)

Is there any plan for merging this idea into the main code so we can connect a WhatsApp channel to a Matrix channel of which we are admin? (This opens the connection of other bridges, e.g. Telegram and WhatsApp both together.)

If there is something that is know to be done, I could try it out maybe. (Actually this seems what is implemented by AndrewFerr.) Thanks a lot again!!