AlexJameson

antispam bot mvp

Apr 7th, 2024 (edited)
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import logging
  3. import os
  4. from dotenv import load_dotenv
  5. from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
  6.  
  7. from telegram.ext import ApplicationBuilder, CallbackContext, MessageHandler, CommandHandler, CallbackQueryHandler
  8.  
  9. logging.basicConfig(level=logging.WARNING,
  10.                     format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  11.                     filename="bot.log",
  12.                     filemode="a")
  13.  
  14. load_dotenv()
  15.  
  16. TOKEN = os.getenv('ANTISPAM_TOKEN')
  17. TARGET_CHAT = os.getenv('TARGET_GROUP_ID')
  18. ADMIN_USER_IDS = ['1', '2']
  19.  
  20. async def report_manually(update: Update, context: CallbackContext):
  21.     reply_to_message = update.message.reply_to_message
  22.     numeric_chat_id = reply_to_message.chat.id
  23.     chat_id = str(numeric_chat_id).replace("-100", "")
  24.     message_id = reply_to_message.message_id
  25.     if reply_to_message:
  26.         user = reply_to_message.from_user
  27.         if user.last_name is not None:
  28.             user_display_name = f"{user.first_name} {user.last_name}"
  29.         elif user.last_name is None:
  30.             user_display_name = f"{user.first_name}"
  31.         user_link = f"https://t.me/{user.username}"
  32.         link = f"https://t.me/c/{chat_id}/{reply_to_message.message_id}"
  33.         keyboard = InlineKeyboardMarkup([
  34.             [InlineKeyboardButton("Delete", callback_data='delete')]
  35.         ])
  36.  
  37.         if reply_to_message.text is not None:
  38.             words = reply_to_message.text.split()
  39.             message_text = reply_to_message.text_html_urled
  40.             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]}"
  41.             await context.bot.send_message(chat_id=TARGET_CHAT,
  42.                                     text=text_message_content,
  43.                                     disable_web_page_preview=True,
  44.                                     parse_mode="HTML",
  45.                                     reply_markup=keyboard)
  46.         if reply_to_message.text is None:
  47.             # The same logic but for images and captions
  48.     else:
  49.         await context.bot.send_message(
  50.             chat_id=update.message.chat_id,
  51.             text="Reply to a message with the /ban command."
  52.         )
  53.  
  54. async def button_delete(update: Update, context: CallbackContext):
  55.     query = update.callback_query
  56.     await query.answer()
  57.     message_url = query.message.entities[2].url
  58.     url_parts = message_url.split('/')
  59.     message_id = url_parts[-1]
  60.     #message_id = query.message.reply_to_message.message_id
  61.     chat_id_temp = str(url_parts[-2])
  62.     chat_id=f"-100{chat_id_temp}"
  63.     await context.bot.delete_message(chat_id=chat_id, message_id=message_id)
  64.     #user_id = update.message.from_user.id
  65.     #context.bot.ban_chat_member(chat_id=chat_id, user_id=user_id)
  66.  
  67.  
  68. def main():
  69.     print("I'm working")
  70.  
  71.     application = ApplicationBuilder().token(TOKEN).build()
  72.  
  73.     delete_handler = CallbackQueryHandler(button_delete, pattern='delete')
  74.     application.add_handler(delete_handler)
  75.  
  76.     application.add_handler(CommandHandler("ban", report_manually))
  77.     application.run_polling()
  78.  
  79. if __name__ == '__main__':
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment