
    Vhy                     r    d dl mZmZmZ eZdZdZd dlm	Z	 d dl
mZ d dlmZ 	 ddZd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona!  
module: twilio
short_description: Sends a text message to a mobile phone through Twilio
description:
  - Sends a text message to a phone number through the Twilio messaging API.
notes:
  - This module is non-idempotent because it sends an email through the external API. It is idempotent only in the case that
    the module fails.
  - Like the other notification modules, this one requires an external dependency to work. In this case, you need a Twilio
    account with a purchased or verified phone number to send the text message.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  account_sid:
    type: str
    description:
      - User's Twilio account token found on the account page.
    required: true
  auth_token:
    type: str
    description:
      - User's Twilio authentication token.
    required: true
  msg:
    type: str
    description:
      - The body of the text message.
    required: true
  to_numbers:
    type: list
    elements: str
    description:
      - One or more phone numbers to send the text message to, format C(+15551112222).
    required: true
    aliases: [to_number]
  from_number:
    type: str
    description:
      - The Twilio number to send the text message from, format C(+15551112222).
    required: true
  media_url:
    type: str
    description:
      - A URL with a picture, video or sound clip to send with an MMS (multimedia message) instead of a plain SMS.
    required: false

author: "Matt Makai (@makaimc)"
aS  
# send an SMS about the build status to (555) 303 5681
# note: replace account_sid and auth_token values with your credentials
# and you have to have the 'from_number' on your Twilio account
- name: Send a text message to a mobile phone through Twilio
  community.general.twilio:
    msg: All servers with webserver role are now configured.
    account_sid: ACXXXXXXXXXXXXXXXXX
    auth_token: ACXXXXXXXXXXXXXXXXX
    from_number: "+15552014545"
    to_number: "+15553035681"
  delegate_to: localhost

# send an SMS to multiple phone numbers about the deployment
# note: replace account_sid and auth_token values with your credentials
# and you have to have the 'from_number' on your Twilio account
- name: Send a text message to a mobile phone through Twilio
  community.general.twilio:
    msg: This server configuration is now complete.
    account_sid: ACXXXXXXXXXXXXXXXXX
    auth_token: ACXXXXXXXXXXXXXXXXX
    from_number: "+15553258899"
    to_numbers:
      - "+15551113232"
      - "+12025551235"
      - "+19735559010"
  delegate_to: localhost

# send an MMS to a single recipient with an update on the deployment
# and an image of the results
# note: replace account_sid and auth_token values with your credentials
# and you have to have the 'from_number' on your Twilio account
- name: Send a text message to a mobile phone through Twilio
  community.general.twilio:
    msg: Deployment complete!
    account_sid: ACXXXXXXXXXXXXXXXXX
    auth_token: ACXXXXXXXXXXXXXXXXX
    from_number: "+15552014545"
    to_number: "+15553035681"
    media_url: https://demo.twilio.com/logo.png
  delegate_to: localhost
)AnsibleModule)	urlencode)	fetch_urlNc                     d|d}d}|||d}	|r||	d<   t        |	      }
|ddd}|j                  d	d
      | j                  d<   |j                  d	d
      | j                  d<   t        | ||
|      S )Nz+https://api.twilio.com/2010-04-01/Accounts/z/Messages.jsonAnsible)FromToBodyMediaUrlz!application/x-www-form-urlencodedzapplication/json)z
User-AgentzContent-typeAccept
 url_usernameurl_password)dataheaders)r   replaceparamsr   )moduleaccount_sid
auth_tokenmsgfrom_number	to_number	media_urlURIAGENTr   encoded_datar   s               l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/twilio.pypost_twilio_apir#   u   s     CEy#>D$ZT?L"B+G %0$7$7b$AFMM.!$.$6$6tR$@FMM.!VS|WEE    c                  N   t        t        t        d      t        dd      t        d      t        d      t        ddgdd      t        d d	      
      d      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|D ]U  }t        | ||||||      \  }}	|	d   dvsd}
d|	v r| j	                  |	d         }|d   }
| j                  d|d|
       W | j                  |d       y )NT)required)r&   no_logr   liststr)r&   aliasestypeelementsF)defaultr&   )r   r   r   r   
to_numbersr   )argument_specsupports_check_moder   r   r   r   r.   r   status)      zunknown errorbodymessagezunable to send message to z: )r   )r   changed)r   dictr   r#   	from_json	fail_json	exit_json)r   r   r   r   r   r.   r   numberrinfobody_messager4   s               r"   mainr?      s;   d+T$7d#d+TK=vX]^4%8
 !
F --.K|,J
--
C--.K|,Jk*I ^!&+z3"-vyB4>+*L~''V5#Iv|!\]^ e,r$   __main__)N)
__future__r   r   r   r+   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   +ansible.module_utils.six.moves.urllib.parser   ansible.module_utils.urlsr   r#   r?   __name__ r$   r"   <module>rJ      sS    A @4l)\ 5 A / *.F6-D zF r$   