From 353b83d415061bff2881cbe324273409740be64c Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Fri, 24 Feb 2023 21:08:59 -0800 Subject: bot: implement delayed checkin, (kind of), some TODO left --- callback.py | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) (limited to 'callback.py') diff --git a/callback.py b/callback.py index 5fe4593..cec6188 100644 --- a/callback.py +++ b/callback.py @@ -13,6 +13,8 @@ from dbstore.peewee_store import User, db, TOOT_VISIBILITY_PRIVATE, TOOT_VISIBIL import uuid from mastodon import Mastodon from util import decrypt, check_user +from datetime import datetime, timedelta +from config import KEY_IS_SCHEDULED_TOOT def generate_uuid(): @@ -131,9 +133,15 @@ async def callback_location_sharing(update: Update, context: ContextTypes.DEFAUL content_type = "text/markdown" if user["home_instance_type"] == "pleroma" else None + if user["delayed_checkin"] > 0: + scheduled_at = datetime.now() + timedelta(minutes=user["delayed_checkin"]) + else: + scheduled_at = None + status = get_mastodon_client(update.effective_user.id).status_post(content, visibility=user["tool_visibility"], content_type=content_type, + scheduled_at=scheduled_at, media_ids=[]) context.user_data[KEY_TOOT_STATUS_ID] = status["id"] @@ -216,17 +224,32 @@ async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE, user: content_type = "text/markdown" if user["home_instance_type"] == "pleroma" else None + if user["delayed_checkin"] > 0: + scheduled_at = datetime.now() + timedelta(minutes=user["delayed_checkin"]) + else: + scheduled_at = None + status = get_mastodon_client(context.user_data["user_id"]).status_post(content, visibility=user["toot_visibility"], content_type=content_type, + scheduled_at=scheduled_at, media_ids=[]) + if "scheduled_at" in status: + context.user_data[KEY_TOOT_STATUS_ID] = status["id"] + context.user_data[KEY_TOOT_STATUS_CONTENT] = content + context.user_data[KEY_IS_SCHEDULED_TOOT] = True - context.user_data[KEY_TOOT_STATUS_ID] = status["id"] - context.user_data[KEY_TOOT_STATUS_CONTENT] = content + await context.bot.send_message(chat_id=context.user_data.get("chat_id"), + text=f"Selected place: {poi_name}, \nToot scheduled at: {status['scheduled_at']}", + parse_mode=ParseMode.MARKDOWN) + elif "url" in status: + context.user_data[KEY_TOOT_STATUS_ID] = status["id"] + context.user_data[KEY_TOOT_STATUS_CONTENT] = content + context.user_data[KEY_IS_SCHEDULED_TOOT] = False - await context.bot.send_message(chat_id=context.user_data.get("chat_id"), - text=f"Selected place: {poi_name}, \nPosted to Mastodon: {status['url']}", - parse_mode=ParseMode.MARKDOWN) + await context.bot.send_message(chat_id=context.user_data.get("chat_id"), + text=f"Selected place: {poi_name}, \nPosted to Mastodon: {status['url']}", + parse_mode=ParseMode.MARKDOWN) msg = await context.bot.send_message(chat_id=context.user_data.get("chat_id"), text=PROMPT_ADD_COMMENT, @@ -276,10 +299,23 @@ async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYP content_type = "text/markdown" if user["home_instance_type"] == "pleroma" else None comment = update.effective_message.text - get_mastodon_client(update.effective_user.id).status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), - content_type=content_type, - status=f"{comment}\n\n" + context.user_data.get( - KEY_TOOT_STATUS_CONTENT)) + if context.user_data.get(KEY_IS_SCHEDULED_TOOT): + # TODO: + # Mastodon don't have API to update the content of scheduled toots + # we need to store scheduled toots in memory before posting to the server + pass + # get_mastodon_client(update.effective_user.id).scheduled_status_update( + # id=context.user_data.get(KEY_TOOT_STATUS_ID), + # content_type=content_type, + # status=f"{comment}\n\n" + context.user_data.get( + # KEY_TOOT_STATUS_CONTENT)) + + else: + get_mastodon_client(update.effective_user.id).status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), + content_type=content_type, + status=f"{comment}\n\n" + context.user_data.get( + KEY_TOOT_STATUS_CONTENT)) + context.user_data[KEY_TOOT_STATUS_CONTENT] = f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT) return await _process_comment(context) -- cgit v1.2.3