mirror of
https://github.com/Xtao-Labs/misskey2telegram.git
synced 2024-11-22 05:53:09 +00:00
fix: announcement
This commit is contained in:
parent
5c737450cd
commit
3091144ba0
@ -1,11 +1,13 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from mipac import Route
|
||||
|
||||
from init import bot
|
||||
from misskey_init import MisskeyBot
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mipac.models.announcement import Announcement
|
||||
|
||||
announcement_template = """<b>Misskey Announcement</b>
|
||||
|
||||
<b>{0}</b>
|
||||
@ -13,28 +15,16 @@ announcement_template = """<b>Misskey Announcement</b>
|
||||
{1}"""
|
||||
|
||||
|
||||
class Announcement:
|
||||
def __init__(self, data, misskey_bot: MisskeyBot):
|
||||
class UnreadAnnouncement:
|
||||
def __init__(self, data: "Announcement", misskey_bot: MisskeyBot):
|
||||
self.misskey_bot = misskey_bot
|
||||
self.id = data["id"]
|
||||
self.title = data["title"]
|
||||
self.text = data["text"]
|
||||
self.is_read = data["is_read"]
|
||||
self.image_url = data["image_url"]
|
||||
self._created_at = data["created_at"]
|
||||
self._updated_at = data["updated_at"]
|
||||
|
||||
@property
|
||||
def created_at(self) -> datetime:
|
||||
return datetime.strptime(self._created_at, "%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
|
||||
@property
|
||||
def updated_at(self) -> Optional[datetime]:
|
||||
return (
|
||||
datetime.strptime(self._updated_at, "%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
if self._updated_at
|
||||
else None
|
||||
)
|
||||
self.id = data.id
|
||||
self.title = data.title
|
||||
self.text = data.text
|
||||
self.is_read = data.is_read
|
||||
self.image_url = data.image_url
|
||||
self.created_at = data.created_at
|
||||
self.updated_at = data.updated_at
|
||||
|
||||
async def send_notice(self):
|
||||
if not self.image_url:
|
||||
@ -67,17 +57,3 @@ class Announcement:
|
||||
auth=True,
|
||||
lower=True,
|
||||
)
|
||||
|
||||
|
||||
async def get_unread_announcements(misskey_bot: MisskeyBot):
|
||||
data = {
|
||||
"limit": 10,
|
||||
"withUnreads": True,
|
||||
}
|
||||
req = await misskey_bot.core.http.request(
|
||||
Route("POST", "/api/announcements"),
|
||||
json=data,
|
||||
auth=True,
|
||||
lower=True,
|
||||
)
|
||||
return [Announcement(i, misskey_bot) for i in req]
|
||||
|
@ -1,14 +0,0 @@
|
||||
from defs.announcement import get_unread_announcements
|
||||
from misskey_init import misskey_bot_map
|
||||
from models.services.scheduler import scheduler
|
||||
|
||||
|
||||
# @scheduler.scheduled_job("interval", minutes=15, id="check_announcement")
|
||||
async def announcement():
|
||||
for bot in misskey_bot_map.values():
|
||||
data = await get_unread_announcements(bot)
|
||||
for an in data:
|
||||
try:
|
||||
await an.send_notice()
|
||||
finally:
|
||||
await an.mark_as_read()
|
@ -1,14 +1,35 @@
|
||||
import contextlib
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from defs.announcement import UnreadAnnouncement
|
||||
from init import bot
|
||||
from misskey_init import init_misskey_bot, misskey_bot_map
|
||||
from models.services.scheduler import scheduler
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mipac import MeDetailed
|
||||
from misskey_init import MisskeyBot
|
||||
|
||||
bot.loop.create_task(init_misskey_bot())
|
||||
|
||||
|
||||
@scheduler.scheduled_job("cron", hour=1, id="daily_status")
|
||||
async def get_unread_announcements(me: "MeDetailed", m_bot: "MisskeyBot"):
|
||||
if not me:
|
||||
return
|
||||
un = [UnreadAnnouncement(an, m_bot) for an in me.unread_announcements]
|
||||
for an in un:
|
||||
try:
|
||||
await an.send_notice()
|
||||
finally:
|
||||
await an.mark_as_read()
|
||||
|
||||
|
||||
@scheduler.scheduled_job("cron", minute=30, id="daily_status")
|
||||
async def daily_status():
|
||||
for m_bot in misskey_bot_map.values():
|
||||
with contextlib.suppress(Exception):
|
||||
await m_bot.core.api.get_me()
|
||||
me = await m_bot.core.api.get_me()
|
||||
await get_unread_announcements(me, m_bot)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user