Created
August 12, 2018 06:01
-
-
Save gowhari/707c5c808f83433016be4946a35efeda to your computer and use it in GitHub Desktop.
telegram bot that prevents adding bots to group by non admin members, only admins are able to add bots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
import logging | |
import telepot | |
import telepot.loop | |
thisbot_token = 'YOUR-BOT-TOKEN' | |
access_warning = ''\ | |
'WARNING: A bot has been added to group but '\ | |
'I don\'t have enough permission to remove it. Make me admin please.' | |
invited_group_warning = ''\ | |
'WARNING: Thank you for adding me here, '\ | |
'but I can only work in supergroups, here is a basic group.' | |
in_group_warning = ''\ | |
'WARNING: A bot has been added to group but '\ | |
'I can not remove it as this group is a basic group.' | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d: %(message)s') | |
logger = logging.getLogger() | |
blacklist = set() | |
class Investigator: | |
def check(self, msg): | |
if 'new_chat_members' not in msg: | |
return | |
bots = [i for i in msg['new_chat_members'] if i['is_bot']] | |
if not bots: | |
return | |
self.msg = msg | |
self.chat_id = msg['chat']['id'] | |
self.chat_title = msg['chat']['title'] | |
self.from_id = msg['from']['id'] | |
self.is_admin = self.check_is_admin() | |
self.unauthorized = False | |
for i in bots: | |
self.check_bot(i) | |
self.check_blacklist() | |
def check_bot(self, bot): | |
if self.is_myself(bot) or self.added_by_admin(bot): | |
return | |
self.unauthorized = True | |
self.kick_member(bot) | |
def is_myself(self, bot): | |
if bot['id'] != thisbot_id: | |
return False | |
logger.info('I have been invited to a group :): "%s"', self.chat_title) | |
if self.msg['chat']['type'] == 'group': | |
thisbot.sendMessage(self.chat_id, invited_group_warning) | |
return True | |
def check_is_admin(self): | |
admins = thisbot.getChatAdministrators(self.chat_id) | |
admins = [i for i in admins if i['user']['id'] == self.from_id] | |
if len(admins) == 0: | |
return False | |
admin = admins[0] | |
if admin.get('status') == 'creator' or admin.get('can_invite_users'): | |
return True | |
return False | |
def added_by_admin(self, bot): | |
if not self.is_admin: | |
return False | |
logger.info('Bot added by admin: "%s", "%s"', self.chat_title, bot['username']) | |
return True | |
def kick_member(self, member): | |
name = member['username'] | |
try: | |
thisbot.kickChatMember(self.chat_id, member['id']) | |
logger.info('Successfully removed: "%s", "%s"', self.chat_title, name) | |
except telepot.exception.NotEnoughRightsError: | |
thisbot.sendMessage(self.chat_id, access_warning) | |
logger.warn('Remove failed: "%s", "%s"', self.chat_title, name) | |
except telepot.exception.TelegramError: | |
logger.warn('Remove failed: "%s", "%s"', self.chat_title, name) | |
if self.msg['chat']['type'] == 'group': | |
thisbot.sendMessage(self.chat_id, in_group_warning) | |
def check_blacklist(self): | |
if not self.unauthorized: | |
return | |
key = (self.chat_id, self.from_id) | |
if key not in blacklist: | |
blacklist.add(key) | |
else: | |
self.kick_member(self.msg['from']) | |
def investigate(msg): | |
Investigator().check(msg) | |
def main(): | |
global thisbot, thisbot_id | |
thisbot = telepot.Bot(thisbot_token) | |
thisbot_id = thisbot.getMe()['id'] | |
telepot.loop.MessageLoop(thisbot, investigate).run_as_thread() | |
while True: | |
time.sleep(1) | |
if __name__ == '__main__': | |
main() |
CooL
damton garm
How it is work?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!