Skip to content

Commit

Permalink
added "revoke" button under all links in log channel and inline mode,…
Browse files Browse the repository at this point in the history
… removed "ADMIN_IDS" list since it checks if you're an admin automatically now
  • Loading branch information
suhasa010 committed Jun 13, 2021
1 parent fb44ff1 commit 7fb3657
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
3 changes: 0 additions & 3 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 28 additions & 4 deletions securejoinbot.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -40,17 +43,21 @@ 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(
id=uuid4(),
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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
<b>Profile:</b> <a href="tg://user?id={update.effective_chat.id}">{update.effective_chat.first_name}</a>\n
<b>Invite Link:</b> {invitelink.invite_link}""", parse_mode = 'HTML')
<b>Invite Link:</b> {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:
Expand All @@ -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()

0 comments on commit 7fb3657

Please sign in to comment.