Facebook notify updated

This commit is contained in:
Gopal 2016-12-31 09:59:44 +05:30
parent 6892048de0
commit 227fb29cab

View File

@ -12,45 +12,47 @@ import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import ( from homeassistant.components.notify import (
PLATFORM_SCHEMA, BaseNotificationService) ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_ID = 'id'
CONF_PHONE_NUMBER = 'phone_number'
CONF_PAGE_ACCESS_TOKEN = 'page_access_token' CONF_PAGE_ACCESS_TOKEN = 'page_access_token'
BASE_URL = 'https://graph.facebook.com/v2.6/me/messages' BASE_URL = 'https://graph.facebook.com/v2.6/me/messages'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_PHONE_NUMBER): cv.string,
vol.Required(CONF_PAGE_ACCESS_TOKEN): cv.string, vol.Required(CONF_PAGE_ACCESS_TOKEN): cv.string,
}) })
def get_service(hass, config): def get_service(hass, config):
"""Get the Twitter notification service.""" """Get the Facebook notification service."""
return FacebookNotificationService( return FacebookNotificationService(config[CONF_PAGE_ACCESS_TOKEN])
config[CONF_PHONE_NUMBER], config[CONF_PAGE_ACCESS_TOKEN]
)
class FacebookNotificationService(BaseNotificationService): class FacebookNotificationService(BaseNotificationService):
"""Implementation of a notification service for the Twitter service.""" """Implementation of a notification service for the Facebook service."""
def __init__(self, id, access_token): def __init__(self, access_token):
"""Initialize the service.""" """Initialize the service."""
self.user_id = id
self.page_access_token = access_token self.page_access_token = access_token
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
"""Send some message.""" """Send some message."""
payload = {'access_token': self.page_access_token} payload = {'access_token': self.page_access_token}
targets = kwargs.get(ATTR_TARGET)
if not targets:
_LOGGER.info("At least 1 target is required")
return
for target in targets:
body = { body = {
"recipient": {"phone_number": self.user_id}, "recipient": {"phone_number": target},
"message": {"text": message} "message": {"text": message}
} }
import json import json
resp = requests.post(BASE_URL, data=json.dumps(body), params=payload, resp = requests.post(BASE_URL, data=json.dumps(body),
params=payload,
headers={'Content-Type': 'application/json'}) headers={'Content-Type': 'application/json'})
if resp.status_code != 200: if resp.status_code != 200:
obj = resp.json() obj = resp.json()