Making Facebook messenger notify more usable

At the moment (version .37) notification via messenger requires the phone number of the recipient. It will be nice to have it working also with facebook user ID. The work around is to change in file
https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/notify/facebook.py
line 60 from
“recipient”: {“phone_number”: target},
to
“recipient”: {“id”: target},
introducing the choice between ID or phone_number will be nice

Nice, just be careful on the ID part as I think that requires some further input on the Facebook developer side in order to work. I haven’t tried myself so can’t confirm.

https://developers.facebook.com/docs/messenger-platform/send-api-reference#request

Yes, as far as I’ve understood, with the ID you need the recipient to have started the chat. While started (and not deleted by the recipient). It works like a charme

HI, since I’ve been experimenting with this components, I found that attaching a picture is not that straightforward, at least it wasn’t for me. The solution is twofold:

  1. The picture you want to attach should be reachable via web from some kind of url (which may contain authentication)

  2. The correct configuration is:

    • service: notify.messenger
      data:
      message: “your message here”
      data:
      attachment:
      type: “image”
      payload:
      url: “http://your.url.here
      target:
      - yourID

Cool, I managed to get the sending of an image on an externally accessible URL as per your above.

Automation example:

- alias: Facebook Notify Native
  trigger:
    platform: state
    entity_id: input_boolean.facebook_notify
    from: 'off'
    to: 'on'
  action:
    service: notify.facebooknotify
    data:
      message: 'Test Message'
      data:
        attachment: 
            type: 'image'
            payload: 
                url: 'https://home-assistant.io/demo/favicon-192x192.png'
      target: '+MOBILENUMBER'

Reading from the facebook messenger API details it is also possible to upload and send files. I think that this would only require an addition of a new field “filedata”. I see “recipient” and “message” already exist in the python file.

curl  \
  -F 'recipient={"id":"USER_ID"}' \
  -F 'message={"attachment":{"type":"image", "payload":{}}}' \
  -F 'filedata=@/tmp/shirt.png;type=image/png' \
  "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"    

I currently achieve this by calling a bash script with the curl command and this will upload and send via messenger. It would be great to have this natively within HomeAssistant.
I’m no python/github expert, not sure if this is something that @adrianlzt or @Danielhiversen could assist with?

1 Like

Yes, you right. To upload from local computer without using an external curl call we need to modify the python component in the payload part and in the “headers” because a Form upload is required. I’ll give it a try and report back. Your solution is good anyway

1 Like

Awesome, more than happy to help test it out.

Does anyone know if filedata has been implemented yet?

@Pawny and mcfanda, thanx for the automation example. However when I try to replace the jpg url with my own url I don’t get the screenshot. The error I get is:
2017-05-10 14:33:01 ERROR (Thread-5) [homeassistant.components.notify.facebook] Error 400 : (#100) Failed to fetch the file from the url (Code 100)

Note: I use a Hikvision camera which doesn’t have a .jpg url but it produces an image by using “http://ip-adress:port/Streaming/channels/1/picture”

I also tried to pull a jpg with Telegram notification and this works just fine.

Any suggestions?

Facebook can only pull an image that is accessible from the internet. I would assume your Hikvision camera isn’t open to the internet so Facebook can’t download the image to send it.

The alternative version is using the curl script that I posted above which uploads the image. This is an interim solution until that capability is added to the Facebook component.

Another question regarding target:
target: '+MOBILENUMBER'

I added two mobile numbers like below but only receive messages on the first mobile number. I wonder if my automation.yaml is correct?

This my automation.yaml:

 - alias: Notify when laundry is done
   trigger:
     platform: state
     entity_id: switch.washing_machine
     from: 'on'
     to: 'off'
   action:
     - service: notify.facebook
       data:
         message: "Laundry is ready."
         target:
           - +MOBILENUMBER01
           - +MOBILENUMBER02

get error when run curl command, please help.
{“error”:{“message”:"(#100) No matching user found",“type”:“OAuthException”,“code”:100,“error_subcode”:2018001,“fbtrace_id”:“C/QsAC+LYkH”}}

I’m getting the same response. Since Oct 9th (after i updated to 0.55, now on 0.55.1), I’m getting tons of:

2017-10-16 20:06:30 ERROR (Thread-8) [homeassistant.components.notify.facebook] Error 400 : (#100) No matching user found (Code 100)

in my logs. I have not changed anything in my config or Facebook apps page, but to me it seems the latter has changed its policy when it comes to non-US users and the ability to use messenger for apps. Can anyone confirm this (or is it a HA-side issue)?

I’m using Facebook notify for almost all automation information from HA, and now it’s completely broken so I’d greatly appreciate any help pinpointing the issue…

Yep, same issue here. Any update on whats going on? :slight_smile:

I’m having the same issue as well.

Try adding single quotes around the target telephone number. It solved the issue for me.

Adding quotes around the target telephone number (in my secrets.yaml file, since that’s where I keep mine) fixed things up for me.

In case anyone’s interested also hacked around sending a local file with a shell_command. In configuration.yaml I added the following commands to save and send a local image with facebook messenger:

shell_command:
  take_snapshot: 'curl "{{ url }}" --output {{ filename }} --silent'
  send_image: 'curl -F ''recipient={"phone_number":"{{ target }}"}'' -F ''message={"attachment":{"type":"image", "payload":{}}}'' -F ''filedata=@{{ filename }};type=image/jpeg'' "https://graph.facebook.com/v2.6/me/messages?access_token={{ token }}"'

Then, in an automation’s action I can save and send the image.

- id: front_motion
  alias: motion at front door
  trigger:
  - platform: state
    entity_id: binary_sensor.front_door_motion
    from: 'off'
    to: 'on'
  action:
  - service: shell_command.take_snapshot
    data_template:
      url: 'http://localhost:8123{{ states.camera.front_door.attributes.entity_picture }}'
      filename: '/tmp/doorbell_motion.jpg'
  - service: notify.facebook
    data:
      message: Motion at front door
      target: !secret notify_phone_number
  - service: shell_command.send_image
    data:
      filename: '/tmp/doorbell_motion.jpg'
      target: !secret notify_phone_number
      token: !secret facebook_token
3 Likes

@broox, im getting this error:
{“error”:{“message”:"(#100) Upload attachment failure",“type”:“OAuthException”,“code”:100,“error_subcode”:2018047,“fbtrace_id”:"*********"}}

Any suggestions?
I can send text messages fine(same auth code) with notify.facebook, and the jpeg-file gets saved under /tmp/

Did you get yours to work?

Where can you find the logs of errors? Mine doesn’t say anything in home-assistant.log but no images are sent.

No i did not. Still not able to send images via Facebook notify