Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # A simple script to print all updates received.
- # Import modules to access environment, sleep, write to stderr
- import os
- import sys
- import time
- # Import the client
- from telethon import TelegramClient
- from telethon.tl.functions.messages import DeleteMessagesRequest
- from telethon.tl.functions.channels import EditBannedRequest
- from telethon.tl.types import ChatBannedRights
- from datetime import datetime, timedelta
- # This is a helper method to access environment variables or
- # prompt the user to type them in the terminal if missing.
- def get_env(name, message, cast=str):
- if name in os.environ:
- return os.environ[name]
- while True:
- value = input(message)
- try:
- return cast(value)
- except ValueError as e:
- print(e, file=sys.stderr)
- time.sleep(1)
- # Define some variables so the code reads easier
- session = os.environ.get('TG_SESSION', 'printer')
- api_id = "yourr_app_id" #get_env('TG_API_ID', 'Enter your API ID: ', int)
- api_hash = "your_app_hash" #get_env('TG_API_HASH', 'Enter your API hash: ')
- proxy = None # https://github.com/Anorov/PySocks
- # This is our update handler. It is called when a new update arrives.
- async def handler(update):
- #print(update)
- botid = update.message.via_bot_id
- msgid = update.message.id
- channelid = update.message.to_id.channel_id
- fromid = update.message.from_id
- my_user = (await client.get_entity(fromid))
- whitelisted = ("@gif", "@youtube", "@wiki", "@bold", "@pic", "@bing", "@imdb", "@vid", "@sticker")
- whitelisted_ids = []
- for _username in whitelisted:
- _entity = (await client.get_entity(_username))
- _entityid = _entity.id
- whitelisted_ids.append(_entityid)
- #fromusr = client.ResolveUsernameRequest(fromid)
- print("channel = " + str(channelid))
- print("from = " + str(fromid) + " / " + my_user.first_name)
- #chat = client.get_entity(PeerChat(channelid))
- #print(chat)
- #channelname = client(ResolveUsernameRequest(group))
- if botid and (botid not in whitelisted_ids):
- print("via bot " + str(botid) + " detected | msg_id = " + str(msgid))
- 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')
- await client.delete_messages(channelid, [msgid])
- rights = ChatBannedRights(
- until_date=datetime.now() - timedelta(days=1),
- view_messages=False,
- send_messages=True
- )
- await client(EditBannedRequest(channelid, fromid, rights))
- else:
- pass
- # Use the client in a `with` block. It calls `start/disconnect` automatically.
- with TelegramClient(session, api_id, api_hash, proxy=proxy) as client:
- # Register the update handler so that it gets called
- client.add_event_handler(handler)
- # Run the client until Ctrl+C is pressed, or the client disconnects
- print('(Press Ctrl+C to stop this)')
- client.run_until_disconnected()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement