Extracting Voicemails from email

Home Assistant Automation

Having given up my landline and moved to a voip system. I wanted to grab the information about missed calls and voicemails to Home assistant to send local messages and store the files into the media folder.

Step 1

Reading emails from GMail is quite tricky so I ended up setting up a Zoho account and setting filters in GMail to forward the relevant emails. The reason for this is Zoho allow you to generate Web Hooks when emails arrive, so I was able to set up a simple Web Hook automation triggered when mail arrived.

See dev-platform/webhook at Zoho for instructions.

Step 2

Having extracted the link to the file I set up the folder integration to download to a folder in media. I also needed to add the folder to the allowed list:

homeassistant:
  allowlist_external_dirs: 
    - /media/
    - /media/downloads
downloader:
  download_dir: /media/downloads

Step 3

I have used variables defined in the automation to extract the information I need from the Json sent in the web hook which then keeps the rest of the automation very clean.

alias: Voicemail
description: ""
trigger:
  - platform: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
    local_only: false
    webhook_id: voicemail-has-arrived
condition: []
action:
  - service: script.messages_update
    data:
      action: create_message
      message: "{{subject}}"
  - if:
      - condition: template
        value_template: "{{type == 'missed_call'}}"
    then:
      - service: notify.mobile_app_jane_s_phone
        data:
          message: "{{subject}}"
          data:
            actions:
              - action: URI
                title: Call Number
                uri: >-
                  intent:tel:{{phone_no}}///#Intent;action=android.intent.action.DIAL;end
      - service: notify.mobile_app_stephen_phone
        data:
          message: "{{subject}}"
          data:
            actions:
              - action: URI
                title: Call Number
                uri: >-
                  intent:tel:{{phone_no}}///#Intent;action=android.intent.action.DIAL;end
      - service: script.messages_update
        data:
          action: create_message
          message: "{{subject}}"
      - stop: Missed call - message only
  - service: notify.mobile_app_jane_s_phone
    data:
      data:
        actions:
          - action: URI
            title: View Voicemail
            uri: "{{filename}}"
          - action: URI
            title: Call Number
            uri: >-
              intent:tel:{{phone_no}}///#Intent;action=android.intent.action.DIAL;end            
      message: "{{subject}}"
    enabled: true
  - service: notify.mobile_app_stephen_phone
    data:
      data:
        actions:
          - action: URI
            title: View Voicemail
            uri: "{{filename}}"
          - action: URI
            title: Call Number
            uri: >-
              intent:tel:{{phone_no}}///#Intent;action=android.intent.action.DIAL;end  
      message: "{{subject}}"
    enabled: true
  - service: downloader.download_file
    data:
      url: "{{filename}}"
    enabled: true
variables:
  filename: |-
    {% set list = trigger.json.summary |
              regex_findall('(https\:\/\/secure.live.sipgate.co.uk\/download.*voice.*mp3)','')  %}              
    {{list[0] or 'NA'}}  
  matchtable: |-
    {% set list = trigger.json.summary |
              regex_findall('(https\:\/\/secure.live.sipgate.co.uk\/download.*voice.*mp3)','')  %}              
    {{list}} 
  subject: >-
    {{trigger.json.subject | replace('Fwd:','')}} - {{
    as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%a %B %-d,
    %H:%M') }}
  type: >-
    {% if 'New message' in  trigger.json.subject %}voicemail{% else
    %}missed_call{% endif %}
  phone_no: "{{trigger.json.subject | regex_findall_index('\\d+.?\\d+') }}"
mode: single

Code above updated, so you can now click a button to load the phone dialler ready to call the number. The HA app needs the Phone permission turned on.