Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
54858279
Commit
54858279
authored
Jan 25, 2014
by
someda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add typetalk notification module
parent
30948ae0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
116 additions
and
0 deletions
+116
-0
library/notification/typetalk
+116
-0
No files found.
library/notification/typetalk
0 → 100644
View file @
54858279
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION
=
'''
---
module: typetalk
version_added: "1.5"
short_description: Send a message to typetalk
description:
- Send a message to typetalk using typetalk API ( http://developers.typetalk.in/ )
options:
client_id:
description:
- OAuth2 client ID
required: true
client_secret:
description:
- OAuth2 client secret
required: true
topic:
description:
- topic id to post message
required: true
msg:
description:
- message body
required: true
requirements: [ urllib, urllib2, json ]
author: Takashi Someda <someda@isenshi.com>
'''
EXAMPLES
=
'''
- typetalk: client_id=12345 client_secret=12345 topic=1 msg="install completed"
'''
try
:
import
urllib
except
ImportError
:
urllib
=
None
try
:
import
urllib2
except
ImportError
:
urllib2
=
None
try
:
import
json
except
ImportError
:
json
=
None
def
do_request
(
url
,
params
,
headers
=
{}):
data
=
urllib
.
urlencode
(
params
)
headers
=
dict
(
headers
,
**
{
'User-Agent'
:
'Ansible/typetalk module'
,
})
return
urllib2
.
urlopen
(
urllib2
.
Request
(
url
,
data
,
headers
))
def
get_access_token
(
client_id
,
client_secret
):
params
=
{
'client_id'
:
client_id
,
'client_secret'
:
client_secret
,
'grant_type'
:
'client_credentials'
,
'scope'
:
'topic.post'
}
res
=
do_request
(
'https://typetalk.in/oauth2/access_token'
,
params
)
return
json
.
load
(
res
)[
'access_token'
]
def
send_message
(
client_id
,
client_secret
,
topic
,
msg
):
"""
send message to typetalk
"""
try
:
access_token
=
get_access_token
(
client_id
,
client_secret
)
url
=
'https://typetalk.in/api/v1/topics/
%
d'
%
topic
headers
=
{
'Authorization'
:
'Bearer
%
s'
%
access_token
,
}
do_request
(
url
,
{
'message'
:
msg
},
headers
)
return
True
,
{
'access_token'
:
access_token
}
except
urllib2
.
HTTPError
,
e
:
return
False
,
e
def
main
():
module
=
AnsibleModule
(
argument_spec
=
dict
(
client_id
=
dict
(
required
=
True
),
client_secret
=
dict
(
required
=
True
),
topic
=
dict
(
required
=
True
,
type
=
'int'
),
msg
=
dict
(
required
=
True
),
),
supports_check_mode
=
False
)
if
not
(
urllib
and
urllib2
and
json
):
module
.
fail_json
(
msg
=
"urllib, urllib2 and json modules are required"
)
client_id
=
module
.
params
[
"client_id"
]
client_secret
=
module
.
params
[
"client_secret"
]
topic
=
module
.
params
[
"topic"
]
msg
=
module
.
params
[
"msg"
]
res
,
error
=
send_message
(
client_id
,
client_secret
,
topic
,
msg
)
if
not
res
:
module
.
fail_json
(
msg
=
'fail to send message with response code
%
s'
%
error
.
code
)
module
.
exit_json
(
changed
=
True
,
topic
=
topic
,
msg
=
msg
)
# import module snippets
from
ansible.module_utils.basic
import
*
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment