Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import logging
- import os
- from dotenv import load_dotenv
- from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
- from telegram.ext import ApplicationBuilder, CallbackContext, MessageHandler, CommandHandler, CallbackQueryHandler
- logging.basicConfig(level=logging.WARNING,
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
- filename="bot.log",
- filemode="a")
- load_dotenv()
- TOKEN = os.getenv('ANTISPAM_TOKEN')
- TARGET_CHAT = os.getenv('TARGET_GROUP_ID')
- ADMIN_USER_IDS = ['1', '2']
- async def report_manually(update: Update, context: CallbackContext):
- reply_to_message = update.message.reply_to_message
- numeric_chat_id = reply_to_message.chat.id
- chat_id = str(numeric_chat_id).replace("-100", "")
- message_id = reply_to_message.message_id
- if reply_to_message:
- user = reply_to_message.from_user
- if user.last_name is not None:
- user_display_name = f"{user.first_name} {user.last_name}"
- elif user.last_name is None:
- user_display_name = f"{user.first_name}"
- user_link = f"https://t.me/{user.username}"
- link = f"https://t.me/c/{chat_id}/{reply_to_message.message_id}"
- keyboard = InlineKeyboardMarkup([
- [InlineKeyboardButton("Delete", callback_data='delete')]
- ])
- if reply_to_message.text is not None:
- words = reply_to_message.text.split()
- message_text = reply_to_message.text_html_urled
- text_message_content = f"<a href='{user_link}'><b>{user_display_name}</b></a>\n\n{message_text}\n\n<a href='{link}'>Go to chat</a>\n\n@{ADMIN_USER_IDS[0]}"
- await context.bot.send_message(chat_id=TARGET_CHAT,
- text=text_message_content,
- disable_web_page_preview=True,
- parse_mode="HTML",
- reply_markup=keyboard)
- if reply_to_message.text is None:
- # The same logic but for images and captions
- else:
- await context.bot.send_message(
- chat_id=update.message.chat_id,
- text="Reply to a message with the /ban command."
- )
- async def button_delete(update: Update, context: CallbackContext):
- query = update.callback_query
- await query.answer()
- message_url = query.message.entities[2].url
- url_parts = message_url.split('/')
- message_id = url_parts[-1]
- #message_id = query.message.reply_to_message.message_id
- chat_id_temp = str(url_parts[-2])
- chat_id=f"-100{chat_id_temp}"
- await context.bot.delete_message(chat_id=chat_id, message_id=message_id)
- #user_id = update.message.from_user.id
- #context.bot.ban_chat_member(chat_id=chat_id, user_id=user_id)
- def main():
- print("I'm working")
- application = ApplicationBuilder().token(TOKEN).build()
- delete_handler = CallbackQueryHandler(button_delete, pattern='delete')
- application.add_handler(delete_handler)
- application.add_handler(CommandHandler("ban", report_manually))
- application.run_polling()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment