{"id":13550,"date":"2023-06-05T17:08:46","date_gmt":"2023-06-05T17:08:46","guid":{"rendered":"https:\/\/www.rjt.org.uk\/home\/?post_type=home_assistant_tip&#038;p=13550"},"modified":"2023-08-18T17:52:21","modified_gmt":"2023-08-18T17:52:21","slug":"extracting-voicemails-from-email","status":"publish","type":"home_assistant_tip","link":"https:\/\/www.rjt.org.uk\/home\/archives\/home-assistant-tip\/extracting-voicemails-from-email\/","title":{"rendered":"Extracting Voicemails from email"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>Step 1<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>See <a href=\"https:\/\/www.zoho.com\/mail\/help\/dev-platform\/webhook.html#alink4\">dev-platform\/webhook at Zoho<\/a> for instructions.<\/p>\n\n\n\n<p>Step 2<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">homeassistant:\n  allowlist_external_dirs: \n    - \/media\/\n    - \/media\/downloads\ndownloader:\n  download_dir: \/media\/downloads<\/pre>\n\n\n\n<p>Step 3<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">alias: Voicemail\ndescription: \"\"\ntrigger:\n  - platform: webhook\n    allowed_methods:\n      - POST\n      - PUT\n      - GET\n    local_only: false\n    webhook_id: voicemail-has-arrived\ncondition: []\naction:\n  - service: script.messages_update\n    data:\n      action: create_message\n      message: \"{{subject}}\"\n  - if:\n      - condition: template\n        value_template: \"{{type == 'missed_call'}}\"\n    then:\n      - service: notify.mobile_app_jane_s_phone\n        data:\n          message: \"{{subject}}\"\n          data:\n            actions:\n              - action: URI\n                title: Call Number\n                uri: >-\n                  intent:tel:{{phone_no}}\/\/\/#Intent;action=android.intent.action.DIAL;end\n      - service: notify.mobile_app_stephen_phone\n        data:\n          message: \"{{subject}}\"\n          data:\n            actions:\n              - action: URI\n                title: Call Number\n                uri: >-\n                  intent:tel:{{phone_no}}\/\/\/#Intent;action=android.intent.action.DIAL;end\n      - service: script.messages_update\n        data:\n          action: create_message\n          message: \"{{subject}}\"\n      - stop: Missed call - message only\n  - service: notify.mobile_app_jane_s_phone\n    data:\n      data:\n        actions:\n          - action: URI\n            title: View Voicemail\n            uri: \"{{filename}}\"\n          - action: URI\n            title: Call Number\n            uri: >-\n              intent:tel:{{phone_no}}\/\/\/#Intent;action=android.intent.action.DIAL;end            \n      message: \"{{subject}}\"\n    enabled: true\n  - service: notify.mobile_app_stephen_phone\n    data:\n      data:\n        actions:\n          - action: URI\n            title: View Voicemail\n            uri: \"{{filename}}\"\n          - action: URI\n            title: Call Number\n            uri: >-\n              intent:tel:{{phone_no}}\/\/\/#Intent;action=android.intent.action.DIAL;end  \n      message: \"{{subject}}\"\n    enabled: true\n  - service: downloader.download_file\n    data:\n      url: \"{{filename}}\"\n    enabled: true\nvariables:\n  filename: |-\n    {% set list = trigger.json.summary |\n              regex_findall('(https\\:\\\/\\\/secure.live.sipgate.co.uk\\\/download.*voice.*mp3)','')  %}              \n    {{list[0] or 'NA'}}  \n  matchtable: |-\n    {% set list = trigger.json.summary |\n              regex_findall('(https\\:\\\/\\\/secure.live.sipgate.co.uk\\\/download.*voice.*mp3)','')  %}              \n    {{list}} \n  subject: >-\n    {{trigger.json.subject | replace('Fwd:','')}} - {{\n    as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%a %B %-d,\n    %H:%M') }}\n  type: >-\n    {% if 'New message' in  trigger.json.subject %}voicemail{% else\n    %}missed_call{% endif %}\n  phone_no: \"{{trigger.json.subject | regex_findall_index('\\\\d+.?\\\\d+') }}\"\nmode: single\n<\/pre>\n\n\n\n<p>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.<\/p>\n","protected":false},"template":"","class_list":["post-13550","home_assistant_tip","type-home_assistant_tip","status-publish","hentry","comments-off"],"_links":{"self":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip\/13550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip"}],"about":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/types\/home_assistant_tip"}],"wp:attachment":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/media?parent=13550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}