diff --git a/hc/api/management/commands/settelegramwebhook.py b/hc/api/management/commands/settelegramwebhook.py new file mode 100644 index 00000000..0da3ed19 --- /dev/null +++ b/hc/api/management/commands/settelegramwebhook.py @@ -0,0 +1,24 @@ +from django.conf import settings +from django.core.management.base import BaseCommand +from django.urls import reverse + +import requests + +SETWEBHOOK_TMPL = "https://api.telegram.org/bot%s/setWebhook" + + +class Command(BaseCommand): + help = "Set up telegram bot's webhook address" + + def handle(self, *args, **options): + if settings.TELEGRAM_TOKEN is None: + return "Abort: settings.TELEGRAM_TOKEN is not set" + + form = {"url": settings.SITE_ROOT + reverse("hc-telegram-webhook")} + url = SETWEBHOOK_TMPL % settings.TELEGRAM_TOKEN + r = requests.post(url, data=form) + + if r.status_code != 200: + return "Fail: status=%d, %s" % (r.status_code, r.content) + + return "Done, Telegram's webhook set to: %s" % form["url"] diff --git a/hc/front/urls.py b/hc/front/urls.py index 5f5352df..65938671 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -27,7 +27,7 @@ channel_urls = [ url(r'^add_pushover/$', views.add_pushover, name="hc-add-pushover"), url(r'^add_opsgenie/$', views.add_opsgenie, name="hc-add-opsgenie"), url(r'^add_victorops/$', views.add_victorops, name="hc-add-victorops"), - url(r'^telegram/bot/$', views.telegram_bot), + url(r'^telegram/bot/$', views.telegram_bot, name="hc-telegram-webhook"), url(r'^add_telegram/$', views.add_telegram, name="hc-add-telegram"), url(r'^add_sms/$', views.add_sms, name="hc-add-sms"), url(r'^([\w-]+)/checks/$', views.channel_checks, name="hc-channel-checks"),