misskey2telegram/defs/announcement.py

84 lines
2.3 KiB
Python
Raw Normal View History

2023-01-24 07:23:25 +00:00
from datetime import datetime
from typing import Optional
from mipac import Route
from init import bot
2023-01-27 12:36:41 +00:00
from misskey_init import MisskeyBot
2023-01-24 07:23:25 +00:00
announcement_template = """<b>Misskey Announcement</b>
<b>{0}</b>
{1}"""
class Announcement:
2023-01-27 12:36:41 +00:00
def __init__(self, data, misskey_bot: MisskeyBot):
self.misskey_bot = misskey_bot
2023-01-24 07:23:25 +00:00
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]:
2023-07-03 14:39:52 +00:00
return (
datetime.strptime(self._updated_at, "%Y-%m-%dT%H:%M:%S.%fZ")
if self._updated_at
else None
)
2023-01-24 07:23:25 +00:00
async def send_notice(self):
if not self.image_url:
await bot.send_message(
2023-01-27 12:36:41 +00:00
self.misskey_bot.tg_user.chat_id,
2023-01-24 07:23:25 +00:00
announcement_template.format(
self.title,
self.text[:1000],
),
2023-01-27 12:36:41 +00:00
reply_to_message_id=self.misskey_bot.tg_user.notice_topic,
2023-01-24 07:23:25 +00:00
)
else:
await bot.send_photo(
2023-01-27 12:36:41 +00:00
self.misskey_bot.tg_user.chat_id,
2023-01-24 07:23:25 +00:00
self.image_url,
caption=announcement_template.format(
self.title,
self.text[:1000],
),
2023-01-27 12:36:41 +00:00
reply_to_message_id=self.misskey_bot.tg_user.notice_topic,
2023-01-24 07:23:25 +00:00
)
async def mark_as_read(self):
data = {
"announcementId": self.id,
}
2023-01-27 12:36:41 +00:00
await self.misskey_bot.core.http.request(
2023-01-24 07:23:25 +00:00
Route("POST", "/api/i/read-announcement"),
2023-07-03 14:39:52 +00:00
json=data,
auth=True,
lower=True,
2023-01-24 07:23:25 +00:00
)
2023-01-27 12:36:41 +00:00
async def get_unread_announcements(misskey_bot: MisskeyBot):
2023-01-24 07:23:25 +00:00
data = {
"limit": 10,
"withUnreads": True,
}
req = await misskey_bot.core.http.request(
Route("POST", "/api/announcements"),
2023-07-03 14:39:52 +00:00
json=data,
auth=True,
lower=True,
2023-01-24 07:23:25 +00:00
)
2023-01-27 12:36:41 +00:00
return [Announcement(i, misskey_bot) for i in req]