用Matrix和wechaty来聊微信

Matrix chat是一个很不错的聊天软件,它支持了多种聊天协议和聊天机器人。并且支持通过matrix-appservice-wechaty来支持微信聊天。
现在我来梳理一下matrix-appservice-wechaty的部署方法。

需要准备的东西

  1. 域名(假设你的域名是example.com)
  2. 服务器,最好国内的

配置流程

配置域名解析

Type Host Priority Weight Port Target
A matrix - - - matrix-server-IP
CNAME element - - - matrix.
CNAME dimension (*) - - - matrix.
CNAME jitsi (*) - - - matrix.
SRV _matrix-identity._tcp 10 0 443 matrix.

配置matrix chat

git clone https://github.com/spantaleev/matrix-docker-ansible-deploy.git
mkdir inventory/host_vars/example.com/
export MATRIX_REGISTRATION_ADMIN_SECRET=$(pwgen -s 64 1)
echo '
matrix_domain: example.com
matrix_ssl_lets_encrypt_support_email: ${email}
matrix_synapse_enable_registration: true
matrix_registration_admin_secret: "${MATRIX_REGISTRATION_ADMIN_SECRET}"
matrix_postgres_connection_password: 'synapse-password'
matrix_synapse_federation_enabled: true
matrix_ma1sd_enabled: true
' > inventory/host_vars/example.com/vars.yml
echo 'example.com ansible_host=example.com ansible_ssh_user=root' >> inventory/hosts
ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start

等一切部署完之后可以检测一下有没有问题

ansible-playbook -i inventory/hosts setup.yml --tags=self-check

配置matrix-appservice-wechaty

连接远程服务器

新建docker-compose.yml并填入如下内容

version: '2'
services:
  wechaty-gateway:
      image: wechaty/wechaty:next
      volumes:
        - /matrix/synapse/config:/data
      networks:
        - default
      environment:
        - WECHATY_PUPPET_SERVER_PORT=7777
        - WECHATY_TOKEN={{random_token}}
        - WECHATY_PUPPET=wechaty-puppet-padlocal
        - WECHATY_PUPPET_PADLOCAL_TOKEN={{padlocal_token}}
      ports:
        - 7777:7777
  matrix-appservice-wechaty:
      container_name: matrix-appservice-wechaty
      image: wechaty/matrix-appservice
      volumes:
        - /matrix/synapse/config:/data
      networks:
        - default
      environment:
        - WECHATY_PUPPET_SERVICE_TOKEN=puppet_{{random_token}}
        - WECHATY_PUPPET_SERVICE_ENDPOINT=example.com:7777
        - WECHATY_PUPPET=wechaty-puppet-service
#        - WECHATY_PUPPET=wechaty-puppet-puppeteer
        - WECHATY_LOG=silly
      command: ["--config", "/data/wechaty-config.yaml", "--file", "/data/wechaty-registration.yaml"]
      ports:
        - 8788:8788

padlocal_token需要申请
random_token是随机字符串,可以用uuid
如果没有padlocal_token可以使用wechaty-puppet-puppeteer,把内容改为

version: '2'
services:
  matrix-appservice-wechaty:
      container_name: matrix-appservice-wechaty
      image: wechaty/matrix-appservice
      volumes:
        - /matrix/synapse/config:/data
      networks:
        - default
      environment:
        - WECHATY_PUPPET=wechaty-puppet-puppeteer
        - WECHATY_LOG=silly
      command: ["--config", "/data/wechaty-config.yaml", "--file", "/data/wechaty-registration.yaml"]
      ports:
        - 8788:8788

然后新增文件/matrix/synapse/config/wechaty-config.yaml,并填入如下内容

domain: example.com
homeserverUrl: https://matrix.example.com
registration: /data/wechaty-registration.yaml

运行docker-compose run --rm matrix-appservice-wechaty --config /data/wechaty-config.yaml --url "http://example:8788" --generate-registration生成配置文件

然后编辑/matrix/synapse/config/homeserver.yaml
修改app_service_config_files那一行为app_service_config_files: ["/data/wechaty-registration.yaml"]

运行systemctl restart matrix-*重启matrix服务

注册并登录

  1. 打开https://example.com,然后注册账号
  2. 点击People右边的➕,然后输入@wechaty:example.com点击Go
  3. 在打开的聊天窗口,等出现This room has been registered as your bridge management/status room.
  4. 然后发送!login,如果提示You are not enable matrix-appservice-wechatyyet. Please talk to thewechaty bot to check you in. I had enabled it for you ;-) 就再发送一遍
  5. 扫描二维码登录

参考资料

  1. https://github.com/spantaleev/matrix-docker-ansible-deploy
  2. https://wechaty.js.org/2021/01/28/csharp-wechaty-for-padlocal-puppet-service/
  3. https://github.com/wechaty/wechaty-puppet-puppeteer