healthchecks_healthchecks/hc/api/management/commands/settelegramwebhook.py
Pēteris Caune 52ab8c4703
Improve type hints in hc.lib.curl and hc.api.transports
This results in changes in other places too:

* curl.post() does not accept `data` as positional arg,
  it must now be a keyword argument
* we need asserts and if clauses in a few places to make sure
  we are not passing `None` in the arguments to hc.lib.curl.request
2023-10-20 19:11:08 +03:00

33 lines
935 B
Python

from __future__ import annotations
from typing import Any
from django.conf import settings
from django.core.management.base import BaseCommand
from django.urls import reverse
from hc.lib import curl
SETWEBHOOK_TMPL = "https://api.telegram.org/bot%s/setWebhook"
class Command(BaseCommand):
help = "Set up telegram bot's webhook address"
def handle(self, **options: Any) -> str:
if settings.TELEGRAM_TOKEN is None:
return "Abort: settings.TELEGRAM_TOKEN is not set"
form = {
"url": settings.SITE_ROOT + reverse("hc-telegram-webhook"),
"allowed_updates": ["message", "channel_post"],
}
url = SETWEBHOOK_TMPL % settings.TELEGRAM_TOKEN
r = curl.post(url, json=form)
if r.status_code != 200:
return "Fail: status=%d, %s" % (r.status_code, r.content.decode())
return "Done, Telegram's webhook set to: %s" % form["url"]