kumify/msgio/gateways/telegram.py
Klaus-Uwe Mitterer 3ae64b5caf Add django-dbsettings requirement
Move Telegram bot token to dbsettings (incl. migration)
2020-12-30 10:33:45 +01:00

32 lines
No EOL
973 B
Python

from django.views.generic import View
from django.dispatch import receiver
import json
import telegram
from dbsettings.functions import dbsettings
from ..signals import send_message
from ..models import GatewayUser
class TelegramWebhookView(View):
def post(self, *args, **kwargs):
pass # TODO: Implement webhook receiver and management tool
class TelegramDispatcher:
def __init__(self, token=dbsettings.TELEGRAM_TOKEN):
self.bot = telegram.Bot(token=token)
def send(self, message, chat_id):
self.bot.sendMessage(chat_id=chat_id, text=message)
@receiver(send_message)
def telegram_sender(sender, **kwargs):
if kwargs["dispatcher"] == "telegram":
notification = kwargs["notification"]
settings = GatewayUser.objects.get(user=notification.recipient, gateway="telegram")
chat_id = settings.gatewayusersetting_set.get(key="chat_id").value
TelegramDispatcher().send(notification.content, chat_id)