2021-06-11 17:15:28 +00:00
#!/usr/bin/python3
2021-06-11 17:27:17 +00:00
from dotenv import dotenv_values
2021-06-11 17:18:44 +00:00
from telegram import Update , Chat , ChatMember , ParseMode , ChatMemberUpdated , BotCommand
from telegram . ext import Updater , CommandHandler , ChatMemberHandler , CallbackContext , MessageHandler , Filters
2021-06-11 17:15:28 +00:00
import telegram
import logging
import os . path
import yaml
2021-06-11 17:27:17 +00:00
TOKEN = dotenv_values ( ) [ ' TOKEN ' ]
2021-06-11 17:15:28 +00:00
configFilename = ' config.yml '
config = { }
reply_markup = None
2021-06-11 17:18:44 +00:00
bot = telegram . Bot ( token = TOKEN )
updater = Updater ( token = TOKEN , use_context = True )
2021-06-11 17:15:28 +00:00
dispatcher = updater . dispatcher
2021-06-11 17:27:17 +00:00
logging . basicConfig ( format = ' %(asctime)s - %(levelname)s - %(message)s ' , level = logging . INFO )
2021-06-11 17:15:28 +00:00
def loadConfig ( ) :
if ( os . path . isfile ( configFilename ) ) :
global config
config . clear ( )
with open ( configFilename , ' r ' ) as filehandle :
try :
config = yaml . safe_load ( filehandle )
except yaml . YAMLError as e :
logging . error ( e )
def saveConfig ( ) :
with open ( configFilename , ' w ' , encoding = ' utf8 ' ) as filehandle :
yaml . dump ( config , filehandle , default_flow_style = False , allow_unicode = True )
def setup ( update : Update , context : CallbackContext ) :
global config
user = update . effective_user
config [ ' adminID ' ] = user . id
config [ ' name ' ] = user . first_name
saveConfig ( )
loadConfig ( )
logging . info ( " Bot claimed by " + config [ ' name ' ] + " . " )
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = ' You just claimed this bot, ' + config [ ' name ' ] + ' . ' )
2021-06-11 21:49:21 +00:00
start ( update , context )
2021-06-11 17:15:28 +00:00
def setName ( update : Update , context : CallbackContext ) :
if ( context . args ) :
global config
config [ ' name ' ] = " " . join ( context . args )
saveConfig ( )
loadConfig ( )
logging . info ( ' Name changed to ' + config [ ' name ' ] )
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = ' Your name is now ' + config [ ' name ' ] + ' . ' )
else :
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = ' You need at least one argument. Example: /setname Your Name ' )
def listBlacklist ( update : Update , context : CallbackContext ) :
if ( isAdmin ) :
global config
if ( ' blacklist ' in config ) :
bot . sendMessage ( chat_id = config [ ' adminID ' ] , text = " Currently blocked: \n " + " \n " . join ( config [ ' blacklist ' ] ) )
else :
bot . sendMessage ( chat_id = config [ ' adminID ' ] , text = " Currently blocked: \n Nothing at all. " )
def isInBlacklist ( userid ) :
if ( ' blacklist ' in config ) :
if ( str ( userid ) in config [ ' blacklist ' ] ) :
logging . info ( " Blocked " + str ( userid ) )
return True
else :
logging . info ( " OK " + str ( userid ) )
return False
def isAdmin ( userid ) :
global config
if ( userid == config [ ' adminID ' ] ) :
return True
else :
return False
def block ( update : Update , context : CallbackContext ) :
if ( isAdmin ) :
if ( context . args ) :
global config
if ( ' blacklist ' not in config ) :
config [ ' blacklist ' ] = [ ]
config [ ' blacklist ' ] . append ( context . args [ 0 ] )
saveConfig ( )
loadConfig ( )
logging . info ( " Blocked: " + context . args [ 0 ] )
bot . sendMessage ( chat_id = config [ ' adminID ' ] , text = " Blocked: " + context . args [ 0 ] )
def unblock ( update : Update , context : CallbackContext ) :
if ( isAdmin ) :
if ( context . args ) :
global config
if ( ' blacklist ' in config ) :
if ( context . args [ 0 ] in config [ ' blacklist ' ] ) :
config [ ' blacklist ' ] . remove ( context . args [ 0 ] )
saveConfig ( )
loadConfig ( )
logging . info ( " Unblocked: " + context . args [ 0 ] )
bot . sendMessage ( chat_id = config [ ' adminID ' ] , text = " Unblocked: " + context . args [ 0 ] )
def start ( update : Update , context : CallbackContext ) :
2021-06-11 21:49:21 +00:00
commandsList = [ ]
commandsList . append ( BotCommand ( command = ' start ' , description = ' Information about this bot. ' ) )
if ( ' name ' in config ) :
commandsList . append ( BotCommand ( command = ' request ' , description = ' Send a contact request to ' + config [ ' name ' ] ) )
if ( isAdmin ) :
commandsList . append ( BotCommand ( command = ' block ' , description = ' Admin: Block a user. ID required. /block {ID} ' ) )
commandsList . append ( BotCommand ( command = ' unblock ' , description = ' Admin: Unblock a user. ID required. /unblock {ID} ' ) )
commandsList . append ( BotCommand ( command = ' blocklist ' , description = ' Admin: List the blocked IDs. ' ) )
commandsList . append ( BotCommand ( command = ' setname ' , description = ' Admin: Set your name for information purposes. /setname {name} ' ) )
if ( ' adminID ' not in config ) :
dispatcher . add_handler ( CommandHandler ( ' setup ' , setup ) )
commandsList . append ( BotCommand ( command = ' setup ' , description = ' Set yourself up as owner. ' ) )
bot . set_my_commands ( commands = commandsList )
2021-06-11 17:15:28 +00:00
reply_markup = telegram . ReplyKeyboardRemove ( )
2021-06-11 21:49:21 +00:00
2021-06-11 17:15:28 +00:00
if ( ' name ' in config ) :
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = " Hello! " + config [ ' name ' ] + " has set up a contact protection bot. To request contact, type /request. " , reply_markup = reply_markup )
2021-06-11 21:54:53 +00:00
if ( isAdmin ( update . effective_user . id ) ) :
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = " You are admin btw. " )
2021-06-11 17:15:28 +00:00
else :
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = " Hello! This bot isn ' t claimed yet. Type /setup to claim. " , reply_markup = reply_markup )
def forwardContact ( update : Update , context : CallbackContext ) :
user = update . effective_user
if ( isInBlacklist ( user . id ) ) :
return
mesg = " \n ID: " + str ( user . id )
2021-06-11 17:41:31 +00:00
mesg + = " \n Username: @ " + str ( user . username )
mesg + = " \n Fore name: " + str ( user . first_name )
mesg + = " \n Last name: " + str ( user . last_name )
2021-06-11 17:15:28 +00:00
bot . sendMessage ( chat_id = config [ ' adminID ' ] , text = " Request by: " + mesg )
bot . send_contact ( chat_id = config [ ' adminID ' ] , contact = update . message . contact )
reply_markup = telegram . ReplyKeyboardRemove ( )
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = " Your contact information has been forwarded. " + config [ ' name ' ] + " will eventually contact you back. " , reply_markup = reply_markup )
def request ( update : Update , context : CallbackContext ) :
user = update . effective_user
if ( isInBlacklist ( user . id ) ) :
return False
logging . info ( " ID: " + str ( user . id ) )
2021-06-11 17:41:31 +00:00
logging . info ( " Username: @ " + str ( user . username ) )
logging . info ( " Fore name: " + str ( user . first_name ) )
logging . info ( " Last name: " + str ( user . last_name ) )
2021-06-11 17:15:28 +00:00
if ( user . username is None ) :
context . bot . send_message ( chat_id = update . effective_chat . id , text = " You don ' t have a public username set up in Telegram. This will make it hard to contact you back. "
+ " Please set up a username or share your phone number. " )
reply_markup = telegram . ReplyKeyboardMarkup ( [ [ telegram . KeyboardButton ( ' Share phone number ' , request_contact = True ) ] ] )
context . bot . sendMessage ( chat_id = update . effective_chat . id , text = ' Share phone number? ' , reply_markup = reply_markup )
else :
mesg = " \n ID: " + str ( user . id )
2021-06-11 17:41:31 +00:00
mesg + = " \n Username: @ " + str ( user . username )
mesg + = " \n Fore name: " + str ( user . first_name )
mesg + = " \n Last name: " + str ( user . last_name )
2021-06-11 17:15:28 +00:00
bot . sendMessage ( chat_id = config [ ' adminID ' ] , text = " Request by: " + mesg )
context . bot . send_message ( chat_id = update . effective_chat . id , text = " Your request has been sent. " + config [ ' name ' ] + " will eventually contact you back. " )
loadConfig ( )
dispatcher . add_handler ( CommandHandler ( ' start ' , start ) )
dispatcher . add_handler ( CommandHandler ( ' block ' , block ) )
dispatcher . add_handler ( CommandHandler ( ' blocklist ' , listBlacklist ) )
dispatcher . add_handler ( CommandHandler ( ' unblock ' , unblock ) )
dispatcher . add_handler ( CommandHandler ( ' setname ' , setName ) )
dispatcher . add_handler ( CommandHandler ( ' request ' , request ) )
dispatcher . add_handler ( MessageHandler ( Filters . contact , forwardContact ) )
updater . start_polling ( )