| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2021, Christian Wollinger <[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 |
module: discord |
| 11 |
short_description: Send Discord messages |
| 12 |
version_added: 3.1.0 |
| 13 |
description: |
| 14 |
- Sends a message to a Discord channel using the Discord webhook API. |
| 15 |
author: Christian Wollinger (@cwollinger) |
| 16 |
seealso: |
| 17 |
- name: API documentation |
| 18 |
description: Documentation for Discord API. |
| 19 |
link: https://discord.com/developers/docs/resources/webhook#execute-webhook |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._attributes |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
webhook_id: |
| 29 |
description: |
| 30 |
- The webhook ID. |
| 31 |
- 'Format from Discord webhook URL: C(/webhooks/{webhook.id}/{webhook.token}).' |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
webhook_token: |
| 35 |
description: |
| 36 |
- The webhook token. |
| 37 |
- 'Format from Discord webhook URL: C(/webhooks/{webhook.id}/{webhook.token}).' |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
content: |
| 41 |
description: |
| 42 |
- Content of the message to the Discord channel. |
| 43 |
- At least one of O(content) and O(embeds) must be specified. |
| 44 |
type: str |
| 45 |
username: |
| 46 |
description: |
| 47 |
- Overrides the default username of the webhook. |
| 48 |
type: str |
| 49 |
avatar_url: |
| 50 |
description: |
| 51 |
- Overrides the default avatar of the webhook. |
| 52 |
type: str |
| 53 |
tts: |
| 54 |
description: |
| 55 |
- Set this to V(true) if this is a TTS (Text to Speech) message. |
| 56 |
type: bool |
| 57 |
default: false |
| 58 |
embeds: |
| 59 |
description: |
| 60 |
- Send messages as Embeds to the Discord channel. |
| 61 |
- Embeds can have a colored border, embedded images, text fields and more. |
| 62 |
- 'Allowed parameters are described in the Discord Docs: U(https://discord.com/developers/docs/resources/channel#embed-object).' |
| 63 |
- At least one of O(content) and O(embeds) must be specified. |
| 64 |
type: list |
| 65 |
elements: dict |
| 66 |
""" |
| 67 |
|
| 68 |
EXAMPLES = r""" |
| 69 |
- name: Send a message to the Discord channel |
| 70 |
community.general.discord: |
| 71 |
webhook_id: "00000" |
| 72 |
webhook_token: "XXXYYY" |
| 73 |
content: "This is a message from ansible" |
| 74 |
|
| 75 |
- name: Send a message to the Discord channel with specific username and avatar |
| 76 |
community.general.discord: |
| 77 |
webhook_id: "00000" |
| 78 |
webhook_token: "XXXYYY" |
| 79 |
content: "This is a message from ansible" |
| 80 |
username: Ansible |
| 81 |
avatar_url: "https://docs.ansible.com/favicon/favicon-32x32.png" |
| 82 |
|
| 83 |
- name: Send a embedded message to the Discord channel |
| 84 |
community.general.discord: |
| 85 |
webhook_id: "00000" |
| 86 |
webhook_token: "XXXYYY" |
| 87 |
embeds: |
| 88 |
- title: "Embedded message" |
| 89 |
description: "This is an embedded message" |
| 90 |
footer: |
| 91 |
text: "Author: Ansible" |
| 92 |
image: |
| 93 |
url: "https://docs.ansible.com/favicon/favicon-32x32.png" |
| 94 |
|
| 95 |
- name: Send two embedded messages |
| 96 |
community.general.discord: |
| 97 |
webhook_id: "00000" |
| 98 |
webhook_token: "XXXYYY" |
| 99 |
embeds: |
| 100 |
- title: "First message" |
| 101 |
description: "This is my first embedded message" |
| 102 |
footer: |
| 103 |
text: "Author: Ansible" |
| 104 |
image: |
| 105 |
url: "https://docs.ansible.com/favicon/favicon-32x32.png" |
| 106 |
- title: "Second message" |
| 107 |
description: "This is my first second message" |
| 108 |
footer: |
| 109 |
text: "Author: Ansible" |
| 110 |
icon_url: "https://docs.ansible.com/favicon/favicon-32x32.png" |
| 111 |
fields: |
| 112 |
- name: "Field 1" |
| 113 |
value: "Value of my first field" |
| 114 |
- name: "Field 2" |
| 115 |
value: "Value of my second field" |
| 116 |
timestamp: "{{ ansible_date_time.iso8601 }}" |
| 117 |
""" |
| 118 |
|
| 119 |
RETURN = r""" |
| 120 |
http_code: |
| 121 |
description: |
| 122 |
- Response Code returned by Discord API. |
| 123 |
returned: always |
| 124 |
type: int |
| 125 |
sample: 204 |
| 126 |
""" |
| 127 |
|
| 128 |
from ansible.module_utils.basic import AnsibleModule |
| 129 |
from ansible.module_utils.urls import fetch_url |
| 130 |
|
| 131 |
|
| 132 |
def discord_check_mode(module): |
| 133 |
webhook_id = module.params["webhook_id"] |
| 134 |
webhook_token = module.params["webhook_token"] |
| 135 |
|
| 136 |
headers = {"content-type": "application/json"} |
| 137 |
|
| 138 |
url = f"https://discord.com/api/webhooks/{webhook_id}/{webhook_token}" |
| 139 |
|
| 140 |
response, info = fetch_url(module, url, method="GET", headers=headers) |
| 141 |
return response, info |
| 142 |
|
| 143 |
|
| 144 |
def discord_text_msg(module): |
| 145 |
webhook_id = module.params["webhook_id"] |
| 146 |
webhook_token = module.params["webhook_token"] |
| 147 |
content = module.params["content"] |
| 148 |
user = module.params["username"] |
| 149 |
avatar_url = module.params["avatar_url"] |
| 150 |
tts = module.params["tts"] |
| 151 |
embeds = module.params["embeds"] |
| 152 |
|
| 153 |
headers = {"content-type": "application/json"} |
| 154 |
|
| 155 |
url = f"https://discord.com/api/webhooks/{webhook_id}/{webhook_token}" |
| 156 |
|
| 157 |
payload = { |
| 158 |
"content": content, |
| 159 |
"username": user, |
| 160 |
"avatar_url": avatar_url, |
| 161 |
"tts": tts, |
| 162 |
"embeds": embeds, |
| 163 |
} |
| 164 |
|
| 165 |
payload = module.jsonify(payload) |
| 166 |
|
| 167 |
response, info = fetch_url(module, url, data=payload, headers=headers, method="POST") |
| 168 |
return response, info |
| 169 |
|
| 170 |
|
| 171 |
def main(): |
| 172 |
module = AnsibleModule( |
| 173 |
argument_spec=dict( |
| 174 |
webhook_id=dict(type="str", required=True), |
| 175 |
webhook_token=dict(type="str", required=True, no_log=True), |
| 176 |
content=dict(type="str"), |
| 177 |
username=dict(type="str"), |
| 178 |
avatar_url=dict(type="str"), |
| 179 |
tts=dict(type="bool", default=False), |
| 180 |
embeds=dict(type="list", elements="dict"), |
| 181 |
), |
| 182 |
required_one_of=[["content", "embeds"]], |
| 183 |
supports_check_mode=True, |
| 184 |
) |
| 185 |
|
| 186 |
if module.check_mode: |
| 187 |
response, info = discord_check_mode(module) |
| 188 |
if info["status"] != 200: |
| 189 |
try: |
| 190 |
module.fail_json( |
| 191 |
http_code=info["status"], msg=info["msg"], response=module.from_json(info["body"]), info=info |
| 192 |
) |
| 193 |
except Exception: |
| 194 |
module.fail_json(http_code=info["status"], msg=info["msg"], info=info) |
| 195 |
else: |
| 196 |
module.exit_json( |
| 197 |
msg=info["msg"], changed=False, http_code=info["status"], response=module.from_json(response.read()) |
| 198 |
) |
| 199 |
else: |
| 200 |
response, info = discord_text_msg(module) |
| 201 |
if info["status"] != 204: |
| 202 |
try: |
| 203 |
module.fail_json( |
| 204 |
http_code=info["status"], msg=info["msg"], response=module.from_json(info["body"]), info=info |
| 205 |
) |
| 206 |
except Exception: |
| 207 |
module.fail_json(http_code=info["status"], msg=info["msg"], info=info) |
| 208 |
else: |
| 209 |
module.exit_json(msg=info["msg"], changed=True, http_code=info["status"]) |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == "__main__": |
| 213 |
main() |