boredazfcuk / docker-icloudpd

An Alpine Linux container for the iCloud Photos Downloader command line utility
1.71k stars 164 forks source link

Feature request - Webhooks #16

Closed eldudemeister closed 4 years ago

eldudemeister commented 4 years ago

Hi, is it possible/doable to have webhooks as a notification service or notification to an mqtt broker? Loving the container!

boredazfcuk commented 4 years ago

Probably, the Discord webhook has this curl command : https://birdie0.github.io/discord-webhooks-guide/tools/curl.html and an example:

url='https://discordapp.com/api/webhooks/"203019812404264973/rptBmYgoehu70kw2rItSlhRqKi7kMJh1bM2KCUUD2vR6grZckvtdl62h4xR7XWUS5463'
curl -H "Content-Type: application/json" \
-X POST \
-d '{"username": "test", "content": "hello"}' $url

which is pretty similar to to my Telegram command:

curl --silent --request POST "${notification_url}" \
         --data chat_id="${telegram_chat_id}" \
         --data parse_mode="markdown" \
         --data text="${2}"

My Webhooks one would probably look like:

notification_url='https://discordapp.com/api/webhooks/"203019812404264973/rptBmYgoehu70kw2rItSlhRqKi7kMJh1bM2KCUUD2vR6grZckvtdl62h4xR7XWUS5463'

curl --silent --request POST "${notification_url}" \
         --data username="${webhook_username}" \
         --data content="${2}"

I don't have a webhooks notification system I can test with though... To be honest, I don't even know if the PushBullet notification method works coz I don't have that either, but it looks like it should as the API is pretty much the same lol.

What webhooks service in particular are you using?

eldudemeister commented 4 years ago

I'm using Home Assistant as a notifier service that would generate the webhook which would then send a notification to my phone depending on the message json payload passed from icloud-pd. If it helps, I think i used the pushbullet notification previously and pretty sure it was working good!

boredazfcuk commented 4 years ago

Hi, I've just pushed a new version that has untested Webhook support. It probably won't work as I'm unsure of the format of the data that I should be sending.

I've found this link which suggests it should be formatted as:

curl --request POST \
  --url http://HomeAsisstant:8123/api/webhook/MyWebhookID\
  --header 'content-type: application/json' \
  --data '{
    "data":"Forwarded"
}'

So I've just imitated this, but changed "Forwarded" to the message I send via Telegram.

Give it a test and let me know what you find.

eldudemeister commented 4 years ago

Thanks so much for this, I've pulled the new image and updated my compose, just a couple of questions, should the host ip address be the full http://HomeAsisstant:8123/api/webhook/MyWebhookID or just the host ip and the port as I specify the webhook id separately.

boredazfcuk commented 4 years ago

Host IP is just the IP address, nothing else... You could put the hostname of the computer in it if you like... in fact, I’ll rename that variable to webhook_server in the next push, I’ll sort that tonight.

I’ll also add a webhook_port variable just in case people run on non-default.

I combine the webhook_https, host_ip_address and webhook_id variable to generate the server’s URL.

boredazfcuk commented 4 years ago

I've pushed this to Dockerhub now. The variables it uses for the webhooks URL are now:

webhook_server
webhook_port
webhook_id
webhook_https (if set True webhook_scheme=https, otherwise webhook_scheme=http)

It puts them all together to generate the notification URL like this:

notification_url="${webhook_scheme}://${webhook_server}:${webhook_port}/api/webhook/${webhook_id}"

Let me know how you get on and I'll either update again, or close the issue.

Thanks.

eldudemeister commented 4 years ago

I've pulled the latest image but I get the following in the logs 020-05-28 14:28:56 WARNING Webhook notifications enabled, but API key/token not set - disabling notifications My compose looks like this?

     - notification_days=14
     - webhook_server=192.168.68.110
     - webhook_port=8123
     - webhook_id=VCyx4YOljF0apiIEK3Gu7ssWj9hSDT9T
     - webhook_https=false
     - synchronisation_interval=43200

Have I gone wrong somewhere?

boredazfcuk commented 4 years ago

My mistake. There was an additional check when configuring notifications that I had missed. I've updated and pushed to Dockerhub again.

eldudemeister commented 4 years ago

That worked! Thanks so much again, the message is being passed to home assistant so now I just need to configure the payload in my notification on my phone

boredazfcuk commented 4 years ago

No worries. I use some emoji characters in it which may not be translated well on the receiving system.

If you need me to amend the message, then just reply back here.

eldudemeister commented 4 years ago

That would be amazing if you could, this character ↵ is causing some issues?

eldudemeister commented 4 years ago

and the backslashes and carriage return?

boredazfcuk commented 4 years ago

I've removed those all. That's going to make the downloaded/deleted files previews look a little weird. Let me know if you need those removing too.