
    Vh"                     p    d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ dZd Zd	 Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: rocketchat
short_description: Send notifications to Rocket Chat
description:
  - This module sends notifications to Rocket Chat through the Incoming WebHook integration.
author: "Ramon de la Fuente (@ramondelafuente)"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  domain:
    type: str
    description:
      - The domain for your environment without protocol. (For example V(example.com) or V(chat.example.com)).
    required: true
  token:
    type: str
    description:
      - Rocket Chat Incoming Webhook integration token. This provides authentication to Rocket Chat's Incoming webhook for
        posting messages.
    required: true
  protocol:
    type: str
    description:
      - Specify the protocol used to send notification messages before the webhook URL (that is, V(http) or V(https)).
    default: https
    choices:
      - 'http'
      - 'https'
  msg:
    type: str
    description:
      - Message to be sent.
  channel:
    type: str
    description:
      - Channel to send the message to. If absent, the message goes to the channel selected for the O(token) specified during
        the creation of webhook.
  username:
    type: str
    description:
      - This is the sender of the message.
    default: "Ansible"
  icon_url:
    type: str
    description:
      - URL for the message sender's icon.
    default: "https://docs.ansible.com/favicon.ico"
  icon_emoji:
    type: str
    description:
      - Emoji for the message sender. The representation for the available emojis can be got from Rocket Chat.
      - For example V(:thumbsup:).
      - If O(icon_emoji) is set, O(icon_url) will not be used.
  link_names:
    type: int
    description:
      - Automatically create links for channels and usernames in O(msg).
    default: 1
    choices:
      - 1
      - 0
  validate_certs:
    description:
      - If V(false), SSL certificates will not be validated. This should only be used on personally controlled sites using
        self-signed certificates.
    type: bool
    default: true
  color:
    type: str
    description:
      - Allow text to use default colors - use the default of V(normal) to not send a custom color bar at the start of the
        message.
    default: 'normal'
    choices:
      - 'normal'
      - 'good'
      - 'warning'
      - 'danger'
  attachments:
    type: list
    elements: dict
    description:
      - Define a list of attachments.
  is_pre740:
    description:
      - If V(true), the payload matches Rocket.Chat prior to 7.4.0 format.
        This format has been used by the module since its inception, but is no longer supported by Rocket.Chat 7.4.0.
      - The default value of the option will change to V(false) eventually.
      - This parameter will be removed in a future release when Rocket.Chat 7.4.0 becomes the minimum supported version.
    type: bool
    default: true
    version_added: 10.5.0
a  
- name: Send notification message through Rocket Chat
  community.general.rocketchat:
    token: thetoken/generatedby/rocketchat
    domain: chat.example.com
    msg: '{{ inventory_hostname }} completed'
  delegate_to: localhost

- name: Send notification message through Rocket Chat all options
  community.general.rocketchat:
    domain: chat.example.com
    token: thetoken/generatedby/rocketchat
    msg: '{{ inventory_hostname }} completed'
    channel: "#ansible"
    username: 'Ansible on {{ inventory_hostname }}'
    icon_url: http://www.example.com/some-image-file.png
    link_names: 0
  delegate_to: localhost

- name: Insert a color bar in front of the message for visibility purposes and use the default webhook icon and name configured
    in rocketchat
  community.general.rocketchat:
    token: thetoken/generatedby/rocketchat
    domain: chat.example.com
    msg: '{{ inventory_hostname }} is alive!'
    color: good
    username: ''
    icon_url: ''
  delegate_to: localhost

- name: Use the attachments API
  community.general.rocketchat:
    token: thetoken/generatedby/rocketchat
    domain: chat.example.com
    attachments:
      - text: Display my system load on host A and B
        color: "#ff00dd"
        title: System load
        fields:
          - title: System A
            value: 'load average: 0,74, 0,66, 0,63'
            short: true
          - title: System B
            value: 'load average: 5,16, 4,64, 2,43'
            short: true
  delegate_to: localhost
z{
changed:
  description: A flag indicating if any change was made or not.
  returned: success
  type: bool
  sample: false
)AnsibleModule)	fetch_urlz%s://%s/hooks/%sc
                 t   i }
|dk(  r|t        |      }
n|t        t        ||      g      }
||d   dk(  s|d   dk(  r||
d<   nd|z   |
d<   |||
d	<   |||
d
<   n||
d<   |||
d<   |	d|
vrg |
d<   |'|D ]"  }d|vr|d   |d<   |
d   j                  |       $ | j                  |
      }
|	rd|
z   }
|
S )Nnormal)text)r
   color)attachmentsr   #@channelusername
icon_emojiicon_url
link_namesr   fallbackr
   zpayload=)dictappendjsonify)moduler
   r   r   r   r   r   r   r   	is_pre740payload
attachments               p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/rocketchat.pybuild_payload_for_rocketchatr      s   GT-D/		Dd%$@#ABAJ#71:#4!(GI!$wGI&
 *&
 *'%'GM"% 	6J+)3F);
:&M"))*5	6
 nnW%Gw&N    c                     |j                  d      dk  r| j                  d       t        |||fz  }t        | ||      \  }}|d   dk7  r"| j                  dt	        |d         z         y y )	N/   z.Invalid Token specified, provide a valid tokenmsg)datastatus   z(failed to send message, return status=%s)count	fail_jsonROCKETCHAT_INCOMING_WEBHOOKr   str)r   domaintokenprotocolr   rocketchat_incoming_webhookresponseinfos           r   do_notify_rocketchatr1      s}    {{3!MN"=6SX@Y"Yv'BQNHdH~G#dS[nJ]]^ r   c                  J   t        t        t        dd      t        ddd      t        ddddg      t        dd      t        d	      t        dd
      t        dd      t        d	      t        ddddg      t        dd      t        ddg d      t        ddd      t        dd                  } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d    }| j                  d!   }	| j                  d"   }
| j                  d#   }| j                  d$   }t        | ||||||	|
||
      }t	        | ||||       | j                  d%&       y )'Nr*   T)typerequired)r3   r4   no_loghttpshttp)r3   defaultchoicesF)r3   Ansible)r3   r8   z$https://docs.ansible.com/favicon.icointr!   r   bool)r8   r3   r	   )r	   goodwarningdangerlistr   )r3   elementsr4   )r+   r,   r-   r#   r   r   r   r   r   validate_certsr   r   r   )argument_specr+   r,   r-   r#   r   r   r   r   r   r   r   r   OKr"   )r   r   paramsr   r1   	exit_json)r   r+   r,   r-   r
   r   r   r   r   r   r   r   r   r   s                 r   mainrG      s   UT2ED>ug?PQ%%0e$ui8u.TU'Aq6B6:E8=de&6EJ4f5
F$ ]]8$FMM'"E}}Z(H==DmmI&G}}Z(H}}Z(H|,J|,JMM'"E--.Kk*I*64(HV`blns  vA  CL  MG'B
r   __main__N)
__future__r   r   r   r3   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   ansible.module_utils.urlsr   r)   r   r1   rG   __name__ r   r   <module>rR      sa    A @aF.`
 5 / 1 !H	_#L zF r   