diff --git a/sample.env b/sample.env index 99f112c..b2db1c1 100644 --- a/sample.env +++ b/sample.env @@ -10,9 +10,6 @@ GROUP_ID = -10012345678 #Enter the channel ID where the logs about invite link generations should be posted * NOTIF_CHANNEL_ID = -10012345678 -#Enter the IDs of admins in {} separated by comma * -ADMIN_IDS = {12345678,87654321} - #Enter your redis credentials here * #Replace localhost with redis url and enter password, if any. REDIS_DB_HOST = localhost diff --git a/securejoinbot.py b/securejoinbot.py index d8b8fa7..dcf8bec 100644 --- a/securejoinbot.py +++ b/securejoinbot.py @@ -1,4 +1,5 @@ -from telegram.ext import Updater +from telegram.ext import Updater, CallbackContext +from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton import os from dotenv import load_dotenv load_dotenv() @@ -27,6 +28,8 @@ import redis r = redis.Redis(host=REDIS_DB_HOST, port=REDIS_DB_PORT, db=REDIS_DB_NUMBER, password = REDIS_DB_PASSWORD if (REDIS_DB_PASSWORD) else None) + + import datetime, string from uuid import uuid4 from telegram import InlineQueryResultArticle, InputTextMessageContent, Bot, ChatInviteLink @@ -40,6 +43,9 @@ def inline_generate(update, context): add_seconds = datetime.timedelta(days=int(EXPIRE_DAYS), hours=int(EXPIRE_HOURS), seconds=int(EXPIRE_SECS)) time_in_future = timestamp + add_seconds invitelink = context.bot.create_chat_invite_link(chat_id = GROUP_ID, expire_date=time_in_future, member_limit=EXPIRE_USERS, timeout=None) + markup = InlineKeyboardMarkup([[ + InlineKeyboardButton("Revoke", callback_data=f"{invitelink.invite_link}"), + ]]) results = list() results.append( InlineQueryResultArticle( @@ -47,10 +53,11 @@ def inline_generate(update, context): title= Strings.INLINE_GENERATE_INVITE_LINK, input_message_content=InputTextMessageContent(f"""{invitelink.invite_link}\n""", parse_mode='HTML', disable_web_page_preview=True), description=f"Valid for {EXPIRE_USERS} user(s)\nExpires in {EXPIRE_DAYS} days, {EXPIRE_HOURS} hours, {EXPIRE_SECS} seconds", + reply_markup = markup ) ) r.incrby("total_inv", amount = 1) - context.bot.send_message(chat_id = NOTIF_CHANNEL_ID, text = f"Admin {update.inline_query.from_user.first_name} has generated an expiring link - {invitelink.invite_link}") + context.bot.send_message(chat_id = NOTIF_CHANNEL_ID, text = f"Admin {update.inline_query.from_user.first_name} has generated an expiring link - {invitelink.invite_link}", reply_markup = markup, disable_web_page_preview=True) context.bot.answer_inline_query(update.inline_query.id, results, cache_time = 1) else: results = list() @@ -101,7 +108,7 @@ def revoke(update, context): print(update.message.reply_to_message.text) context.bot.revoke_chat_invite_link(chat_id = GROUP_ID, invite_link = update.message.reply_to_message.text) context.bot.send_message(chat_id=update.effective_chat.id, text="Revoked the invite link.") - context.bot.send_message(chat_id = NOTIF_CHANNEL_ID, text = f"Admin {update.effective_chat.first_name} has revoked {update.message.reply_to_message.text}.") + context.bot.send_message(chat_id = NOTIF_CHANNEL_ID, text = f"Admin {update.effective_chat.first_name} has revoked {update.message.reply_to_message.text}.", disable_web_page_preview=True) from telegram.ext import CommandHandler @@ -129,10 +136,13 @@ def direct_generate(update, context): time_in_future = timestamp + add_seconds invitelink = context.bot.create_chat_invite_link(chat_id = GROUP_ID, expire_date=time_in_future, member_limit=EXPIRE_USERS, timeout=None) context.bot.send_message(chat_id=update.effective_chat.id, text=invitelink.invite_link) + markup = InlineKeyboardMarkup([[ + InlineKeyboardButton("Revoke", callback_data=f"{invitelink.invite_link}"), + ]]) context.bot.send_message(chat_id = NOTIF_CHANNEL_ID, text = f"""{update.effective_chat.first_name} has generated an expiring link. Profile: {update.effective_chat.first_name}\n -Invite Link: {invitelink.invite_link}""", parse_mode = 'HTML') +Invite Link: {invitelink.invite_link}""", parse_mode = 'HTML', disable_web_page_preview=True, reply_markup = markup) r.mset({f"{update.effective_chat.id}_inv": "0"}) r.incrby("total_inv", amount = 1) else: @@ -141,4 +151,18 @@ def direct_generate(update, context): direct_generate_handler = CommandHandler('link', direct_generate) dispatcher.add_handler(direct_generate_handler) +def revoke_inline(update: Update, _: CallbackContext): + query = update.callback_query + user = query.bot.get_chat_member(GROUP_ID, int(query.from_user.id)) + if (user.status == "administrator" or user.status == "creator"): + query.bot.revoke_chat_invite_link(chat_id = GROUP_ID, invite_link = query.data) + query.answer(text="Revoked the invite link.", show_alert = True) + query.bot.send_message(chat_id = NOTIF_CHANNEL_ID, text = f"Admin {query.from_user.first_name} has revoked {query.data}.", disable_web_page_preview=True) + else: + query.answer(Strings.INLINE_UNAUTHORIZED, show_alert = True) + +from telegram.ext import CallbackQueryHandler +revoke_handler = CallbackQueryHandler(revoke_inline, pattern = '^http*') +dispatcher.add_handler(revoke_handler) + updater.start_polling() \ No newline at end of file