webhook_server 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 2.1
2
+ Name: webhook_server
3
+ Version: 0.1.3
4
+ Summary: Create a webhook server with Flask to forward Datadog alerts to Telegram
5
+ Home-page: https://gitlab.com/daisukixci/webhook_server
6
+ License: MIT
7
+ Keywords: datadog,webhook,telegram
8
+ Author: daisukixci
9
+ Author-email: daisuki@tuxtrooper.com
10
+ Maintainer: daisukixci
11
+ Maintainer-email: daisuki@tuxtrooper.com
12
+ Requires-Python: >=3.10,<4.0
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Requires-Dist: Flask (>=3.0.3,<4.0.0)
19
+ Requires-Dist: Flask-BasicAuth (>=0.2.0,<0.3.0)
20
+ Requires-Dist: Flask-HTTPAuth (>=4.8.0,<5.0.0)
21
+ Requires-Dist: requests (>=2.32.3,<3.0.0)
22
+ Project-URL: Documentation, https://gitlab.com/daisukixci/webhook_server/-/blob/main/README.md
23
+ Project-URL: Repository, https://gitlab.com/daisukixci/webhook_server
24
+ Description-Content-Type: text/markdown
25
+
26
+ # Datadog to Telegram webhook
27
+ Provides a basic authenticated webhook to forward alerts from Datadog to Telegram
28
+ ## Requirements
29
+ - A Datadog account
30
+ - A Telegram account
31
+
32
+ ### Quickstart
33
+ #### Datadog
34
+ TODO
35
+ #### Telegram
36
+ TODO
37
+ #### Webhook server
38
+ TODO
39
+
40
+ ### Documentation
41
+
42
+ ### Contribute
43
+ Open to any PR/comments/issues
44
+
45
+ ## Licence
46
+ Copyright 2023 DaisukiXCI
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53
+
@@ -0,0 +1,27 @@
1
+ # Datadog to Telegram webhook
2
+ Provides a basic authenticated webhook to forward alerts from Datadog to Telegram
3
+ ## Requirements
4
+ - A Datadog account
5
+ - A Telegram account
6
+
7
+ ### Quickstart
8
+ #### Datadog
9
+ TODO
10
+ #### Telegram
11
+ TODO
12
+ #### Webhook server
13
+ TODO
14
+
15
+ ### Documentation
16
+
17
+ ### Contribute
18
+ Open to any PR/comments/issues
19
+
20
+ ## Licence
21
+ Copyright 2023 DaisukiXCI
22
+
23
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
24
+
25
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
26
+
27
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ [tool.poetry]
2
+ name = "webhook_server"
3
+ version = "0.1.3"
4
+ description = "Create a webhook server with Flask to forward Datadog alerts to Telegram"
5
+ authors = ["daisukixci <daisuki@tuxtrooper.com>"]
6
+ maintainers = ["daisukixci <daisuki@tuxtrooper.com>"]
7
+ license = "MIT"
8
+ readme = "README.md"
9
+ homepage = "https://gitlab.com/daisukixci/webhook_server"
10
+ repository = "https://gitlab.com/daisukixci/webhook_server"
11
+ documentation = "https://gitlab.com/daisukixci/webhook_server/-/blob/main/README.md"
12
+ keywords = ["datadog", "webhook", "telegram"]
13
+
14
+
15
+ [tool.poetry.dependencies]
16
+ python = "^3.10"
17
+ Flask = "^3.0.3"
18
+ Flask-HTTPAuth = "^4.8.0"
19
+ Flask-BasicAuth = "^0.2.0"
20
+ requests = "^2.32.3"
21
+
22
+ [tool.poetry.group.dev.dependencies]
23
+ pytest = "^8.3.3"
24
+ pytest-cov = "^5.0.0"
25
+ pre-commit = "^4.0.1"
26
+ flake8 = "^7.1.1"
27
+ mypy = "^1.12.0"
28
+ isort = "^5.13.2"
29
+ black = "^24.10.0"
30
+
31
+ [build-system]
32
+ requires = ["poetry-core>=1.0.0"]
33
+ build-backend = "poetry.core.masonry.api"
@@ -0,0 +1,54 @@
1
+ import os
2
+ from flask import Flask
3
+ from flask_basicauth import BasicAuth
4
+
5
+ __version__ = "0.1.0"
6
+
7
+
8
+ def _get_secret(
9
+ name: str,
10
+ default: str = "",
11
+ getenv: bool = True,
12
+ secrets_dir: str = "/var/run/secrets",
13
+ ) -> str:
14
+ """
15
+ This function fetches a docker secret
16
+
17
+ :param name: the name of the docker secret
18
+ :param default: the default value if no secret found
19
+ :param getenv: if environment variable should be fetched as fallback
20
+ :param secrets_dir: the directory where the secrets are stored
21
+ :returns: docker secret or environment variable depending on params
22
+ """
23
+ name_secret = name.lower()
24
+ name_env = name.upper()
25
+
26
+ # initialize value
27
+ value = ""
28
+
29
+ # try to read from secret file
30
+ try:
31
+ with open(
32
+ os.path.join(secrets_dir, name_secret), "r", encoding="UTF-8"
33
+ ) as secret_file:
34
+ value = secret_file.read().strip()
35
+ except IOError:
36
+ # try to read from env if enabled
37
+ if getenv:
38
+ value = os.environ.get(name_env)
39
+
40
+ # set default value if no value found
41
+ if not value:
42
+ value = default
43
+
44
+ return value
45
+
46
+
47
+ app = Flask(__name__)
48
+ app.secret_key = _get_secret("WEBHOOK_FLASK_SECRET_KEY")
49
+ app.config["BASIC_AUTH_USERNAME"] = _get_secret("WEBHOOK_BASIC_AUTH_USERNAME")
50
+ app.config["BASIC_AUTH_PASSWORD"] = _get_secret("WEBHOOK_BASIC_AUTH_PASSWORD")
51
+ app.config["TELEGRAM_BOT_API_TOKEN"] = _get_secret("WEBHOOK_TELEGRAM_BOT_API_TOKEN")
52
+ basic_auth = BasicAuth(app)
53
+
54
+ from . import webhooks
@@ -0,0 +1,28 @@
1
+ from __future__ import annotations
2
+
3
+
4
+ class DatadogAlert:
5
+ def __init__(self, content: dict, fields: list | None = None):
6
+ if not fields:
7
+ self._fields = [
8
+ "title",
9
+ "event_type",
10
+ "body",
11
+ "date",
12
+ "org.id",
13
+ "org.name",
14
+ "id",
15
+ "last_updated",
16
+ ]
17
+ self.alert_message = self._format_content(content)
18
+
19
+ def _format_content(self, content: dict) -> str:
20
+ message = ""
21
+ for field in self._fields:
22
+ message = f"{message}\n<b>{field.title()}</b>:{deref_multi(content, field)}"
23
+ return message
24
+
25
+
26
+ def deref_multi(data: dict, ref: str):
27
+ keys = ref.split(".") if ref else []
28
+ return deref_multi(data[keys[0]], ".".join(keys[1:])) if keys else data
@@ -0,0 +1,31 @@
1
+ import requests
2
+
3
+ from flask import Response, request, jsonify
4
+ from . import app, basic_auth
5
+ from .datadog import DatadogAlert
6
+
7
+
8
+ @app.route("/")
9
+ def ping() -> Response:
10
+ return jsonify({"status": "ok"})
11
+
12
+
13
+ @app.route("/telegram/<chat_id>", methods=["POST"])
14
+ @basic_auth.required
15
+ def webhookHandler(chat_id) -> Response:
16
+ content = request.get_json()
17
+ text = DatadogAlert(content).alert_message
18
+
19
+ url = (
20
+ "https://api.telegram.org/bot"
21
+ + app.config.get("TELEGRAM_BOT_API_TOKEN", "")
22
+ + "/sendMessage"
23
+ )
24
+ payload = {
25
+ "chat_id": chat_id,
26
+ "text": text,
27
+ "parse_mode": "html",
28
+ }
29
+ req = requests.post(url=url, data=payload, timeout=5)
30
+ telegram_response = req.json()
31
+ return jsonify(telegram_response)