Skip to content

Commit

Permalink
Merge pull request #2 from shlomki/add-default-target-configuration
Browse files Browse the repository at this point in the history
Add default target configuration (and update documentation accordingly)
  • Loading branch information
t0mer authored Apr 23, 2024
2 parents d129a00 + 9b38c28 commit e54ab77
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,23 @@ Restart Home Assistant and add the following section to your *configuration.yaml
notify:
- platform: greenapi
name: greenapi
instance_id: #Set the instanceid
token: #Set the greenapi token.
instance_id: #REQUIRED: Set the instanceid
token: #REQUIRED: Set the greenapi token.
target: #OPTIONAL! Set the detault target. If you set the default target here, you won't have to specify it again in your service calls.
```
* instance_id is the Green API instance id.
* token is the Green API instance token.
* Target is the chat/contact/group id to send the message to:
* target is the chat/contact/group id to send the message to:
* For groups, the id should end with *@g.us*
* For chats, the id should end with *@c.us*
## Sending a message
To Send a message you call the service and provide the following parameters:
* message (**Required**): Test to send.
* target (**Required**): The chat/group id to send the message to.
* title (**OPTIONAL**): Add a title for the message in **bold**.
* target (**OPTIONAL** if you've already defined the default target in your notify service, otherwise required): The chat/group id to send the message to.
![Send text message](screenshots/text_message.png)
Expand All @@ -96,7 +98,7 @@ data:
target: 972*********@c.us
```
### Optional - Attache media to message
### Optional - Attach media to message
To send message with media, add the following to the data parameter:
* file : [Path to the file]
Expand Down
10 changes: 6 additions & 4 deletions custom_components/greenapi/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
# vol.Required(ATTR_TARGET): cv.string,
vol.Optional(ATTR_TARGET): cv.string,
vol.Required(ATTR_INSTANCE): cv.string,
vol.Required(ATTR_TOKEN): cv.string,
vol.Optional(ATTR_TITLE): cv.string,
Expand All @@ -27,15 +27,17 @@ def get_service(hass, config, discovery_info=None):
title = config.get(ATTR_TITLE)
token = config.get(ATTR_TOKEN)
instance_id = config.get(ATTR_INSTANCE)
return GreenAPINotificationService(title, token, instance_id)
target = config.get(ATTR_TARGET)
return GreenAPINotificationService(title, token, instance_id, target)

class GreenAPINotificationService(BaseNotificationService):

def __init__(self, title, token,instance_id):
def __init__(self, title, token,instance_id, target):
"""Initialize the service."""
self._title = title
self._token = token
self._instance_id = instance_id
self._target = target
self._greenAPI = API.GreenAPI(self._instance_id, self._token)

def send_message(self, message="", **kwargs):
Expand All @@ -48,7 +50,7 @@ def send_message(self, message="", **kwargs):
title = f"*{title}*"
message = f"{title} \n {message}"
data = kwargs.get(ATTR_DATA)
target = kwargs.get(ATTR_TARGET)[0]
target = kwargs.get(ATTR_TARGET)[0] if kwargs.get(ATTR_TARGET) is not None else self._target #Allow setting the target from either the service-call or the service config. Service call target can override the default config.
_LOGGER.info(f"Sending message to {target}")
if data is not None:
file_path = data["file"]
Expand Down

0 comments on commit e54ab77

Please sign in to comment.