New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add matrix notification module #45823
Merged
gundalow
merged 10 commits into
ansible:devel
from
jcgruenhage:matrix-notification-module
Oct 24, 2018
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bd6dec4
Add matrix notification module
jcgruenhage b04fbff
try to make ansibot happy
jcgruenhage a6ce0e1
docs
jcgruenhage 9842e75
fix typo in encoding
jcgruenhage 4efb8e4
is ansibot happy now?
jcgruenhage e94bd8e
change matrix python lib requirement description
jcgruenhage 56513cd
Example formatting & no_log
gundalow b55968e
Clean up argument requirements
jcgruenhage 108407a
Remove requirements duplicate
jcgruenhage 08d0791
not sure on syntax with these, were adapted from an example elsewhere
jcgruenhage File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#!/usr/bin/python | ||
# coding: utf-8 | ||
|
||
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
ANSIBLE_METADATA = { | ||
'metadata_version': '1.1', | ||
'status': ['preview'], | ||
'supported_by': 'community' | ||
} | ||
|
||
DOCUMENTATION = ''' | ||
--- | ||
author: "Jan Christian Grünhage (@jcgruenhage)" | ||
module: matrix | ||
short_description: Send notifications to matrix | ||
description: | ||
- This module sends html formatted notifications to matrix rooms. | ||
version_added: "2.8" | ||
options: | ||
msg_plain: | ||
description: | ||
- Plain text form of the message to send to matrix, usually markdown | ||
required: true | ||
msg_html: | ||
description: | ||
- HTML form of the message to send to matrix | ||
required: true | ||
room_id: | ||
description: | ||
- ID of the room to send the notification to | ||
required: true | ||
hs_url: | ||
description: | ||
- URL of the homeserver, where the CS-API is reachable | ||
required: true | ||
token: | ||
description: | ||
- Authentication token for the API call. If provided, user_id and password are not required | ||
user_id: | ||
description: | ||
- The user id of the user | ||
password: | ||
description: | ||
- The password to log in with | ||
requirements: | ||
- matrix-client (Python library) | ||
''' | ||
|
||
EXAMPLES = ''' | ||
- name: Send matrix notification with token | ||
matrix: | ||
msg_plain: "**hello world**" | ||
msg_html: "<b>hello world</b>" | ||
room_id: "!12345678:server.tld" | ||
hs_url: "https://matrix.org" | ||
token: "{{ matrix_auth_token }}" | ||
|
||
- name: Send matrix notification with user_id and password | ||
matrix: | ||
msg_plain: "**hello world**" | ||
msg_html: "<b>hello world</b>" | ||
room_id: "!12345678:server.tld" | ||
hs_url: "https://matrix.org" | ||
user_id: "ansible_notification_bot" | ||
password: "{{ matrix_auth_password }}" | ||
''' | ||
|
||
RETURN = ''' | ||
''' | ||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
try: | ||
from matrix_client.client import MatrixClient | ||
except ImportError: | ||
matrix_found = False | ||
else: | ||
matrix_found = True | ||
|
||
|
||
def run_module(): | ||
module_args = dict( | ||
msg_plain=dict(type='str', required=True), | ||
msg_html=dict(type='str', required=True), | ||
room_id=dict(type='str', required=True), | ||
hs_url=dict(type='str', required=True), | ||
token=dict(type='str', required=False, no_log=True), | ||
user_id=dict(type='str', required=False), | ||
password=dict(type='str', required=False, no_log=True), | ||
) | ||
|
||
result = dict( | ||
changed=False, | ||
message='' | ||
) | ||
|
||
module = AnsibleModule( | ||
argument_spec=module_args, | ||
mutually_exclusive=[['password', 'token']], | ||
required_one_of=[['password', 'token']], | ||
required_together=[['user_id', 'password']], | ||
supports_check_mode=True | ||
) | ||
|
||
if not matrix_found: | ||
module.fail_json(msg="Python 'matrix-client' module is required. Install via: $ pip install matrix-client") | ||
|
||
if module.check_mode: | ||
return result | ||
|
||
# create a client object | ||
client = MatrixClient(module.params['hs_url']) | ||
if module.params['token'] is not None: | ||
client.api.token = module.params['token'] | ||
else: | ||
client.login(module.params['user_id'], module.params['password'], sync=False) | ||
|
||
# make sure we are in a given room and return a room object for it | ||
room = client.join_room(module.params['room_id']) | ||
# send an html formatted messages | ||
room.send_html(module.params['msg_html'], module.params['msg_plain']) | ||
|
||
module.exit_json(**result) | ||
|
||
|
||
def main(): | ||
run_module() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would have single 'msg' and then a 'type' option, this follows the pattern of other modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be fit if the message sent contains either a plain text or an html message. Matrix sends both, a plain text variant (usually markdown) and also an HTML variant in the content of a text message event, unless there is no formatting (then the HTML variant is omitted). Because of that, the two different args are required as is.