From a569dde23b94c7722e3a627ef9da790a8446c257 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 16 Aug 2023 17:21:32 +0200 Subject: [PATCH] announce on telegram --- app/item/tasks.py | 23 ++++++++++++++++------- app/settings/common.py | 1 + app/telegrambot/__init__.py | 0 app/telegrambot/admin.py | 3 +++ app/telegrambot/apps.py | 6 ++++++ app/telegrambot/migrations/__init__.py | 0 app/telegrambot/models.py | 3 +++ app/telegrambot/rpc.py | 26 ++++++++++++++++++++++++++ app/telegrambot/tests.py | 3 +++ app/telegrambot/views.py | 3 +++ requirements.txt | 1 + 11 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 app/telegrambot/__init__.py create mode 100644 app/telegrambot/admin.py create mode 100644 app/telegrambot/apps.py create mode 100644 app/telegrambot/migrations/__init__.py create mode 100644 app/telegrambot/models.py create mode 100644 app/telegrambot/rpc.py create mode 100644 app/telegrambot/tests.py create mode 100644 app/telegrambot/views.py diff --git a/app/item/tasks.py b/app/item/tasks.py index b6562c2..06f29d6 100644 --- a/app/item/tasks.py +++ b/app/item/tasks.py @@ -30,13 +30,22 @@ def announce_items(): f.write(r.content) f.close() image = f.name - r = rpc.send( - message, group=settings.SIGNAL_ANNOUNCE_GROUP, - preview_url=link, - preview_title=item.title, - preview_image=image, - preview_description=description, - ) + if getattr(settings, "SIGNAL_ANNOUNCE_GROUP"): + r = rpc.send( + message, + group=settings.SIGNAL_ANNOUNCE_GROUP, + preview_url=link, + preview_title=item.title, + preview_image=image, + preview_description=description, + ) + if getattr(settings, "TELEGRAM_ANNOUNCE_CHANNEL"): + message = f'{item.title}' + r = telegram_rpc.send( + message, + channel=settings.TELEGRAM_ANNOUNCE_CHANNEL, + preview_image=image, + ) item.save() if image: os.unlink(f.name) diff --git a/app/settings/common.py b/app/settings/common.py index eed7845..68df382 100644 --- a/app/settings/common.py +++ b/app/settings/common.py @@ -60,6 +60,7 @@ INSTALLED_APPS = [ "app.user", "app.item", "app.signalbot", + "app.telegrambot", "app.page", ] diff --git a/app/telegrambot/__init__.py b/app/telegrambot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/telegrambot/admin.py b/app/telegrambot/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/telegrambot/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/telegrambot/apps.py b/app/telegrambot/apps.py new file mode 100644 index 0000000..73fcdbb --- /dev/null +++ b/app/telegrambot/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class TelegrambotConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "app.telegrambot" diff --git a/app/telegrambot/migrations/__init__.py b/app/telegrambot/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/telegrambot/models.py b/app/telegrambot/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/telegrambot/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/telegrambot/rpc.py b/app/telegrambot/rpc.py new file mode 100644 index 0000000..899cda3 --- /dev/null +++ b/app/telegrambot/rpc.py @@ -0,0 +1,26 @@ +import asyncio + +import telegram + +from django.conf import settings + + +def send(msg, to=None, channel=None, preview_url=None, preview_title=None, preview_description=None, preview_image=None): + bot = telegram.Bot(token=settings.TELEGRAM_TOKEN) + if preview_image: + asyncio.run(bot.send_photo( + chat_id=channel, + caption=msg, + photo=preview_image, + parse_mode='html' + )) + else: + asyncio.run(bot.send_message( + chat_id=channel, + text=msg, + parse_mode='html' + )) + + +def send_reaction(target_address, target_ts, emoji, to=None, group=None, remove=False): + pass diff --git a/app/telegrambot/tests.py b/app/telegrambot/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/telegrambot/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/telegrambot/views.py b/app/telegrambot/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/app/telegrambot/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/requirements.txt b/requirements.txt index ecb4f4c..cc954f7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ django-celery-results django-celery-beat redis django-brake +python-telegram-bot