You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
6.5 KiB
180 lines
6.5 KiB
#!/usr/bin/python3 |
|
TOKEN='' |
|
|
|
|
|
from telegram import Update, Chat, ChatMember, ParseMode, ChatMemberUpdated, BotCommand |
|
from telegram.ext import Updater, CommandHandler, ChatMemberHandler, CallbackContext, MessageHandler, Filters |
|
import telegram |
|
import logging |
|
import os.path |
|
import yaml |
|
|
|
configFilename='config.yml' |
|
|
|
config = {} |
|
|
|
|
|
reply_markup = None |
|
bot = telegram.Bot(token=TOKEN) |
|
updater = Updater(token=TOKEN, use_context=True) |
|
dispatcher = updater.dispatcher |
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) |
|
|
|
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'] + '.') |
|
|
|
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: \nNothing 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): |
|
reply_markup=telegram.ReplyKeyboardRemove() |
|
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) |
|
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 = "\nID: " + str(user.id) |
|
mesg +="\nUN: @" + str(user.username) |
|
mesg +="\nFN: " + str(user.first_name) |
|
mesg +="\nLN: " + str(user.last_name) |
|
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)) |
|
logging.info("UN: " + str(user.username)) |
|
logging.info("FN: " + str(user.first_name)) |
|
logging.info("LN: " + str(user.last_name)) |
|
|
|
|
|
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 = "\nID: " + str(user.id) |
|
mesg +="\nUN: @" + str(user.username) |
|
mesg +="\nFN: " + str(user.first_name) |
|
mesg +="\nLN: " + str(user.last_name) |
|
|
|
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() |
|
|
|
commandsList = [] |
|
commandsList.append(BotCommand(command='start', description='Information about this bot.')) |
|
if('name' in config): |
|
commandsList.append(BotCommand(command='request', description='Request ' + config['name'] + ' for contact')) |
|
|
|
|
|
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) |
|
|
|
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()
|
|
|