forked from saltstack/salt
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To send a message directly to google chat via the api (not via a bot) you need to configure a webhook and post directly to that webhook url.
- Loading branch information
1 parent
c034a86
commit 969d7d6
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,7 @@ execution modules | |
glanceng | ||
glusterfs | ||
gnomedesktop | ||
google_chat | ||
gpg | ||
grafana4 | ||
grains | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
======================== | ||
salt.modules.google_chat | ||
======================== | ||
|
||
.. automodule:: salt.modules.google_chat | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
Module for sending messages to google chat. | ||
.. versionadded:: Fluorine | ||
To use this module you need to configure a webhook in the google chat room | ||
where you would like the message to be sent, see: | ||
https://developers.google.com/hangouts/chat/how-tos/webhooks | ||
''' | ||
# Import Python Libs | ||
from __future__ import absolute_import, print_function, unicode_literals | ||
import json | ||
|
||
|
||
# ------------------------------------------------------------------------------ | ||
# module properties | ||
# ------------------------------------------------------------------------------ | ||
|
||
__virtualname__ = 'google_chat' | ||
|
||
# ------------------------------------------------------------------------------ | ||
# property functions | ||
# ------------------------------------------------------------------------------ | ||
|
||
|
||
def __virtual__(): | ||
return __virtualname__ | ||
|
||
# ------------------------------------------------------------------------------ | ||
# callable functions | ||
# ------------------------------------------------------------------------------ | ||
|
||
|
||
def send_message(url, message): | ||
''' | ||
Send a message to the google chat room specified in the webhook url. | ||
.. code-block:: bash | ||
salt '*' google_chat.send_message "https://chat.googleapis.com/v1/spaces/example_space/messages?key=example_key" "This is a test message" | ||
''' | ||
headers = {'Content-Type': 'application/json'} | ||
data = {'text': message} | ||
result = __utils__['http.query'](url, | ||
'POST', | ||
data=json.dumps(data), | ||
header_dict=headers, | ||
decode=True, | ||
status=True) | ||
|
||
if result.get('status', 0) == 200: | ||
return True | ||
else: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
Test the Google Chat Execution module. | ||
''' | ||
from __future__ import absolute_import, print_function, unicode_literals | ||
|
||
# Import Salt Testing libs | ||
from tests.support.mixins import LoaderModuleMockMixin | ||
from tests.support.unit import skipIf, TestCase | ||
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch | ||
|
||
# Import Salt Libs | ||
import salt.modules.google_chat as gchat | ||
|
||
|
||
def mocked_http_query(url, method, **kwargs): # pylint: disable=unused-argument | ||
''' | ||
Mocked data for test_send_message_success | ||
''' | ||
return {'status': 200, | ||
'dict': None} | ||
|
||
|
||
def mocked_http_query_failure(url, method, **kwargs): # pylint: disable=unused-argument | ||
''' | ||
Mocked data for test_send_message_failure | ||
''' | ||
return {'status': 522, | ||
'dict': None} | ||
|
||
|
||
@skipIf(NO_MOCK, NO_MOCK_REASON) | ||
class TestModulesCfutils(TestCase, LoaderModuleMockMixin): | ||
''' | ||
Test cases for salt.modules.google_chat | ||
''' | ||
def setup_loader_modules(self): | ||
return {gchat: {}} | ||
|
||
def test_send_message_success(self): | ||
''' | ||
Testing a successful message | ||
''' | ||
with patch.dict(gchat.__utils__, {'http.query': mocked_http_query}): # pylint: disable=no-member | ||
self.assertTrue(gchat.send_message('https://example.com', 'Yupiii')) | ||
|
||
def test_send_message_failure(self): | ||
''' | ||
Testing a failed message | ||
''' | ||
with patch.dict(gchat.__utils__, {'http.query': mocked_http_query_failure}): # pylint: disable=no-member | ||
self.assertFalse(gchat.send_message('https://example.com', 'Yupiii')) |