2020-12-29 16:26:48 +00:00
|
|
|
from django.views.generic import View
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
import telegram
|
|
|
|
|
2020-12-30 09:33:45 +00:00
|
|
|
from dbsettings.functions import dbsettings
|
|
|
|
|
2020-12-29 16:26:48 +00:00
|
|
|
from ..signals import send_message
|
|
|
|
from ..models import GatewayUser
|
2021-01-02 09:13:07 +00:00
|
|
|
from ..helpers import run_filters
|
2020-12-29 16:26:48 +00:00
|
|
|
|
|
|
|
class TelegramWebhookView(View):
|
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
pass # TODO: Implement webhook receiver and management tool
|
|
|
|
|
|
|
|
class TelegramDispatcher:
|
2020-12-30 09:33:45 +00:00
|
|
|
def __init__(self, token=dbsettings.TELEGRAM_TOKEN):
|
2020-12-29 16:26:48 +00:00
|
|
|
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
|
|
|
|
|
2021-01-02 09:13:07 +00:00
|
|
|
text = run_filters(notification)
|
|
|
|
|
|
|
|
TelegramDispatcher().send(text, chat_id)
|