| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017 Marc Sensenich <[email protected]> |
| 3 |
# Copyright (c) 2017 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: office_365_connector_card |
| 11 |
short_description: Use webhooks to create Connector Card messages within an Office 365 group |
| 12 |
description: |
| 13 |
- Creates Connector Card messages through Office 365 Connectors. |
| 14 |
- See |
| 15 |
U(https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-reference#connector-card-for-microsoft-365-groups). |
| 16 |
author: "Marc Sensenich (@marc-sensenich)" |
| 17 |
notes: |
| 18 |
- This module is not idempotent, therefore if you run the same task twice then you create two Connector Cards. |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
webhook: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- The webhook URL is given to you when you create a new Connector. |
| 31 |
required: true |
| 32 |
summary: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- A string used for summarizing card content. |
| 36 |
- This is the message subject. |
| 37 |
- This is required if the text parameter is not populated. |
| 38 |
color: |
| 39 |
type: str |
| 40 |
description: |
| 41 |
- Accent color used for branding or indicating status in the card. |
| 42 |
title: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- A title for the Connector message. Shown at the top of the message. |
| 46 |
text: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- The main text of the card. |
| 50 |
- This is rendered below the sender information and optional title, |
| 51 |
- And above any sections or actions present. |
| 52 |
actions: |
| 53 |
type: list |
| 54 |
elements: dict |
| 55 |
description: |
| 56 |
- This array of objects is used to power the action links found at the bottom of the card. |
| 57 |
sections: |
| 58 |
type: list |
| 59 |
elements: dict |
| 60 |
description: |
| 61 |
- Contains a list of sections to display in the card. |
| 62 |
- For more information see U(https://learn.microsoft.com/en-us/outlook/actionable-messages/message-card-reference#section-fields). |
| 63 |
""" |
| 64 |
|
| 65 |
EXAMPLES = r""" |
| 66 |
- name: Create a simple Connector Card |
| 67 |
community.general.office_365_connector_card: |
| 68 |
webhook: https://outlook.office.com/webhook/GUID/IncomingWebhook/GUID/GUID |
| 69 |
text: 'Hello, World!' |
| 70 |
|
| 71 |
- name: Create a Connector Card with the full format |
| 72 |
community.general.office_365_connector_card: |
| 73 |
webhook: https://outlook.office.com/webhook/GUID/IncomingWebhook/GUID/GUID |
| 74 |
summary: This is the summary property |
| 75 |
title: This is the **card's title** property |
| 76 |
text: This is the **card's text** property. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod |
| 77 |
tempor incididunt ut labore et dolore magna aliqua. |
| 78 |
color: E81123 |
| 79 |
sections: |
| 80 |
- title: This is the **section's title** property |
| 81 |
activity_image: http://connectorsdemo.azurewebsites.net/images/MSC12_Oscar_002.jpg |
| 82 |
activity_title: This is the section's **activityTitle** property |
| 83 |
activity_subtitle: This is the section's **activitySubtitle** property |
| 84 |
activity_text: This is the section's **activityText** property. |
| 85 |
hero_image: |
| 86 |
image: http://connectorsdemo.azurewebsites.net/images/WIN12_Scene_01.jpg |
| 87 |
title: This is the image's alternate text |
| 88 |
text: This is the section's text property. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod |
| 89 |
tempor incididunt ut labore et dolore magna aliqua. |
| 90 |
facts: |
| 91 |
- name: This is a fact name |
| 92 |
value: This is a fact value |
| 93 |
- name: This is a fact name |
| 94 |
value: This is a fact value |
| 95 |
- name: This is a fact name |
| 96 |
value: This is a fact value |
| 97 |
images: |
| 98 |
- image: http://connectorsdemo.azurewebsites.net/images/MicrosoftSurface_024_Cafe_OH-06315_VS_R1c.jpg |
| 99 |
title: This is the image's alternate text |
| 100 |
- image: http://connectorsdemo.azurewebsites.net/images/WIN12_Scene_01.jpg |
| 101 |
title: This is the image's alternate text |
| 102 |
- image: http://connectorsdemo.azurewebsites.net/images/WIN12_Anthony_02.jpg |
| 103 |
title: This is the image's alternate text |
| 104 |
actions: |
| 105 |
- "@type": ActionCard |
| 106 |
name: Comment |
| 107 |
inputs: |
| 108 |
- "@type": TextInput |
| 109 |
id: comment |
| 110 |
is_multiline: true |
| 111 |
title: Input's title property |
| 112 |
actions: |
| 113 |
- "@type": HttpPOST |
| 114 |
name: Save |
| 115 |
target: http://... |
| 116 |
- "@type": ActionCard |
| 117 |
name: Due Date |
| 118 |
inputs: |
| 119 |
- "@type": DateInput |
| 120 |
id: dueDate |
| 121 |
title: Input's title property |
| 122 |
actions: |
| 123 |
- "@type": HttpPOST |
| 124 |
name: Save |
| 125 |
target: http://... |
| 126 |
- "@type": HttpPOST |
| 127 |
name: Action's name prop. |
| 128 |
target: http://... |
| 129 |
- "@type": OpenUri |
| 130 |
name: Action's name prop |
| 131 |
targets: |
| 132 |
- os: default |
| 133 |
uri: http://... |
| 134 |
- start_group: true |
| 135 |
title: This is the title of a **second section** |
| 136 |
text: This second section is visually separated from the first one by setting its **startGroup** property to true. |
| 137 |
""" |
| 138 |
|
| 139 |
RETURN = r""" |
| 140 |
""" |
| 141 |
|
| 142 |
# import module snippets |
| 143 |
from ansible.module_utils.basic import AnsibleModule |
| 144 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 145 |
from ansible.module_utils.urls import fetch_url |
| 146 |
|
| 147 |
OFFICE_365_CARD_CONTEXT = "http://schema.org/extensions" |
| 148 |
OFFICE_365_CARD_TYPE = "MessageCard" |
| 149 |
OFFICE_365_CARD_EMPTY_PAYLOAD_MSG = "Summary or Text is required." |
| 150 |
OFFICE_365_INVALID_WEBHOOK_MSG = "The Incoming Webhook was not reachable." |
| 151 |
|
| 152 |
|
| 153 |
def build_actions(actions): |
| 154 |
action_items = [] |
| 155 |
|
| 156 |
for action in actions: |
| 157 |
action_item = snake_dict_to_camel_dict(action) |
| 158 |
action_items.append(action_item) |
| 159 |
|
| 160 |
return action_items |
| 161 |
|
| 162 |
|
| 163 |
def build_sections(sections): |
| 164 |
sections_created = [] |
| 165 |
|
| 166 |
for section in sections: |
| 167 |
sections_created.append(build_section(section)) |
| 168 |
|
| 169 |
return sections_created |
| 170 |
|
| 171 |
|
| 172 |
def build_section(section): |
| 173 |
section_payload = dict() |
| 174 |
|
| 175 |
if "title" in section: |
| 176 |
section_payload["title"] = section["title"] |
| 177 |
|
| 178 |
if "start_group" in section: |
| 179 |
section_payload["startGroup"] = section["start_group"] |
| 180 |
|
| 181 |
if "activity_image" in section: |
| 182 |
section_payload["activityImage"] = section["activity_image"] |
| 183 |
|
| 184 |
if "activity_title" in section: |
| 185 |
section_payload["activityTitle"] = section["activity_title"] |
| 186 |
|
| 187 |
if "activity_subtitle" in section: |
| 188 |
section_payload["activitySubtitle"] = section["activity_subtitle"] |
| 189 |
|
| 190 |
if "activity_text" in section: |
| 191 |
section_payload["activityText"] = section["activity_text"] |
| 192 |
|
| 193 |
if "hero_image" in section: |
| 194 |
section_payload["heroImage"] = section["hero_image"] |
| 195 |
|
| 196 |
if "text" in section: |
| 197 |
section_payload["text"] = section["text"] |
| 198 |
|
| 199 |
if "facts" in section: |
| 200 |
section_payload["facts"] = section["facts"] |
| 201 |
|
| 202 |
if "images" in section: |
| 203 |
section_payload["images"] = section["images"] |
| 204 |
|
| 205 |
if "actions" in section: |
| 206 |
section_payload["potentialAction"] = build_actions(section["actions"]) |
| 207 |
|
| 208 |
return section_payload |
| 209 |
|
| 210 |
|
| 211 |
def build_payload_for_connector_card( |
| 212 |
module, summary=None, color=None, title=None, text=None, actions=None, sections=None |
| 213 |
): |
| 214 |
payload = dict() |
| 215 |
payload["@context"] = OFFICE_365_CARD_CONTEXT |
| 216 |
payload["@type"] = OFFICE_365_CARD_TYPE |
| 217 |
|
| 218 |
if summary is not None: |
| 219 |
payload["summary"] = summary |
| 220 |
|
| 221 |
if color is not None: |
| 222 |
payload["themeColor"] = color |
| 223 |
|
| 224 |
if title is not None: |
| 225 |
payload["title"] = title |
| 226 |
|
| 227 |
if text is not None: |
| 228 |
payload["text"] = text |
| 229 |
|
| 230 |
if actions: |
| 231 |
payload["potentialAction"] = build_actions(actions) |
| 232 |
|
| 233 |
if sections: |
| 234 |
payload["sections"] = build_sections(sections) |
| 235 |
|
| 236 |
payload = module.jsonify(payload) |
| 237 |
return payload |
| 238 |
|
| 239 |
|
| 240 |
def do_notify_connector_card_webhook(module, webhook, payload): |
| 241 |
headers = {"Content-Type": "application/json"} |
| 242 |
|
| 243 |
response, info = fetch_url(module=module, url=webhook, headers=headers, method="POST", data=payload) |
| 244 |
|
| 245 |
if info["status"] == 200: |
| 246 |
module.exit_json(changed=True) |
| 247 |
elif info["status"] == 400 and module.check_mode: |
| 248 |
if info["body"] == OFFICE_365_CARD_EMPTY_PAYLOAD_MSG: |
| 249 |
module.exit_json(changed=True) |
| 250 |
else: |
| 251 |
module.fail_json(msg=OFFICE_365_INVALID_WEBHOOK_MSG) |
| 252 |
else: |
| 253 |
module.fail_json(msg=f"failed to send {payload} as a connector card to Incoming Webhook: {info['msg']}") |
| 254 |
|
| 255 |
|
| 256 |
def main(): |
| 257 |
module = AnsibleModule( |
| 258 |
argument_spec=dict( |
| 259 |
webhook=dict(required=True, no_log=True), |
| 260 |
summary=dict(type="str"), |
| 261 |
color=dict(type="str"), |
| 262 |
title=dict(type="str"), |
| 263 |
text=dict(type="str"), |
| 264 |
actions=dict(type="list", elements="dict"), |
| 265 |
sections=dict(type="list", elements="dict"), |
| 266 |
), |
| 267 |
supports_check_mode=True, |
| 268 |
) |
| 269 |
|
| 270 |
webhook = module.params["webhook"] |
| 271 |
summary = module.params["summary"] |
| 272 |
color = module.params["color"] |
| 273 |
title = module.params["title"] |
| 274 |
text = module.params["text"] |
| 275 |
actions = module.params["actions"] |
| 276 |
sections = module.params["sections"] |
| 277 |
|
| 278 |
payload = build_payload_for_connector_card(module, summary, color, title, text, actions, sections) |
| 279 |
|
| 280 |
if module.check_mode: |
| 281 |
# In check mode, send an empty payload to validate connection |
| 282 |
check_mode_payload = build_payload_for_connector_card(module) |
| 283 |
do_notify_connector_card_webhook(module, webhook, check_mode_payload) |
| 284 |
|
| 285 |
do_notify_connector_card_webhook(module, webhook, payload) |
| 286 |
|
| 287 |
|
| 288 |
if __name__ == "__main__": |
| 289 |
main() |