2019-04-03 15:40:03 +00:00
|
|
|
"""Facebook platform for notify component."""
|
2018-04-17 12:23:41 +00:00
|
|
|
import json
|
2016-12-29 12:49:11 +00:00
|
|
|
import logging
|
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
from aiohttp.hdrs import CONTENT_TYPE
|
2016-12-29 12:49:11 +00:00
|
|
|
import requests
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.notify import (
|
|
|
|
ATTR_DATA,
|
|
|
|
ATTR_TARGET,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService,
|
|
|
|
)
|
2020-04-08 16:47:38 +00:00
|
|
|
from homeassistant.const import CONTENT_TYPE_JSON, HTTP_OK
|
2019-12-09 13:14:40 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-12-29 12:49:11 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PAGE_ACCESS_TOKEN = "page_access_token"
|
|
|
|
BASE_URL = "https://graph.facebook.com/v2.6/me/messages"
|
2016-12-29 12:49:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_PAGE_ACCESS_TOKEN): cv.string}
|
|
|
|
)
|
2016-12-29 12:49:11 +00:00
|
|
|
|
|
|
|
|
2017-01-17 06:35:09 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-12-31 04:29:44 +00:00
|
|
|
"""Get the Facebook notification service."""
|
|
|
|
return FacebookNotificationService(config[CONF_PAGE_ACCESS_TOKEN])
|
2016-12-29 12:49:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FacebookNotificationService(BaseNotificationService):
|
2016-12-31 04:29:44 +00:00
|
|
|
"""Implementation of a notification service for the Facebook service."""
|
2016-12-29 12:49:11 +00:00
|
|
|
|
2016-12-31 04:29:44 +00:00
|
|
|
def __init__(self, access_token):
|
2016-12-29 12:49:11 +00:00
|
|
|
"""Initialize the service."""
|
|
|
|
self.page_access_token = access_token
|
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
"""Send some message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
payload = {"access_token": self.page_access_token}
|
2016-12-31 04:29:44 +00:00
|
|
|
targets = kwargs.get(ATTR_TARGET)
|
2017-02-06 13:01:41 +00:00
|
|
|
data = kwargs.get(ATTR_DATA)
|
|
|
|
|
|
|
|
body_message = {"text": message}
|
|
|
|
|
|
|
|
if data is not None:
|
|
|
|
body_message.update(data)
|
|
|
|
# Only one of text or attachment can be specified
|
2019-07-31 19:25:30 +00:00
|
|
|
if "attachment" in body_message:
|
|
|
|
body_message.pop("text")
|
2016-12-31 04:29:44 +00:00
|
|
|
|
|
|
|
if not targets:
|
2017-01-04 12:53:29 +00:00
|
|
|
_LOGGER.error("At least 1 target is required")
|
2016-12-31 04:29:44 +00:00
|
|
|
return
|
|
|
|
|
2020-03-10 07:21:04 +00:00
|
|
|
for target in targets:
|
|
|
|
# If the target starts with a "+", it's a phone number,
|
|
|
|
# otherwise it's a user id.
|
|
|
|
if target.startswith("+"):
|
|
|
|
recipient = {"phone_number": target}
|
|
|
|
else:
|
|
|
|
recipient = {"id": target}
|
|
|
|
|
|
|
|
body = {
|
|
|
|
"recipient": recipient,
|
|
|
|
"message": body_message,
|
|
|
|
"messaging_type": "MESSAGE_TAG",
|
|
|
|
"tag": "ACCOUNT_UPDATE",
|
2016-12-31 04:29:44 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = requests.post(
|
2020-03-10 07:21:04 +00:00
|
|
|
BASE_URL,
|
|
|
|
data=json.dumps(body),
|
2019-07-31 19:25:30 +00:00
|
|
|
params=payload,
|
|
|
|
headers={CONTENT_TYPE: CONTENT_TYPE_JSON},
|
|
|
|
timeout=10,
|
|
|
|
)
|
2020-04-08 16:47:38 +00:00
|
|
|
if resp.status_code != HTTP_OK:
|
2018-04-17 12:23:41 +00:00
|
|
|
log_error(resp)
|
|
|
|
|
|
|
|
|
|
|
|
def log_error(response):
|
|
|
|
"""Log error message."""
|
|
|
|
obj = response.json()
|
2019-07-31 19:25:30 +00:00
|
|
|
error_message = obj["error"]["message"]
|
|
|
|
error_code = obj["error"]["code"]
|
2018-04-17 12:23:41 +00:00
|
|
|
|
|
|
|
_LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Error %s : %s (Code %s)", response.status_code, error_message, error_code
|
|
|
|
)
|