Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mrjohnson22 opened this issue Jul 3, 2020 · 8 comments · May be fixed by #512
Open

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

mrjohnson22 opened this issue Jul 3, 2020 · 8 comments · May be fixed by #512
Labels
enhancement New feature or request

Comments

@mrjohnson22
Copy link

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
Copy link
Contributor

olmari commented Jul 3, 2020

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
Copy link
Author

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 mrjohnson22 changed the title Allow using an existing Matrix room as a portal to a WhatsApp group chat Allow plumbing an existing Matrix room to a WhatsApp group chat Jul 3, 2020
@mrjohnson22
Copy link
Author

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

  • If the plumbed room is invite-only, each Whatsapp puppet user must be manually invited to the room.
  • If double-puppeting is disabled, then the above also applies to Whatsapp puppets for Matrix users logged into the bridge.
    • One idea for how to avoid these invite problems is to require the Whatsapp bot user to be in plumbed rooms, and have it automatically send any required invites to puppet users.
  • Posting a message from Matrix into a plumbed room crashed the bridge (though it could be due to a misconfiguration on my end):
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x860078]

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:
  username_template: whatsapp_{{.}}
  displayname_template: '{{if .Notify}}{{.Notify}}{{else}}{{.Jid}}{{end}} (WA)'
  community_template: 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: <b>{{ .Sender.Displayname }}</b> sent an audio file
      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.location: <b>{{ .Sender.Displayname }}</b> sent a location
      m.notice: '<b>{{ .Sender.Displayname }}</b>: {{ .Message }}'
      m.text: '<b>{{ .Sender.Displayname }}</b>: {{ .Message }}'
      m.video: <b>{{ .Sender.Displayname }}</b> 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

@tulir tulir added the enhancement New feature or request label Jul 17, 2020
@luxus
Copy link

luxus commented Jan 31, 2021

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
Copy link

ruifm commented Jan 31, 2021

@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
Copy link

Asklios commented Feb 6, 2022

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.

AndrewFerr added a commit to element-hq/mautrix-whatsapp that referenced this issue Jun 10, 2022
@AndrewFerr AndrewFerr linked a pull request Jun 10, 2022 that will close this issue
AndrewFerr added a commit to element-hq/mautrix-whatsapp that referenced this issue Jun 22, 2022
AndrewFerr added a commit to element-hq/mautrix-whatsapp that referenced this issue Jun 22, 2022
AndrewFerr added a commit to element-hq/mautrix-whatsapp that referenced this issue Jun 23, 2022
AndrewFerr added a commit to element-hq/mautrix-whatsapp that referenced this issue Jun 30, 2022
AndrewFerr added a commit to element-hq/mautrix-whatsapp that referenced this issue Jul 13, 2022
@Gredin67
Copy link

Is there some more work required to merge #512 ?

@wehlutyk
Copy link

wehlutyk commented Apr 23, 2023

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

8 participants