Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright Ansible Project
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 module: cisco_webex
11 short_description: Send a message to a Cisco Webex Teams Room or Individual
12 description:
13 - Send a message to a Cisco Webex Teams Room or Individual with options to control the formatting.
14 author: Drew Rusell (@drew-russell)
15 notes:
16 - The O(recipient_type) must be valid for the supplied O(recipient_id).
17 - Full API documentation can be found at U(https://developer.webex.com/docs/api/basics).
18 extends_documentation_fragment:
19 - community.general._attributes
20 attributes:
21 check_mode:
22 support: full
23 diff_mode:
24 support: none
25
26 options:
27
28 recipient_type:
29 description:
30 - The request parameter you would like to send the message to.
31 - Messages can be sent to either a room or individual (by ID or E-Mail).
32 required: true
33 choices: ['roomId', 'toPersonEmail', 'toPersonId']
34 type: str
35
36 recipient_id:
37 description:
38 - The unique identifier associated with the supplied O(recipient_type).
39 required: true
40 type: str
41
42 msg_type:
43 description:
44 - Specifies how you would like the message formatted.
45 default: text
46 choices: ['text', 'markdown']
47 type: str
48 aliases: ['message_type']
49
50 personal_token:
51 description:
52 - Your personal access token required to validate the Webex Teams API.
53 required: true
54 aliases: ['token']
55 type: str
56
57 msg:
58 description:
59 - The message you would like to send.
60 required: true
61 type: str
62 """
63
64 EXAMPLES = r"""
65 # Note: The following examples assume a variable file has been imported
66 # that contains the appropriate information.
67
68 - name: Cisco Webex Teams - Markdown Message to a Room
69 community.general.cisco_webex:
70 recipient_type: roomId
71 recipient_id: "{{ room_id }}"
72 msg_type: markdown
73 personal_token: "{{ token }}"
74 msg: "**Cisco Webex Teams Ansible Module - Room Message in Markdown**"
75
76 - name: Cisco Webex Teams - Text Message to a Room
77 community.general.cisco_webex:
78 recipient_type: roomId
79 recipient_id: "{{ room_id }}"
80 msg_type: text
81 personal_token: "{{ token }}"
82 msg: "Cisco Webex Teams Ansible Module - Room Message in Text"
83
84 - name: Cisco Webex Teams - Text Message by an Individuals ID
85 community.general.cisco_webex:
86 recipient_type: toPersonId
87 recipient_id: "{{ person_id}}"
88 msg_type: text
89 personal_token: "{{ token }}"
90 msg: "Cisco Webex Teams Ansible Module - Text Message to Individual by ID"
91
92 - name: Cisco Webex Teams - Text Message by an Individuals E-Mail Address
93 community.general.cisco_webex:
94 recipient_type: toPersonEmail
95 recipient_id: "{{ person_email }}"
96 msg_type: text
97 personal_token: "{{ token }}"
98 msg: "Cisco Webex Teams Ansible Module - Text Message to Individual by E-Mail"
99 """
100
101 RETURN = r"""
102 status_code:
103 description:
104 - The Response Code returned by the Webex Teams API.
105 - Full Response Code explanations can be found at U(https://developer.webex.com/docs/api/basics).
106 returned: always
107 type: int
108 sample: 200
109
110 message:
111 description:
112 - The Response Message returned by the Webex Teams API.
113 - Full Response Code explanations can be found at U(https://developer.webex.com/docs/api/basics).
114 returned: always
115 type: str
116 sample: OK (585 bytes)
117 """
118 from ansible.module_utils.basic import AnsibleModule
119 from ansible.module_utils.urls import fetch_url
120
121
122 def webex_msg(module):
123 """When check mode is specified, establish a read only connection, that does not return any user specific
124 data, to validate connectivity. In regular mode, send a message to a Cisco Webex Teams Room or Individual"""
125
126 # Ansible Specific Variables
127 results = {}
128 ansible = module.params
129
130 headers = {"Authorization": f"Bearer {ansible['personal_token']}", "content-type": "application/json"}
131
132 if module.check_mode:
133 url = "https://webexapis.com/v1/people/me"
134 payload = None
135
136 else:
137 url = "https://webexapis.com/v1/messages"
138
139 payload = {ansible["recipient_type"]: ansible["recipient_id"], ansible["msg_type"]: ansible["msg"]}
140
141 payload = module.jsonify(payload)
142
143 response, info = fetch_url(module, url, data=payload, headers=headers)
144
145 status_code = info["status"]
146 msg = info["msg"]
147
148 # Module will fail if the response is not 200
149 if status_code != 200:
150 results["failed"] = True
151 results["status_code"] = status_code
152 results["message"] = msg
153 else:
154 results["failed"] = False
155 results["status_code"] = status_code
156
157 if module.check_mode:
158 results["message"] = "Authentication Successful."
159 else:
160 results["message"] = msg
161
162 return results
163
164
165 def main():
166 """Ansible main."""
167 module = AnsibleModule(
168 argument_spec=dict(
169 recipient_type=dict(required=True, choices=["roomId", "toPersonEmail", "toPersonId"]),
170 recipient_id=dict(required=True, no_log=True),
171 msg_type=dict(default="text", aliases=["message_type"], choices=["text", "markdown"]),
172 personal_token=dict(required=True, no_log=True, aliases=["token"]),
173 msg=dict(required=True),
174 ),
175 supports_check_mode=True,
176 )
177
178 results = webex_msg(module)
179
180 module.exit_json(**results)
181
182
183 if __name__ == "__main__":
184 main()