Last active
September 23, 2022 09:28
-
-
Save alexiscn/ed6fc0e76f0bfc8e0065b972369359e8 to your computer and use it in GitHub Desktop.
Revisions
-
alexiscn revised this gist
Sep 23, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ async def awake_callback(context: ContextTypes.DEFAULT_TYPE): can_send_messages=True, can_send_media_messages=True, can_send_polls=True, can_send_other_messages=True, can_add_web_page_previews=True, can_change_info=True, can_invite_users=True, -
alexiscn created this gist
Sep 23, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ import logging from telegram import Update, ChatPermissions from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler from datetime import time logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO ) async def sleep_callback(context: ContextTypes.DEFAULT_TYPE): chat_id = context.job.chat_id permissions = ChatPermissions(can_send_messages=True) await context.bot.set_chat_permissions(chat_id=chat_id, permissions=permissions) await context.bot.send_message(chat_id=chat_id, text='睡眠模式已开启') async def awake_callback(context: ContextTypes.DEFAULT_TYPE): chat_id = context.job.chat_id permissions = ChatPermissions( can_send_messages=True, can_send_media_messages=True, can_send_polls=True, can_send_other_messages=False, can_add_web_page_previews=True, can_change_info=True, can_invite_users=True, can_pin_messages=True ) await context.bot.set_chat_permissions(chat_id=chat_id, permissions=permissions) await context.bot.send_message(chat_id=chat_id, text='睡眠模式已关闭') async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await update.message.reply_text("Add Bot To Group, give it admin permission and use send /sleepmode in group to finish setup. ") def remove_job_if_exists(name: str, context: ContextTypes.DEFAULT_TYPE) -> bool: current_jobs = context.job_queue.get_jobs_by_name(name) if not current_jobs: return False for job in current_jobs: job.schedule_removal() return True async def sleepmode(update: Update, context: ContextTypes.DEFAULT_TYPE): chat_id = update.effective_message.chat_id # setup wakeup task wakeup_name = str(chat_id) + "_wakeup" wakeup_timestr = '08:00:00+08:00' remove_job_if_exists(wakeup_name, context) context.job_queue.run_daily(awake_callback, time=time.fromisoformat(wakeup_timestr), chat_id=chat_id, name=wakeup_name) # setup sleep task sleep_name = str(chat_id) + "_sleep" sleep_timestr = '22:00:00+08:00' remove_job_if_exists(sleep_name, context) context.job_queue.run_daily(sleep_callback, time=time.fromisoformat(sleep_timestr), chat_id = chat_id, name=sleep_name) await update.message.reply_text("睡眠模式开始时间: " + sleep_timestr + ", 睡眠模式关闭时间:" + wakeup_timestr) if __name__ == '__main__': application = ApplicationBuilder().token('YOUR_BOT_TOKEN').build() application.add_handler(CommandHandler('start', start)) application.add_handler(CommandHandler('sleepmode', sleepmode)) application.run_polling()