From 507cc20496bf4afbd1758ee03c4b04fe6f440535 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 14 Jan 2024 20:08:08 +0100 Subject: [PATCH] Successfully implemented matrix whatsapp bridge --- group_vars/all | 1 + roles/backup-docker-to-local/tasks/main.yml | 20 +------ roles/docker-matrix-compose/tasks/main.yml | 59 +++++++++++++------ .../templates/docker-compose.yml.j2 | 20 ++++--- ...-config.json.j2 => element.config.json.j2} | 0 ....yml.j2 => mautrix-whatsapp.config.yml.j2} | 30 ++++++---- ...r.yaml.j2 => synapse.homeserver.config.j2} | 5 +- .../{log.config.j2 => synapse.log.config.j2} | 0 roles/docker-matrix-compose/vars/main.yml | 11 +++- tasks/create-and-seed-database.yml | 13 ++++ tasks/seed-database-to-backup.yml | 18 ++++++ 11 files changed, 118 insertions(+), 59 deletions(-) rename roles/docker-matrix-compose/templates/{element-config.json.j2 => element.config.json.j2} (100%) rename roles/docker-matrix-compose/templates/{whatsapp-bridge-config.yml.j2 => mautrix-whatsapp.config.yml.j2} (96%) rename roles/docker-matrix-compose/templates/{homeserver.yaml.j2 => synapse.homeserver.config.j2} (94%) rename roles/docker-matrix-compose/templates/{log.config.j2 => synapse.log.config.j2} (100%) create mode 100644 tasks/create-and-seed-database.yml create mode 100644 tasks/seed-database-to-backup.yml diff --git a/group_vars/all b/group_vars/all index fc25ef86..c030f05f 100644 --- a/group_vars/all +++ b/group_vars/all @@ -170,6 +170,7 @@ mastodon_single_user_mode: false #### Matrix matrix_playbook_tags: "setup-all,start" # For the initial update use: install-all,ensure-matrix-users-created,start matrix_role: "compose" # Role to setup Matrix. Valid values: ansible, compose +matrix_admin_name: "admin" #### Mailu version_mailu: "2.0" diff --git a/roles/backup-docker-to-local/tasks/main.yml b/roles/backup-docker-to-local/tasks/main.yml index 8b12d387..117286de 100644 --- a/roles/backup-docker-to-local/tasks/main.yml +++ b/roles/backup-docker-to-local/tasks/main.yml @@ -46,24 +46,8 @@ state: absent when: mode_reset | bool and run_once_backup_docker_to_local is not defined -- name: Display all database variables - debug: - msg: | - database_instance: "{{ database_instance | default('undefined') }}" - database_name: "{{ database_name | default('undefined') }}" - database_username: "{{ database_username | default('undefined') }}" - database_password: "{{ database_password | default('undefined') }}" - when: mode_verbose | bool - -- name: seed database values - command: - cmd: "python database_entry_seeder.py databases.csv {{database_instance}} {{database_name}} {{database_username}} {{database_password}}" - chdir: "{{backup_docker_to_local_folder}}" - when: > - database_instance is defined and - database_name is defined and - database_username is defined and - database_password is defined +- name: "include seed-database-to-backup.yml" + include_tasks: seed-database-to-backup.yml - name: Set file permissions for databases.csv to be readable, writable, and executable by root only ansible.builtin.file: diff --git a/roles/docker-matrix-compose/tasks/main.yml b/roles/docker-matrix-compose/tasks/main.yml index d0e78271..c17d625c 100644 --- a/roles/docker-matrix-compose/tasks/main.yml +++ b/roles/docker-matrix-compose/tasks/main.yml @@ -14,33 +14,40 @@ domain: "{{element_domain}}" http_port: "{{element_http_port}}" -#- name: "create {{docker_compose_instance_directory}}mautrix_whatsapp" -# file: -# path: "{{docker_compose_instance_directory}}mautrix_whatsapp" -# state: directory -# mode: 0755 -# -#- name: add whatsapp-bridge-config.yml -# template: -# src: "whatsapp-bridge-config.yml.j2" -# dest: "{{docker_compose_instance_directory}}mautrix_whatsapp/config.yml" -# notify: docker compose project setup +- name: "create {{docker_compose_instance_directory}}mautrix_whatsapp" + file: + path: "{{docker_compose_instance_directory}}mautrix_whatsapp" + state: directory + mode: 0755 -- name: add element-config.json +- name: "include create-and-seed-database.yml" + include_tasks: create-and-seed-database.yml + vars: + database_password: "{{mautrix_whatsapp_bridge_database_password}}" + database_username: "{{mautrix_whatsapp_bridge_database_username}}" + database_name: "{{mautrix_whatsapp_bridge_database_name}}" + +- name: add mautrix-whatsapp configuration template: - src: "element-config.json.j2" + src: "mautrix-whatsapp.config.yml.j2" + dest: "{{docker_compose_instance_directory}}mautrix_whatsapp/config.yaml" + notify: docker compose project setup + +- name: add element configuration + template: + src: "element.config.json.j2" dest: "{{docker_compose_instance_directory}}element-config.json" notify: docker compose project setup -- name: add homeserver.yaml +- name: add synapse homeserver configuration template: - src: "homeserver.yaml.j2" + src: "synapse.homeserver.config.j2" dest: "{{docker_compose_instance_directory}}homeserver.yaml" notify: docker compose project setup -- name: add log.config +- name: add synapse log configuration template: - src: "log.config.j2" + src: "synapse.log.config.j2" dest: "{{docker_compose_instance_directory}}{{synapse_domain}}.log.config" notify: docker compose project setup @@ -54,3 +61,21 @@ src: "docker-compose.yml.j2" dest: "{{docker_compose_instance_directory}}docker-compose.yml" notify: docker compose project setup + +- name: flush docker service + meta: flush_handlers + +- name: create registration files + command: + cmd: docker compose exec -it mautrix-whatsapp /usr/bin/mautrix-whatsapp -g -c /data/config.yaml -r {{mautrix_registration_file}} + chdir: "{{docker_compose_instance_directory}}" + +- name: create registration files + command: + cmd: docker compose exec mautrix-whatsapp /usr/bin/mautrix-whatsapp -g -c /data/config.yaml -r {{ mautrix_registration_file }} + chdir: "{{ docker_compose_instance_directory }}" + +- name: set read and execute permissions for the registration file in the container + command: + cmd: docker compose exec mautrix-whatsapp chmod a+rx {{ mautrix_registration_file }} + chdir: "{{ docker_compose_instance_directory }}" diff --git a/roles/docker-matrix-compose/templates/docker-compose.yml.j2 b/roles/docker-matrix-compose/templates/docker-compose.yml.j2 index 8e0b9d88..550e8778 100644 --- a/roles/docker-matrix-compose/templates/docker-compose.yml.j2 +++ b/roles/docker-matrix-compose/templates/docker-compose.yml.j2 @@ -6,6 +6,7 @@ services: synapse: image: matrixdotorg/synapse:latest + container_name: mautrix-synapse restart: {{docker_restart_policy}} logging: driver: journald @@ -13,6 +14,7 @@ services: - synapse_data:/data - ./homeserver.yaml:/data/homeserver.yaml:ro - ./{{synapse_domain}}.log.config:/data/{{synapse_domain}}.log.config:ro + - registration_files:{{registration_file_folder}} environment: - SYNAPSE_SERVER_NAME={{synapse_domain}} - SYNAPSE_REPORT_STATS=no @@ -23,6 +25,7 @@ services: element: image: vectorim/element-web:latest + container_name: mautrix-element restart: {{docker_restart_policy}} volumes: - ./element-config.json:/app/config.json @@ -38,12 +41,14 @@ services: # volumes: # - telegram_bridge_data:/data - #mautrix-whatsapp: - # container_name: mautrix-whatsapp - # image: dock.mau.dev/mautrix/whatsapp:latest - # restart: {{docker_restart_policy}} - # volumes: - # - ./mautrix_whatsapp:/data + mautrix-whatsapp: + container_name: mautrix-whatsapp + image: dock.mau.dev/mautrix/whatsapp:latest + restart: {{docker_restart_policy}} + volumes: + - ./mautrix_whatsapp:/data + - registration_files:{{registration_file_folder}} +{% include 'templates/docker-container-networks.yml.j2' %} #mautrix-facebook: # container_name: mautrix-facebook @@ -62,7 +67,8 @@ services: {% include 'templates/docker-compose-volumes.yml.j2' %} synapse_data: #telegram_bridge_data: - #whatsapp_bridge_data: + whatsapp_bridge_data: + registration_files: #facebook_bridge_data: #instagram_bridge_data: diff --git a/roles/docker-matrix-compose/templates/element-config.json.j2 b/roles/docker-matrix-compose/templates/element.config.json.j2 similarity index 100% rename from roles/docker-matrix-compose/templates/element-config.json.j2 rename to roles/docker-matrix-compose/templates/element.config.json.j2 diff --git a/roles/docker-matrix-compose/templates/whatsapp-bridge-config.yml.j2 b/roles/docker-matrix-compose/templates/mautrix-whatsapp.config.yml.j2 similarity index 96% rename from roles/docker-matrix-compose/templates/whatsapp-bridge-config.yml.j2 rename to roles/docker-matrix-compose/templates/mautrix-whatsapp.config.yml.j2 index ddaf5e7a..4fdab768 100644 --- a/roles/docker-matrix-compose/templates/whatsapp-bridge-config.yml.j2 +++ b/roles/docker-matrix-compose/templates/mautrix-whatsapp.config.yml.j2 @@ -1,9 +1,9 @@ # Homeserver details. homeserver: # The address that this appservice can use to connect to the homeserver. - address: https://{{synapse_domain}} - # The domain of the homeserver (for MXIDs, etc). - domain: "{{synapse_domain}}" + address: http://synapse:8008 + # The domain of the homeserver (also known as server_name, used for MXIDs, etc). + domain: {{synapse_domain}} # What software is the homeserver running? # Standard Matrix homeservers like Synapse, Dendrite and Conduit should just use "standard" here. @@ -27,7 +27,7 @@ homeserver: # Changing these values requires regeneration of the registration. appservice: # The address that the homeserver can use to connect to this appservice. - address: http://localhost:29318 + address: http://mautrix-whatsapp:29318 # The hostname and port where this appservice should listen. hostname: 0.0.0.0 @@ -42,7 +42,7 @@ appservice: # https://github.com/mattn/go-sqlite3#connection-string # Postgres: Connection string. For example, postgres://user:password@host/database?sslmode=disable # To connect via Unix socket, use something like postgres:///dbname?host=/var/run/postgresql - uri: postgres://matrix:{{matrix_database_password}}@database/matrix?sslmode=disable + uri: postgres://{{mautrix_whatsapp_bridge_database_username}}:{{mautrix_whatsapp_bridge_database_password}}@{{database_host}}/{{mautrix_whatsapp_bridge_database_name}}?sslmode=disable # Maximum number of connections. Mostly relevant for Postgres. max_open_conns: 20 max_idle_conns: 2 @@ -70,8 +70,8 @@ appservice: async_transactions: false # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify. - as_token: "GvOrixRxN8G2dO1KhUgWKL8yhC0lgSBtbr819nntLV2ZcPlWayjWj14YIVKMJHmA" - hs_token: "oQbRcd1KO2ggrF9lu13fElKnp1Zf0GDU9fiTFSwQQjnD3VmcRnVlZqhMpXu3criI" + #as_token: "{{mautrix_whatsapp_bridge_as_token}}" + #hs_token: "{{mautrix_whatsapp_bridge_hs_token}}" # Segment-compatible analytics endpoint for tracking some events, like provisioning API login and encryption errors. analytics: @@ -100,6 +100,7 @@ whatsapp: # Bridge config bridge: +{% raw %} # Localpart template of MXIDs for WhatsApp users. # {{.}} is replaced with the phone number of the WhatsApp user. username_template: whatsapp_{{.}} @@ -111,6 +112,7 @@ bridge: # {{.FullName}} - full name from contact list # {{.FirstName}} - first name from contact list displayname_template: "{{or .BusinessName .PushName .JID}} (WA)" +{% endraw %} # Should the bridge create a space for each logged-in user and add bridged rooms to it? # Users who logged in before turning this on should run `!wa sync space` to create and fill the space for the first time. personal_filtering_spaces: false @@ -234,7 +236,7 @@ bridge: force_active_delivery_receipts: false # Servers to always allow double puppeting from double_puppet_server_map: - example.com: https://example.com + {{synapse_domain}}: https://{{synapse_domain}} # Allow using double puppeting from any server with a valid client .well-known file. double_puppet_allow_discovery: false # Shared secrets for https://github.com/devture/matrix-synapse-shared-secret-auth @@ -243,7 +245,7 @@ bridge: # instead of users having to find an access token and run `login-matrix` # manually. login_shared_secret_map: - example.com: foobar + {{synapse_domain}}: foobar # Whether to explicitly set the avatar and room name for private chat portal rooms. # If set to `default`, this will be enabled in encrypted rooms and disabled in unencrypted rooms. # If set to `always`, all DM rooms will have explicit names and avatars set. @@ -417,7 +419,7 @@ bridge: prefix: /_matrix/provision # Shared secret for authentication. If set to "generate", a random secret will be generated, # or if set to "disable", the provisioning API will be disabled. - shared_secret: kY8Ez6M80YBpvGtMz5R1Mb1c7deb8d9zxmlvqwTv0HGCKPVqcrTmHu90Wi04828z + shared_secret: {{mautrix_whatsapp_bridge_provisioning_shared_secret}} # Enable debug API at /debug with provisioning authentication. debug_endpoints: false @@ -432,8 +434,8 @@ bridge: # mxid - Specific user permissions: "*": relay - "example.com": user - "@admin:example.com": admin + "{{synapse_domain}}": user + "@{{matrix_admin_name}}:{{synapse_domain}}": admin # Settings for relay mode relay: @@ -444,6 +446,7 @@ bridge: admin_only: true # The formats to use when sending messages to WhatsApp via the relaybot. message_formats: +{% raw %} m.text: "{{ .Sender.Displayname }}: {{ .Message }}" m.notice: "{{ .Sender.Displayname }}: {{ .Message }}" m.emote: "* {{ .Sender.Displayname }} {{ .Message }}" @@ -452,10 +455,11 @@ bridge: m.audio: "{{ .Sender.Displayname }} sent an audio file" m.video: "{{ .Sender.Displayname }} sent a video" m.location: "{{ .Sender.Displayname }} sent a location" +{% endraw %} # Logging config. See https://github.com/tulir/zeroconfig for details. logging: min_level: debug writers: - type: stdout - format: pretty-colored + format: pretty-colored \ No newline at end of file diff --git a/roles/docker-matrix-compose/templates/homeserver.yaml.j2 b/roles/docker-matrix-compose/templates/synapse.homeserver.config.j2 similarity index 94% rename from roles/docker-matrix-compose/templates/homeserver.yaml.j2 rename to roles/docker-matrix-compose/templates/synapse.homeserver.config.j2 index 962348cc..a08d9d49 100644 --- a/roles/docker-matrix-compose/templates/homeserver.yaml.j2 +++ b/roles/docker-matrix-compose/templates/synapse.homeserver.config.j2 @@ -43,4 +43,7 @@ email: enable_notifs: true notif_for_new_users: false client_base_url: "{{domain_matrix_synapse}}" - validation_token_lifetime: 15m \ No newline at end of file + validation_token_lifetime: 15m + +app_service_config_files: + - "{{mautrix_registration_file}}" \ No newline at end of file diff --git a/roles/docker-matrix-compose/templates/log.config.j2 b/roles/docker-matrix-compose/templates/synapse.log.config.j2 similarity index 100% rename from roles/docker-matrix-compose/templates/log.config.j2 rename to roles/docker-matrix-compose/templates/synapse.log.config.j2 diff --git a/roles/docker-matrix-compose/vars/main.yml b/roles/docker-matrix-compose/vars/main.yml index 3b0d1d12..3fbbf358 100644 --- a/roles/docker-matrix-compose/vars/main.yml +++ b/roles/docker-matrix-compose/vars/main.yml @@ -1,4 +1,9 @@ --- -docker_compose_project_name: "matrix" -database_password: "{{matrix_database_password}}" -database_type: "postgres" \ No newline at end of file +docker_compose_project_name: "matrix" +database_password: "{{matrix_database_password}}" +database_type: "postgres" +registration_file_folder: "/data/registration/" +mautrix_whatsapp_bridge_database_name: "mautrix_whatsapp_bridge" +mautrix_whatsapp_bridge_database_username: "mautrix_whatsapp_bridge" +mautrix_registration_file: "{{registration_file_folder}}mautrix-whatsapp.registration.yaml" + diff --git a/tasks/create-and-seed-database.yml b/tasks/create-and-seed-database.yml new file mode 100644 index 00000000..aa02150e --- /dev/null +++ b/tasks/create-and-seed-database.yml @@ -0,0 +1,13 @@ +# The following parameters need to be based: +# +# - database_instance +# - database_name +# - database_username +# - database_password +- name: "create {{database_name}} database" + include_role: + name: docker-postgres + when: enable_central_database | bool + +- name: "include seed-database-to-backup.yml" + include_tasks: seed-database-to-backup.yml \ No newline at end of file diff --git a/tasks/seed-database-to-backup.yml b/tasks/seed-database-to-backup.yml new file mode 100644 index 00000000..736d7ec7 --- /dev/null +++ b/tasks/seed-database-to-backup.yml @@ -0,0 +1,18 @@ +- name: Display all database variables + debug: + msg: | + database_instance: "{{ database_instance | default('undefined') }}" + database_name: "{{ database_name | default('undefined') }}" + database_username: "{{ database_username | default('undefined') }}" + database_password: "{{ database_password | default('undefined') }}" + when: mode_verbose | bool + +- name: seed database values + command: + cmd: "python database_entry_seeder.py databases.csv {{database_instance}} {{database_name}} {{database_username}} {{database_password}}" + chdir: "{{backup_docker_to_local_folder}}" + when: > + database_instance is defined and + database_name is defined and + database_username is defined and + database_password is defined \ No newline at end of file