Debian Codegraph @

ansible_collections/community/general/plugins/modules/matrix.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2018, Jan Christian Grünhage <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 author: "Jan Christian Grünhage (@jcgruenhage)"
11 module: matrix
12 short_description: Send notifications to matrix
13 description:
14 - This module sends HTML formatted notifications to matrix rooms.
15 extends_documentation_fragment:
16 - community.general._attributes
17 attributes:
18 check_mode:
19 support: full
20 diff_mode:
21 support: none
22 options:
23 msg_plain:
24 type: str
25 description:
26 - Plain text form of the message to send to matrix, usually markdown.
27 required: true
28 msg_html:
29 type: str
30 description:
31 - HTML form of the message to send to matrix.
32 required: true
33 room_id:
34 type: str
35 description:
36 - ID of the room to send the notification to.
37 required: true
38 hs_url:
39 type: str
40 description:
41 - URL of the homeserver, where the CS-API is reachable.
42 required: true
43 token:
44 type: str
45 description:
46 - Authentication token for the API call. If provided, O(user_id) and O(password) are not required.
47 user_id:
48 type: str
49 description:
50 - The user ID of the user.
51 password:
52 type: str
53 description:
54 - The password to log in with.
55 requirements:
56 - matrix-client (Python library)
57 """
58
59 EXAMPLES = r"""
60 - name: Send matrix notification with token
61 community.general.matrix:
62 msg_plain: "**hello world**"
63 msg_html: "<b>hello world</b>"
64 room_id: "!12345678:server.tld"
65 hs_url: "https://matrix.org"
66 token: "{{ matrix_auth_token }}"
67
68 - name: Send matrix notification with user_id and password
69 community.general.matrix:
70 msg_plain: "**hello world**"
71 msg_html: "<b>hello world</b>"
72 room_id: "!12345678:server.tld"
73 hs_url: "https://matrix.org"
74 user_id: "ansible_notification_bot"
75 password: "{{ matrix_auth_password }}"
76 """
77
78 RETURN = r"""
79 """
80 import traceback
81
82 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
83
84 MATRIX_IMP_ERR = None
85 try:
86 from matrix_client.client import MatrixClient
87 except ImportError:
88 MATRIX_IMP_ERR = traceback.format_exc()
89 matrix_found = False
90 else:
91 matrix_found = True
92
93
94 def run_module():
95 module_args = dict(
96 msg_plain=dict(type="str", required=True),
97 msg_html=dict(type="str", required=True),
98 room_id=dict(type="str", required=True),
99 hs_url=dict(type="str", required=True),
100 token=dict(type="str", no_log=True),
101 user_id=dict(type="str"),
102 password=dict(type="str", no_log=True),
103 )
104
105 result = dict(changed=False, message="")
106
107 module = AnsibleModule(
108 argument_spec=module_args,
109 mutually_exclusive=[["password", "token"]],
110 required_one_of=[["password", "token"]],
111 required_together=[["user_id", "password"]],
112 supports_check_mode=True,
113 )
114
115 if not matrix_found:
116 module.fail_json(msg=missing_required_lib("matrix-client"), exception=MATRIX_IMP_ERR)
117
118 if module.check_mode:
119 return result
120
121 # create a client object
122 client = MatrixClient(module.params["hs_url"])
123 if module.params["token"] is not None:
124 client.api.token = module.params["token"]
125 else:
126 client.login(module.params["user_id"], module.params["password"], sync=False)
127
128 # make sure we are in a given room and return a room object for it
129 room = client.join_room(module.params["room_id"])
130 # send an html formatted messages
131 room.send_html(module.params["msg_html"], module.params["msg_plain"])
132
133 module.exit_json(**result)
134
135
136 def main():
137 run_module()
138
139
140 if __name__ == "__main__":
141 main()