send announcements

This commit is contained in:
j 2023-07-25 10:33:50 +01:00
parent 6bbe5bbd44
commit 17887c05fd
2 changed files with 42 additions and 3 deletions

View file

@ -1,14 +1,45 @@
import os
import tempfile
import requests
from celery.schedules import crontab
from django.conf import settings
from django.utils import timezone
from ..signalbot import rpc
from ..celery import app
from . import models
@app.task(queue="default")
def announce_items():
pass
if not getattr(settings, 'SIGNAL_ANNOUNCE_GROUP'):
return
now = timezone.now()
qs = models.Item.objects.exclude(published=None).filter(announced=None).filter(published__lte=now).order_by('published')
for item in qs:
item.announced = now
link = settings.BASE_URL + item.get_absolute_url()
message = link
image = item.data.get('thumbnail')
description = ""
if image:
f = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
r = requests.get(image)
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,
)
item.save()
if image:
os.unlink(f.name)
@app.task(queue="default")

View file

@ -29,7 +29,7 @@ def api(method, params):
raise Exception("Error: %s", response)
def send(msg, to=None, group=None):
def send(msg, to=None, group=None, preview_url=None, preview_title=None, preview_description=None, preview_image=None):
params = {
"message": msg
}
@ -37,6 +37,14 @@ def send(msg, to=None, group=None):
params["groupId"] = group
else:
params["recipient"] = to
if preview_url:
params['previewUrl'] = preview_url
if preview_title:
params['previewTitle'] = preview_title
if preview_description:
params['previewDescription'] = preview_description
if preview_image:
params['previewImage'] = preview_image
return api("send", params)