Advertisement
Guest User

viabotcleaner

a guest
May 21st, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # A simple script to print all updates received.
  3. # Import modules to access environment, sleep, write to stderr
  4. import os
  5. import sys
  6. import time
  7.  
  8. # Import the client
  9. from telethon import TelegramClient
  10. from telethon.tl.functions.messages import DeleteMessagesRequest
  11. from telethon.tl.functions.channels import EditBannedRequest
  12. from telethon.tl.types import ChatBannedRights
  13.  
  14. from datetime import datetime, timedelta
  15.  
  16. # This is a helper method to access environment variables or
  17. # prompt the user to type them in the terminal if missing.
  18. def get_env(name, message, cast=str):
  19.     if name in os.environ:
  20.         return os.environ[name]
  21.     while True:
  22.         value = input(message)
  23.         try:
  24.             return cast(value)
  25.         except ValueError as e:
  26.             print(e, file=sys.stderr)
  27.             time.sleep(1)
  28.  
  29.  
  30. # Define some variables so the code reads easier
  31. session = os.environ.get('TG_SESSION', 'printer')
  32. api_id = "yourr_app_id" #get_env('TG_API_ID', 'Enter your API ID: ', int)
  33. api_hash = "your_app_hash" #get_env('TG_API_HASH', 'Enter your API hash: ')
  34. proxy = None  # https://github.com/Anorov/PySocks
  35.  
  36. # This is our update handler. It is called when a new update arrives.
  37. async def handler(update):
  38.     #print(update)
  39.     botid = update.message.via_bot_id
  40.     msgid = update.message.id
  41.     channelid = update.message.to_id.channel_id
  42.     fromid = update.message.from_id
  43.     my_user = (await client.get_entity(fromid))
  44.  
  45.     whitelisted = ("@gif", "@youtube", "@wiki", "@bold", "@pic", "@bing", "@imdb", "@vid", "@sticker")
  46.     whitelisted_ids = []
  47.  
  48.     for _username in whitelisted:
  49.         _entity = (await client.get_entity(_username))
  50.         _entityid = _entity.id
  51.         whitelisted_ids.append(_entityid)
  52.  
  53.     #fromusr = client.ResolveUsernameRequest(fromid)
  54.     print("channel = " + str(channelid))
  55.     print("from = " + str(fromid) + " / " + my_user.first_name)
  56.  
  57.     #chat = client.get_entity(PeerChat(channelid))
  58.     #print(chat)
  59.     #channelname = client(ResolveUsernameRequest(group))
  60.  
  61.     if botid and (botid not in whitelisted_ids):
  62.         print("via bot " + str(botid) + " detected | msg_id = " + str(msgid))
  63.         await client.send_message(channelid, "Removed msg from [" + my_user.first_name + "](tg://user?id="+str(fromid)+") .\nReason: Message **via-bot** detected.", parse_mode='md')
  64.         await client.delete_messages(channelid, [msgid])
  65.         rights = ChatBannedRights(
  66.             until_date=datetime.now() - timedelta(days=1),
  67.             view_messages=False,
  68.             send_messages=True
  69.         )
  70.         await client(EditBannedRequest(channelid, fromid, rights))
  71.     else:
  72.         pass
  73.  
  74.  
  75. # Use the client in a `with` block. It calls `start/disconnect` automatically.
  76. with TelegramClient(session, api_id, api_hash, proxy=proxy) as client:
  77.     # Register the update handler so that it gets called
  78.     client.add_event_handler(handler)
  79.  
  80.     # Run the client until Ctrl+C is pressed, or the client disconnects
  81.     print('(Press Ctrl+C to stop this)')
  82.     client.run_until_disconnected()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement