Skip to content

Commit

Permalink
feat: best comments bot (internal beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
vas3k committed Sep 18, 2023
1 parent 000a750 commit ff8cbb8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions etc/crontab
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0 1 * * * root cd /app && python3 manage.py delete_users >/proc/1/fd/1 2>/proc/1/fd/2
0 3 * * * root cd /app && python3 manage.py cleanup_old_oauth_tokens >/proc/1/fd/1 2>/proc/1/fd/2
0 8 * * 2,3,4,5 root cd /app && python3 manage.py send_daily_digest --production true >/proc/1/fd/1 2>/proc/1/fd/2
30 9,12,15,18,22 * * 1-5 root cd /app && python3 manage.py send_best_comments >/proc/1/fd/1 2>/proc/1/fd/2
0 11 * * 1 root cd /app && python3 manage.py send_weekly_digest --production true >/proc/1/fd/1 2>/proc/1/fd/2
0 5 * * 7 root cd /app && python3 manage.py rebuild_search_index >/proc/1/fd/1 2>/proc/1/fd/2
0 8 * * * root cd /app && python3 manage.py replay_stuck_reviews >/proc/1/fd/1 2>/proc/1/fd/2
Expand Down
52 changes: 52 additions & 0 deletions notifications/management/commands/send_best_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging
from datetime import datetime, timedelta

from django.conf import settings
from django.core.management import BaseCommand
from django.template.loader import render_to_string

from comments.models import Comment
from notifications.telegram.common import send_telegram_message, Chat

log = logging.getLogger(__name__)

TELEGRAM_CHANNEL_ID = -1001814814883
TIME_INTERVAL = timedelta(days=3)
LIMIT = 20
MIN_UPVOTES = 25


class Command(BaseCommand):
help = "Send best comments to the channel"

def handle(self, *args, **options):
best_comments = Comment.visible_objects().filter(
created_at__gte=datetime.utcnow() - TIME_INTERVAL,
post__is_approved_by_moderator=True,
upvotes__gte=MIN_UPVOTES,
).order_by("-upvotes")[:LIMIT]

for comment in best_comments:
if not comment.metadata or not comment.metadata.get("in_best_comments"):
self.stdout.write(f"Comment {comment.id} +{comment.upvotes}")
comment.metadata = comment.metadata or {}
comment.metadata["in_best_comments"] = True
comment.save()

message = render_to_string("messages/best_comments.html", {
"comment": comment,
"settings": settings,
})

try:
send_telegram_message(
chat=Chat(id=TELEGRAM_CHANNEL_ID),
text=message,
disable_preview=False,
)
except Exception as ex:
self.stdout.write(f"Error sending the message: {ex}")

break

self.stdout.write("Done 🥙")
5 changes: 5 additions & 0 deletions notifications/telegram/templates/messages/best_comments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load text_filters %}{% load posts %}Re: <b><a href="{{ settings.APP_HOST }}{% url "show_post" comment.post.type comment.post.slug %}">{% if comment.post.emoji %}{{ comment.post.emoji }} {% endif %}{% if comment.post.prefix %}{{ comment.post.prefix }} {% endif %}{{ comment.post.title }}</a></b> (+{{ comment.upvotes }})

<b>{{ comment.author.slug }}:</b> {% render_plain comment 3000 %}

<a href="{{ settings.APP_HOST }}{% url "show_comment" comment.post.slug comment.id %}#comment-{{ comment.id }}">Посмотреть ➜</a>

0 comments on commit ff8cbb8

Please sign in to comment.