You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
841 B

  1. from django.conf import settings
  2. from django.core.management.base import BaseCommand
  3. from django.urls import reverse
  4. import requests
  5. SETWEBHOOK_TMPL = "https://api.telegram.org/bot%s/setWebhook"
  6. class Command(BaseCommand):
  7. help = "Set up telegram bot's webhook address"
  8. def handle(self, *args, **options):
  9. if settings.TELEGRAM_TOKEN is None:
  10. return "Abort: settings.TELEGRAM_TOKEN is not set"
  11. form = {
  12. "url": settings.SITE_ROOT + reverse("hc-telegram-webhook"),
  13. "allowed_updates": ["message"],
  14. }
  15. url = SETWEBHOOK_TMPL % settings.TELEGRAM_TOKEN
  16. r = requests.post(url, json=form)
  17. if r.status_code != 200:
  18. return "Fail: status=%d, %s" % (r.status_code, r.content)
  19. return "Done, Telegram's webhook set to: %s" % form["url"]