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

Error when changing Matrix WhatsApp displayname_template #2962

Closed
mr8tom opened this issue Oct 23, 2023 · 4 comments
Closed

Error when changing Matrix WhatsApp displayname_template #2962

mr8tom opened this issue Oct 23, 2023 · 4 comments
Labels
bug upstream This issue is related to an upstream project

Comments

@mr8tom
Copy link

mr8tom commented Oct 23, 2023

Describe the bug
I want to have the address book name from my contacts in WhatsApp. I'm the only user at my matrix server. Tried many versions, nothing seems to be correct.

To Reproduce

I tried to change the matrix_mautrix_whatsapp_configuration_yaml in the section displayname_template and always get different errors when running the Ansible playbook.

displayname_template: "{{or .FullName .BusinessName .Push
Name .JID}} (WA)"
-> when I run the Ansible script it says

msg: |-
    The task includes an option with an undefined variable. The
 error was: {{ matrix_mautrix_whatsapp_configuration_yaml | fro
m_yaml | combine(matrix_mautrix_whatsapp_configuration_extensio
n, recursive=True) }}: {{ matrix_mautrix_whatsapp_configuration
_extension_yaml | from_yaml if matrix_mautrix_whatsapp_configur
ation_extension_yaml | from_yaml is mapping else {} }}: bridge:
      delivery_receipts: true
      history_sync:
        backfill: true
      user_avatar_sync: true
      archive_tag: m.lowpriority
      displayname_template: "{{or .FullName .BusinessName .Push
Name .JID}} (WA)"
    : 'or' is undefined

Then I only used .FullName:
displayname_template: "{{.FullName}} -W"
and got this Error message:

ERROR! We were unable to read either as JSON nor YAML, these ar
e the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  did not find expected key

The error appears to be in '/work/inventory/host_vars/matrix.8t
om.de/vars.yml': line 53, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

#  displayname_template: "{{or .FullName .PushName .JID}} -W"
    displayname_template: "{{.FullName}} -W"
    ^ here
We could be wrong, but this one looks like it might be an issue
 with
missing quotes. Always quote template expression brackets when
they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

Then I tried
displayname_template: "{{ '{{.FullName}}' }}" and got also an Error:

ERROR! We were unable to read either as JSON nor YAML, these ar
e the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  did not find expected key

The error appears to be in '/work/inventory/host_vars/matrix.8t
om.de/vars.yml': line 54, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

#    displayname_template: "{{.FullName}} -W"
    displayname_template: "{{ '{{.FullName}}' }}"
    ^ here`ERROR! We were unable to read either as JSON nor YAML, these ar
e the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  did not find expected key

The error appears to be in '/work/inventory/host_vars/matrix.8t
om.de/vars.yml': line 54, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

#    displayname_template: "{{.FullName}} -W"
    displayname_template: "{{ '{{.FullName}}' }}"
    ^ here

Even if I try the standard from the example in Ansible WhatsApp (displayname_template: "{{ '{{if .BusinessName}}{{.BusinessN
ame}}{{else if .PushName}}{{.PushName}}{{else}}{{.JID}}{{end}}
(WA)' }}"),
it doesn't work:

ERROR! We were unable to read either as JSON nor YAML, these ar
e the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  did not find expected key

The error appears to be in '/work/inventory/host_vars/matrix.8t
om.de/vars.yml': line 56, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

#    ...
    displayname_template: "{{ '{{if .BusinessName}}{{.BusinessN
ame}}{{else if .PushName}}{{.PushName}}{{else}}{{.JID}}{{end}}
(WA)' }}"
    ^ here

Seems Syntax related but I have no clue 😓

Expected behavior
Run the Ansible playbook and give me address book names.

Matrix Server:

  • OS: debian 12
  • Architecture: amd64

Ansible:

  • containerized Ansible on debian 12
  • version: 2.13.6-r0-3 docker
@spantaleev
Copy link
Owner

You need to use raw/endraw statements around Jinja2 characters which have a special meaning (like {{ and }}).

Example: {% raw %}{{.PushName}}{{% endraw %}}

@mr8tom
Copy link
Author

mr8tom commented Oct 23, 2023

Thank you @spantaleev
There was a little quota @{{% endraw %}}

I changed mine to this and it works now:
displayname_template: {% raw %}{{.FullName}}{% endraw %}

Now I have the next question, how to format it with "if" statements? Because no I have contacts with an empty name f.E. in WhatsApp Groups.
I tried around a bit but that's kind of hard. Never heard of Jinia2, will read into it but maybe someone already got it? Thanks in advance.

displayname_template: "{{ '{{if .BusinessName}}{{.BusinessN ame}}{{else if .PushName}}{{.PushName}}{{else}}{{.JID}}{{end}} (WA)' }}"

@spantaleev
Copy link
Owner

Jinja is what we use for templating the YAML files. Using raw/endraw or the {{ '{{ stuff here }}' }} syntax allows Jinja to not choke on special characters.


Later on, it's a problem of configuring the displayname_template setting. This is not really related to this Ansible playbook and playbook should not be impeding you in any way.

You can try going to the mautrix-whatsapp repository or #whatsapp:maunium.net Matrix room for assistance with this.

Nevertheless, I'll try to point you in the right direction.

These if statements that you wish to use are for Golang's text/template templating system. It's because the mautrix-whatsapp bridge is written in Golang. For other bridges (written in other languages), the situation will be different.

@mr8tom
Copy link
Author

mr8tom commented Oct 24, 2023

Thank you @spantaleev
I issued the problem also at the GitHub mautrix WhatsApp:
mautrix/whatsapp#565

With the tips from here and from them I got following solution working:

displayname_template: "{{or .FullName .PushName .JID}} (WA)"

needs to enclosed by {% raw %} and {% endraw %} :

displayname_template: {% raw %}{{or .FullName .PushName .JID}} (WA){% endraw %}

@mr8tom mr8tom closed this as completed Oct 24, 2023
@luixxiul luixxiul added bug upstream This issue is related to an upstream project labels Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug upstream This issue is related to an upstream project
Projects
None yet
Development

No branches or pull requests

3 participants