diff --git a/docs/configuring-playbook-bridge-mautrix-telegram.md b/docs/configuring-playbook-bridge-mautrix-telegram.md index 175a9b4b..0886c60d 100644 --- a/docs/configuring-playbook-bridge-mautrix-telegram.md +++ b/docs/configuring-playbook-bridge-mautrix-telegram.md @@ -2,7 +2,7 @@ The playbook can install and configure [mautrix-telegram](https://github.com/tulir/mautrix-telegram) for you. -See that project's [documentation](https://github.com/tulir/mautrix-telegram/wiki#usage) to learn what it does and why it might be useful to you. +See the project's [documentation](https://github.com/tulir/mautrix-telegram/wiki#usage) to learn what it does and why it might be useful to you. You'll need to obtain API keys from `https://my.telegram.org/apps` and then use the following playbook configuration: ``` @@ -10,3 +10,5 @@ matrix_mautrix_telegram_enabled: true matrix_mautrix_telegram_api_id: YOUR_TELEGRAM_APP_ID matrix_mautrix_telegram_api_hash: YOUR_TELEGRAM_API_HASH ``` + +You then need to start a chat with `@telegrambot:{{ hostname_identity }}` diff --git a/docs/configuring-playbook-bridge-mautrix-whatsapp.md b/docs/configuring-playbook-bridge-mautrix-whatsapp.md new file mode 100644 index 00000000..0d70043a --- /dev/null +++ b/docs/configuring-playbook-bridge-mautrix-whatsapp.md @@ -0,0 +1,12 @@ +# Setting up Mautrix Whatsapp + +The playbook can install and configure [mautrix-whatsapp](https://github.com/tulir/mautrix-whatsapp) for you. + +See the project's [documentation](https://github.com/tulir/mautrix-whatsapp/wiki) to learn what it does and why it might be useful to you. + +Use the following playbook configuration: +``` +matrix_mautrix_whatsapp_enabled: true +``` + +You then need to start a chat with `@whatsappbot:{{ hostname_identity }}` diff --git a/roles/matrix-server/defaults/main.yml b/roles/matrix-server/defaults/main.yml index 38d9b0f8..a45d6b95 100644 --- a/roles/matrix-server/defaults/main.yml +++ b/roles/matrix-server/defaults/main.yml @@ -137,6 +137,7 @@ matrix_coturn_base_path: "{{ matrix_base_data_path }}/coturn" matrix_coturn_config_path: "{{ matrix_coturn_base_path }}/turnserver.conf" matrix_scratchpad_dir: "{{ matrix_base_data_path }}/scratchpad" matrix_mautrix_telegram_base_path: "{{ matrix_base_data_path }}/mautrix-telegram" +matrix_mautrix_whatsapp_base_path: "{{ matrix_base_data_path }}/mautrix-whatsapp" matrix_static_files_base_path: "{{ matrix_base_data_path }}/static-files" matrix_docker_image_postgres_v9: "postgres:9.6.10-alpine" @@ -151,6 +152,7 @@ matrix_docker_image_coturn: "instrumentisto/coturn:4.5.0.8" matrix_docker_image_mailer: "panubo/postfix:latest" matrix_docker_image_mxisd: "kamax/mxisd:1.1.1" matrix_docker_image_mautrix_telegram: "tulir/mautrix-telegram:v0.3.0" +matrix_docker_image_mautrix_whatsapp: "tulir/mautrix-whatsapp:latest" # The Docker network that all services would be put into matrix_docker_network: "matrix" @@ -252,6 +254,9 @@ matrix_mautrix_telegram_api_hash: YOUR_TELEGRAM_API_HASH # Use an uuid so it's not easily discoverable matrix_mautrix_telegram_public_endpoint: "/{{ matrix_synapse_macaroon_secret_key | password_hash('sha512', 'telegram') | to_uuid }}" +# Matrix mautrix is a Matrix <-> Whatsapp bridge +# Enable whatsapp bridge +matrix_mautrix_whatsapp_enabled: false # By default, this playbook sets up its own nginx proxy server on port 80/443. # This is fine if you're dedicating the whole server to Matrix. diff --git a/roles/matrix-server/tasks/setup/setup_synapse_ext.yml b/roles/matrix-server/tasks/setup/setup_synapse_ext.yml index 6c6fddda..d202d94e 100644 --- a/roles/matrix-server/tasks/setup/setup_synapse_ext.yml +++ b/roles/matrix-server/tasks/setup/setup_synapse_ext.yml @@ -5,3 +5,5 @@ - include: tasks/setup/setup_synapse_ext_shared_secret_auth.yml - include: tasks/setup/setup_synapse_ext_mautrix_telegram.yml + +- include: tasks/setup/setup_synapse_ext_mautrix_whatsapp.yml diff --git a/roles/matrix-server/tasks/setup/setup_synapse_ext_mautrix_telegram.yml b/roles/matrix-server/tasks/setup/setup_synapse_ext_mautrix_telegram.yml index b4719274..c4daaa87 100644 --- a/roles/matrix-server/tasks/setup/setup_synapse_ext_mautrix_telegram.yml +++ b/roles/matrix-server/tasks/setup/setup_synapse_ext_mautrix_telegram.yml @@ -34,11 +34,11 @@ when: "matrix_mautrix_telegram_enabled" - stat: "path={{ matrix_mautrix_telegram_base_path }}/registration.yaml" - register: mautrix_registration_file + register: mautrix_telegram_registration_file - name: Generate matrix-mautrix-telegram registration.yaml if it doesn't exist shell: /usr/bin/docker run --rm --name matrix-mautrix-telegram-gen -v {{ matrix_mautrix_telegram_base_path }}:/data:z {{ matrix_docker_image_mautrix_telegram }} python3 -m mautrix_telegram -g -c /data/config.yaml -r /data/registration.yaml - when: "matrix_mautrix_telegram_enabled and mautrix_registration_file.stat.exists == False" + when: "matrix_mautrix_telegram_enabled and mautrix_telegram_registration_file.stat.exists == False" - set_fact: matrix_synapse_app_service_config_file_mautrix_telegram: '/app-registration/mautrix-telegram.yml' diff --git a/roles/matrix-server/tasks/setup/setup_synapse_ext_mautrix_whatsapp.yml b/roles/matrix-server/tasks/setup/setup_synapse_ext_mautrix_whatsapp.yml new file mode 100644 index 00000000..d323c891 --- /dev/null +++ b/roles/matrix-server/tasks/setup/setup_synapse_ext_mautrix_whatsapp.yml @@ -0,0 +1,68 @@ +--- + +- name: Ensure Mautrix Whatsapp image is pulled + docker_image: + name: "{{ matrix_docker_image_mautrix_whatsapp }}" + when: "matrix_mautrix_whatsapp_enabled" + +- name: Ensure Mautrix Whatsapp configuration path exists + file: + path: "{{ matrix_mautrix_whatsapp_base_path }}" + state: directory + mode: 0750 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_username }}" + when: "matrix_mautrix_whatsapp_enabled" + +- stat: "path={{ matrix_mautrix_whatsapp_base_path }}/config.yaml" + register: mautrix_config_file + +- name: Ensure Matrix Mautrix whatsapp config installed + template: + src: "{{ role_path }}/templates/mautrix-whatsapp/config.yaml.j2" + dest: "{{ matrix_mautrix_whatsapp_base_path }}/config.yaml" + mode: 0644 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_username }}" + when: "matrix_mautrix_whatsapp_enabled and mautrix_config_file.stat.exists == False" + +- name: Ensure matrix-mautrix-whatsapp.service installed + template: + src: "{{ role_path }}/templates/systemd/matrix-mautrix-whatsapp.service.j2" + dest: "/etc/systemd/system/matrix-mautrix-whatsapp.service" + mode: 0644 + when: "matrix_mautrix_whatsapp_enabled" + +- stat: "path={{ matrix_mautrix_whatsapp_base_path }}/registration.yaml" + register: mautrix_whatsapp_registration_file + +- name: Generate matrix-mautrix-whatsapp registration.yaml if it doesn't exist + shell: /usr/bin/docker run --rm --name matrix-mautrix-whatsapp-gen -v {{ matrix_mautrix_whatsapp_base_path }}:/data:z {{ matrix_docker_image_mautrix_whatsapp }} /usr/bin/mautrix-whatsapp -g -c /data/config.yaml -r /data/registration.yaml + when: "matrix_mautrix_whatsapp_enabled and mautrix_whatsapp_registration_file.stat.exists == False" + +- set_fact: + matrix_synapse_app_service_config_file_mautrix_whatsapp: '/app-registration/mautrix-whatsapp.yml' + +- set_fact: + matrix_synapse_container_additional_volumes: > + {{ matrix_synapse_container_additional_volumes }} + + + {{ [{'src': '{{ matrix_mautrix_whatsapp_base_path }}/registration.yaml', 'dst': '{{ matrix_synapse_app_service_config_file_mautrix_whatsapp }}', 'options': 'ro'}] }} + when: "matrix_mautrix_whatsapp_enabled" + +- set_fact: + matrix_synapse_app_service_config_files: > + {{ matrix_synapse_app_service_config_files }} + + + {{ ["{{ matrix_synapse_app_service_config_file_mautrix_whatsapp }}"] | to_nice_json }} + when: "matrix_mautrix_whatsapp_enabled" + +# +# Tasks related to getting rid of matrix-mautrix-whatsapp (if it was previously enabled) +# + +- name: Ensure matrix-mautrix-whatsapp.service doesn't exist + file: + path: "/etc/systemd/system/matrix-mautrix-whatsapp.service" + state: absent + when: "not matrix_mautrix_whatsapp_enabled" diff --git a/roles/matrix-server/tasks/start.yml b/roles/matrix-server/tasks/start.yml index 3158c0fb..4e5373c4 100644 --- a/roles/matrix-server/tasks/start.yml +++ b/roles/matrix-server/tasks/start.yml @@ -37,3 +37,7 @@ - name: Ensure matrix-mautrix-telegram autoruns and is restarted service: name=matrix-mautrix-telegram enabled=yes state=restarted daemon_reload=yes when: matrix_mautrix_telegram_enabled + +- name: Ensure matrix-mautrix-whatsapp autoruns and is restarted + service: name=matrix-mautrix-whatsapp enabled=yes state=restarted daemon_reload=yes + when: matrix_mautrix_whatsapp_enabled diff --git a/roles/matrix-server/templates/mautrix-whatsapp/config.yaml.j2 b/roles/matrix-server/templates/mautrix-whatsapp/config.yaml.j2 new file mode 100644 index 00000000..a29117cc --- /dev/null +++ b/roles/matrix-server/templates/mautrix-whatsapp/config.yaml.j2 @@ -0,0 +1,86 @@ +# Homeserver details. +homeserver: + # The address that this appservice can use to connect to the homeserver. + address: https://{{ hostname_matrix }} + # The domain of the homeserver (for MXIDs, etc). + domain: {{ hostname_identity }} + +# Application service host/registration related details. +# Changing these values requires regeneration of the registration. +appservice: + # The address that the homeserver can use to connect to this appservice. + address: http://matrix-mautrix-whatsapp:8080 + + # The hostname and port where this appservice should listen. + hostname: 0.0.0.0 + port: 8080 + + # Database config. + database: + # The database type. Only "sqlite3" is supported. + type: sqlite3 + # The database URI. Usually file name. https://github.com/mattn/go-sqlite3#connection-string + uri: mautrix-whatsapp.db + # Path to the Matrix room state store. + state_store_path: ./mx-state.json + + # The unique ID of this appservice. + id: whatsapp + # Appservice bot details. + bot: + # Username of the appservice bot. + username: whatsappbot + # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty + # to leave display name/avatar as-is. + displayname: WhatsApp bridge bot + avatar: remove + + # Authentication tokens for AS <-> HS communication. Autogenerated; do not modify. + as_token: "This value is generated when generating the registration" + hs_token: "This value is generated when generating the registration" + +# Bridge config. Currently unused. +bridge: + # {% raw %} + # protecting the go templates inside the raw section. + # Localpart template of MXIDs for WhatsApp users. + # {{.}} is replaced with the phone number of the WhatsApp user. + username_template: whatsapp_{{.}} + # Displayname template for WhatsApp users. + # {{.Notify}} - nickname set by the WhatsApp user + # {{.Jid}} - phone number (international format) + # The following variables are also available, but will cause problems on multi-user instances: + # {{.Name}} - display name from contact list + # {{.Short}} - short display name from contact list + displayname_template: "{{if .Notify}}{{.Notify}}{{else}}{{.Jid}}{{end}} (WA)" + # {% endraw %} + + # The prefix for commands. Only required in non-management rooms. + command_prefix: "!wa" + + # Permissions for using the bridge. + # Permitted values: + # user - Access to use the bridge to chat with a WhatsApp account. + # admin - User level and some additional administration tools + # Permitted keys: + # * - All Matrix users + # domain - All users on that homeserver + # mxid - Specific user + permissions: + '{{ hostname_identity }}': user + +# Logging config. +logging: + # The directory for log files. Will be created if not found. + directory: ./logs + # Available variables: .Date for the file date and .Index for different log files on the same day. + file_name_format: {% raw %}"{{.Date}}-{{.Index}}.log"{% endraw %} + # Date format for file names in the Go time format: https://golang.org/pkg/time/#pkg-constants + file_date_format: 2006-01-02 + # Log file permissions. + file_mode: 0600 + # Timestamp format for log entries in the Go time format. + timestamp_format: Jan _2, 2006 15:04:05 + # Minimum severity for log messages. + # Options: debug, info, warn, error, fatal + print_level: debug diff --git a/roles/matrix-server/templates/systemd/matrix-mautrix-whatsapp.service.j2 b/roles/matrix-server/templates/systemd/matrix-mautrix-whatsapp.service.j2 new file mode 100644 index 00000000..10a78d3f --- /dev/null +++ b/roles/matrix-server/templates/systemd/matrix-mautrix-whatsapp.service.j2 @@ -0,0 +1,24 @@ +[Unit] +Description=Matrix Mautrix Whatsapp server +After=docker.service +Requires=docker.service +Requires=matrix-synapse.service +After=matrix-synapse.service + +[Service] +Type=simple +ExecStartPre=-/usr/bin/docker kill matrix-mautrix-whatsapp +ExecStartPre=-/usr/bin/docker rm matrix-mautrix-whatsapp +ExecStart=/usr/bin/docker run --rm --name matrix-mautrix-whatsapp \ + --log-driver=none \ + -e "UID={{ matrix_user_uid }}" -e "GID={{ matrix_user_gid }}" \ + --network={{ matrix_docker_network }} \ + -v {{ matrix_mautrix_whatsapp_base_path }}:/data:z \ + {{ matrix_docker_image_mautrix_whatsapp }} +ExecStop=-/usr/bin/docker kill matrix-mautrix-whatsapp +ExecStop=-/usr/bin/docker rm matrix-mautrix-whatsapp +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target