kumify/msgio/gateways/telegram.py
Kumi ae97cfa30e
refactor: clean up unused imports and improve code quality
Removed unnecessary imports across various modules to streamline the application's dependencies and improve loading times. Specific changes include the removal of unused Django model and admin imports in several apps, simplifying view imports by eliminating unutilized components, and cleaning up static CSS for better maintainability. Corrections were made to conditional expressions for clearer logic. The removal of the django.test.TestCase import in test files reflects a shift towards a different testing strategy or the current lack of tests. Exception handling has been made more explicit to avoid catching unintended exceptions, paving the way for more robust error handling and logging in the future. Additionally, a new CSS file was added for frontend enhancements, indicating ongoing UI/UX improvements.

These changes collectively aim to make the codebase more maintainable and efficient, reducing clutter and focusing on used functionalities. It's a step towards optimizing the application's performance and ensuring a clean, manageable codebase for future development.
2024-06-02 20:27:02 +02:00

35 lines
1 KiB
Python

from django.views.generic import View
from django.dispatch import receiver
import telegram
from dbsettings.functions import dbsettings
from ..signals import send_message
from ..models import GatewayUser
from ..helpers import run_filters
class TelegramWebhookView(View):
def post(self, *args, **kwargs):
pass # TODO: Implement webhook receiver and management tool
class TelegramDispatcher:
def __init__(self, token=None):
token = token or 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
text = run_filters(notification)
TelegramDispatcher().send(text, chat_id)