#!/usr/bin/python# -*- coding: utf-8 -*-DOCUMENTATION='''---module: hipchatversion_added: "1.2"short_description: Send a message to hipchatdescription: - Send a message to hipchatoptions: token: description: - API token. required: true room: description: - ID or name of the room. required: true from: description: - Name the message will appear be sent from. max 15 characters. Over 15, will be shorten. required: false default: Ansible msg: description: - The message body. required: true default: null color: description: - Background color for the message. Default is yellow. required: false default: yellow choices: [ "yellow", "red", "green", "purple", "gray", "random" ] msg_format: description: - message format. html or text. Default is text. required: false default: text choices: [ "text", "html" ] notify: description: - notify or not (change the tab color, play a sound, etc) required: false default: 'yes' choices: [ "yes", "no" ] validate_certs: description: - If C(no), SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates. required: false default: 'yes' choices: ['yes', 'no'] version_added: 1.5.1 api: description: - API url if using a self-hosted hipchat server required: false default: 'https://api.hipchat.com/v1' version_added: 1.6.0# informational: requirements for nodesrequirements: [ urllib, urllib2 ]author: "WAKAYAMA Shirou (@shirou), BOURDEL Paul"'''EXAMPLES='''- hipchat: token=AAAAAA room=notify msg="Ansible task finished"'''# ===========================================# HipChat module specific support methods.#DEFAULT_URI="https://api.hipchat.com/v1"MSG_URI_V1="/rooms/message"MSG_URI_V2="/room/{id_or_name}/message"NOTIFY_URI_V2="/room/{id_or_name}/notification"defsend_msg_v1(module,token,room,msg_from,msg,msg_format='text',color='yellow',notify=False,api=MSG_URI_V1):'''sending message to hipchat v1 server'''print"Sending message to v1 server"params={}params['room_id']=roomparams['from']=msg_from[:15]# max length is 15params['message']=msgparams['message_format']=msg_formatparams['color']=colorparams['api']=apiifnotify:params['notify']=1else:params['notify']=0url=api+MSG_URI_V1+"?auth_token=%s"%(token)data=urllib.urlencode(params)ifmodule.check_mode:# In check mode, exit before actually sending the messagemodule.exit_json(changed=False)response,info=fetch_url(module,url,data=data)ifinfo['status']==200:returnresponse.read()else:module.fail_json(msg="failed to send message, return status=%s"%str(info['status']))defsend_msg_v2(module,token,room,msg_from,msg,msg_format='text',color='yellow',notify=False,api=MSG_URI_V2):'''sending message to hipchat v2 server'''print"Sending message to v2 server"headers={'Authorization':'Bearer %s'%token,'Content-Type':'application/json'}body=dict()body['message']=msgbody['color']=colorbody['message_format']=msg_formatifnotify:POST_URL=api+NOTIFY_URI_V2else:POST_URL=api+MSG_URI_V2url=POST_URL.replace('{id_or_name}',room)data=json.dumps(body)ifmodule.check_mode:# In check mode, exit before actually sending the messagemodule.exit_json(changed=False)response,info=fetch_url(module,url,data=data,headers=headers,method='POST')ifinfo['status']==200:returnresponse.read()else:module.fail_json(msg="failed to send message, return status=%s"%str(info['status']))# ===========================================# Module execution.#defmain():module=Ansible