Skip to content

Instantly share code, notes, and snippets.

@alexiscn
Last active September 23, 2022 09:28
Show Gist options
  • Save alexiscn/ed6fc0e76f0bfc8e0065b972369359e8 to your computer and use it in GitHub Desktop.
Save alexiscn/ed6fc0e76f0bfc8e0065b972369359e8 to your computer and use it in GitHub Desktop.
Telegram Group Defender Bot. A simple bot that disable sending media messages at 22:00(+08:00) and enable sending media messages at 08:00(+08:00).
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=True,
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()
@alexiscn
Copy link
Author

  1. create your bot with @BotFather and replace YOUR_BOT_TOKEN with your bot token get from BotFather
  2. see how to host your bot
  3. add bot to your group and give it admin permissions
  4. send /sleepmode in your group to enable sleepmode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment