Расчистка файлов для новой версии (старая в archive_1)
This commit is contained in:
parent
ba4df6db0f
commit
370c55b3e3
346
anon.py
346
anon.py
@ -1,346 +0,0 @@
|
||||
import telebot
|
||||
import os
|
||||
import json
|
||||
from icecream import ic
|
||||
|
||||
########### Функции ##############
|
||||
from func import *
|
||||
|
||||
########### Работа с БД ##########
|
||||
from db import *
|
||||
raw_db = read_db()
|
||||
|
||||
###### Класс пользователя ########
|
||||
from user import *
|
||||
# user.id = 123
|
||||
# user.pkey = "fd35s"
|
||||
# user.channel = "anotheruser"
|
||||
# user.avatar = "♿️"
|
||||
# user.blocks = [124235]
|
||||
# user.keys = {"anotheruser": "re543"}
|
||||
|
||||
####### Работа с хэшем ###########
|
||||
from hash import hash
|
||||
from random import randint
|
||||
|
||||
##### Инициализация токена #######
|
||||
bot = telebot.TeleBot(raw_db['token'])
|
||||
|
||||
########## Ловим ошибки ##########
|
||||
from catch_err import *
|
||||
|
||||
|
||||
############## MAIN ##############
|
||||
|
||||
@bot.message_handler(commands=['start', 'help'])
|
||||
def start(message):
|
||||
bot.reply_to(message, f"""Приветствую, это бот для анонимного общения.
|
||||
Главное - выполняет свою функцию без рекламы, без платы и без слежки.
|
||||
|
||||
Для начала зарегистрируйся:
|
||||
/reg ЛюбойНикнейм
|
||||
Установка адресата - { telebot.formatting.hcode(":НикАдресата") }
|
||||
|
||||
Исходный код: https://gitea.404.mn/justuser/just_anonchat
|
||||
Связь с админом: { telebot.formatting.hcode(":justuser") }
|
||||
|
||||
Ещё проекты: @just_openbots""", parse_mode="HTML")
|
||||
|
||||
@bot.message_handler(commands=['reg'])
|
||||
def reg(message):
|
||||
try:
|
||||
db = load()
|
||||
if not ok_args(bot, message, 2, '```\n/reg никнейм```'):
|
||||
return 0
|
||||
nick = message.text.split()[1]
|
||||
# Проверка ника
|
||||
if not nick_ok(bot, message, nick):
|
||||
return 0
|
||||
if nick in db:
|
||||
bot.reply_to(message, "Данный пользователь уже зарегистрирован.")
|
||||
elif str(message.chat.id) in db:
|
||||
bot.reply_to(message, "Вы уже зарегистрированы.")
|
||||
else:
|
||||
user = user_(message.chat.id, hash(randint(74287, 5747962)))
|
||||
db[nick] = user
|
||||
db[message.chat.id] = nick
|
||||
|
||||
save(db)
|
||||
bot.reply_to(message, "Вы зарегистрировались!\nПриятного использования.")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
@bot.message_handler(commands=['b'])
|
||||
def b(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 2, '```\n/b :ник``` или ```\n/b ник```'):
|
||||
return 0
|
||||
db = load()
|
||||
nick = db[str(message.chat.id)]
|
||||
user = db[nick]
|
||||
block = message.text.split()[1]
|
||||
|
||||
# Block by ":user"
|
||||
if block[0] == ":":
|
||||
block = block[1:]
|
||||
|
||||
if block in db:
|
||||
if db[block].id not in user.blocks:
|
||||
user.blocks.append(db[block].id)
|
||||
save(db)
|
||||
bot.reply_to(message, f"Пользователь {telebot.formatting.hcode(block)} был заблокирован.",parse_mode="HTML")
|
||||
else:
|
||||
bot.reply_to(message, "Данного пользователя не существует.")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
@bot.message_handler(commands=['u'])
|
||||
def u(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 2, '```\n/u :ник``` или ```\n/u ник```'):
|
||||
return 0
|
||||
db = load()
|
||||
nick = db[str(message.chat.id)]
|
||||
user = db[nick]
|
||||
block = message.text.split()[1]
|
||||
|
||||
# Unblock by ":user"
|
||||
if block[0] == ":":
|
||||
block = block[1:]
|
||||
|
||||
if block in db and db[block].id in user.blocks:
|
||||
user.blocks.remove(db[block].id)
|
||||
save(db)
|
||||
bot.reply_to(message, f"Была снята блокировка с пользователя {telebot.formatting.hcode(block)}",parse_mode="HTML")
|
||||
else:
|
||||
bot.reply_to(message, "Данного пользователя не существует.")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
@bot.message_handler(commands=['nick'])
|
||||
def nick(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 2, '```\n/nick ник```'):
|
||||
return 0
|
||||
db = load()
|
||||
new_nick = message.text.split()[1]
|
||||
# Проверка ника
|
||||
if not nick_ok(bot, message, new_nick):
|
||||
return 0
|
||||
old_nick = db[str(message.chat.id)]
|
||||
|
||||
if new_nick not in db:
|
||||
db[new_nick] = db[old_nick]
|
||||
db[new_nick].avatar = "♿️"
|
||||
db[str(message.chat.id)] = new_nick
|
||||
del db[old_nick]
|
||||
save(db)
|
||||
bot.reply_to(message,f"Вы успешно сменили ник с {telebot.formatting.hcode(old_nick)} на {telebot.formatting.hcode(new_nick)}",parse_mode="HTML")
|
||||
bot.reply_to(message, """Ваша аватарка сброшена до стандартной: ♿️
|
||||
Также вы можете сбросить публичный ключ: /key_res""")
|
||||
else:
|
||||
bot.reply_to(message,"Данный ник уже занят")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
@bot.message_handler(commands=['av'])
|
||||
def av(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 2, '```\n/av ❄️```'):
|
||||
return 0
|
||||
db = load()
|
||||
if not len(message.text.split()) > 1:
|
||||
bot.reply_to(message,"Укажите аватарку")
|
||||
return 0
|
||||
new_avatar = message.text.split()[1]
|
||||
if len(new_avatar) > 10:
|
||||
bot.reply_to(message,"Слишком большое количество символов для аватарки")
|
||||
else:
|
||||
nick = db[str(message.chat.id)]
|
||||
db[nick].avatar = new_avatar
|
||||
save(db)
|
||||
bot.reply_to(message,"Новая аватарка успешно установлена")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
######### Работа с ключом ########
|
||||
|
||||
@bot.message_handler(commands=['key'])
|
||||
def key(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 2, '```\n/key ник``` или ```\n/key :ник```'):
|
||||
return 0
|
||||
db = load()
|
||||
nick = message.text.split()[1]
|
||||
if nick[0] == ':':
|
||||
nick = nick[1:]
|
||||
key = db[nick].pkey
|
||||
bot.reply_to(message,f"Ключ пользователя: {telebot.formatting.hcode(key)}", parse_mode="HTML")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
@bot.message_handler(commands=['ver'])
|
||||
def ver(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 3, '```\n/ver ник ключ``` или ```\n/ver :ник ключ```'):
|
||||
return 0
|
||||
db = load()
|
||||
nick = message.text.split()[1]
|
||||
if nick[0] == ':':
|
||||
nick = nick[1:]
|
||||
key = message.text.split()[2]
|
||||
if not nick in db:
|
||||
bot.reply_to(message,"Не существует такого пользователя")
|
||||
return 0
|
||||
|
||||
if key == db[nick].pkey:
|
||||
bot.reply_to(message,"✅ Ключи совпадают")
|
||||
else:
|
||||
bot.reply_to(message,"❌ Ключи не совпадают")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
@bot.message_handler(commands=['key_res'])
|
||||
def key_res(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 1, '```\n/key_res```'):
|
||||
return 0
|
||||
db = load()
|
||||
|
||||
key = hash(randint(74287, 5747962))
|
||||
nick = db[str(message.chat.id)]
|
||||
old_key = db[nick].pkey
|
||||
|
||||
db[nick].pkey = key
|
||||
save(db)
|
||||
|
||||
bot.reply_to(message,f"""🔑 Ключ успешно сброшен.
|
||||
|
||||
Старый ключ: {telebot.formatting.hcode(old_key)}
|
||||
Новый ключ: {telebot.formatting.hcode(key)}""",parse_mode="HTML")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
|
||||
##################################
|
||||
|
||||
@bot.message_handler(commands=['me'])
|
||||
def me(message):
|
||||
try:
|
||||
if not is_auth(bot, message) or not ok_args(bot, message, 1, '```\n/me```'):
|
||||
return 0
|
||||
db = load()
|
||||
nick = db[str(message.chat.id)]
|
||||
user = db[nick]
|
||||
ch = user.channel
|
||||
if not user.channel:
|
||||
ch = "Не задан."
|
||||
|
||||
bot.reply_to(message, f"""Заданный канал: {telebot.formatting.hcode(ch)}
|
||||
|
||||
Ваш ник: {telebot.formatting.hcode(nick)}
|
||||
Ваша аватарка: {telebot.formatting.hcode(user.avatar)}
|
||||
Ваш публичный ключ: {telebot.formatting.hcode(user.pkey)}""",parse_mode="HTML")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
|
||||
###### Передача сообщений ########
|
||||
|
||||
@bot.message_handler(func=lambda message: True, content_types=['photo','text', 'document', 'voice', 'video'])
|
||||
def catch_all_messages(message):
|
||||
try:
|
||||
db = load()
|
||||
# Авторизован ли
|
||||
if not is_auth(bot, message):
|
||||
return 0
|
||||
nick = db[str(message.chat.id)]
|
||||
user = db[nick]
|
||||
avatar = ' ' + user.avatar
|
||||
if message.content_type == "text" and message.text[:1].lower() == ":":
|
||||
channel = message.text[1:]
|
||||
if channel in db:
|
||||
# Проверяем ключи
|
||||
if not key_valid(bot, message, channel):
|
||||
return 0
|
||||
|
||||
user.channel = channel
|
||||
bot.reply_to(message, "Установлен адресат: " + telebot.formatting.hcode(channel), parse_mode="HTML")
|
||||
save(db)
|
||||
else:
|
||||
bot.reply_to(message, "Не существует данного пользователя.")
|
||||
elif user.channel != None:
|
||||
channel = user.channel
|
||||
db = load()
|
||||
# Проверяем существование адресата
|
||||
if channel not in db:
|
||||
bot.reply_to(message, "Не существует данного пользователя.")
|
||||
return 0
|
||||
# Проверяем ключи
|
||||
if not key_valid(bot, message, channel):
|
||||
return 0
|
||||
|
||||
if message.chat.id not in db[channel].blocks:
|
||||
try:
|
||||
# Check if image
|
||||
if "photo" in message.json:
|
||||
img_id = message.json['photo'][0]['file_id']
|
||||
# Catch caption
|
||||
if "caption" in message.json:
|
||||
caption = "\n" + message.json['caption']
|
||||
else:
|
||||
caption = ""
|
||||
|
||||
bot.send_photo(db[channel].id, img_id, f"{telebot.formatting.hcode(':'+nick) + avatar}" + caption, parse_mode="HTML")
|
||||
|
||||
elif "document" in message.json:
|
||||
doc_id = message.json['document']['file_id']
|
||||
if "caption" in message.json:
|
||||
caption = "\n" + message.json['caption']
|
||||
else:
|
||||
caption = ""
|
||||
bot.send_document(db[channel].id, doc_id, caption = f"{telebot.formatting.hcode(':'+nick) + avatar}" + caption, parse_mode="HTML")
|
||||
elif "voice" in message.json:
|
||||
voice_id = message.json['voice']['file_id']
|
||||
bot.send_document(db[channel].id, voice_id, caption = f"{telebot.formatting.hcode(':'+nick) + avatar}", parse_mode="HTML")
|
||||
elif "video" in message.json:
|
||||
vid_id = message.json['video']['file_id']
|
||||
if "caption" in message.json:
|
||||
caption = "\n" + message.json['caption']
|
||||
else:
|
||||
caption = ""
|
||||
bot.send_video(db[channel].id, vid_id, caption = f"{telebot.formatting.hcode(':'+nick) + avatar}", parse_mode="HTML")
|
||||
else:
|
||||
try:
|
||||
bot.send_message(db[channel].id, f"{telebot.formatting.hcode(':'+nick) + avatar}\n" + message.text, parse_mode="HTML")
|
||||
except:
|
||||
catch_error(bot, message, 'spec_symb')
|
||||
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
bot.reply_to(message, "Сообщение не было доставлено.\nВероятно пользователь заблокировал бота.")
|
||||
else:
|
||||
bot.reply_to(message, "Увы, но вас заблокировал данный пользователь.")
|
||||
else:
|
||||
bot.reply_to(message, f"У вас не указан чат.\nЧтобы подключится к чату напишите: {telebot.formatting.hcode(':Никнейм')} ", parse_mode="HTML")
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
#### POLLING ####
|
||||
from sys import argv
|
||||
if len(argv) > 1:
|
||||
mode = "debug"
|
||||
else:
|
||||
mode = "normal"
|
||||
|
||||
if mode == "normal":
|
||||
while True:
|
||||
try:
|
||||
bot.polling()
|
||||
except KeyboardInterrupt:
|
||||
exit()
|
||||
except:
|
||||
pass
|
||||
elif mode == "debug":
|
||||
ic("Started debug...")
|
||||
bot.polling()
|
||||
28
catch_err.py
28
catch_err.py
@ -1,28 +0,0 @@
|
||||
import logging
|
||||
import traceback
|
||||
from io import StringIO # Для перевода лога в переменную
|
||||
|
||||
import telebot
|
||||
|
||||
# Базовая инициализация
|
||||
global log_stream
|
||||
log_stream = StringIO()
|
||||
logging.basicConfig(stream=log_stream)
|
||||
|
||||
def catch_error(bot, message, err_type = None):
|
||||
try:
|
||||
if not err_type:
|
||||
global log_stream
|
||||
|
||||
logging.error(traceback.format_exc()) # Логирование ошибок
|
||||
err = log_stream.getvalue() # Ошибка -> переменная
|
||||
|
||||
bot.reply_to(message, 'Critical error:\n\n' + telebot.formatting.hcode(err), parse_mode='HTML')
|
||||
|
||||
# Очистка логов
|
||||
log_stream.truncate(0)
|
||||
log_stream.seek(0)
|
||||
elif err_type == 'spec_symb':
|
||||
bot.reply_to(message, 'Невозможно отправить сообщение из-за специфических символов')
|
||||
except:
|
||||
pass
|
||||
68
db.py
68
db.py
@ -1,68 +0,0 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
# Создаём БД, если её нету
|
||||
if not os.path.exists('db.json'):
|
||||
db = {"token": "None"}
|
||||
js = json.dumps(db, indent=2)
|
||||
with open("db.json", "w") as outfile:
|
||||
outfile.write(js)
|
||||
print('Создана БД')
|
||||
print('Введите токен db.json')
|
||||
exit()
|
||||
|
||||
|
||||
# raw_db = {
|
||||
# "Anon": {"id": 2045634, pkey: "fuD2d", channel: "AnotherUser", avatar: "♿️", blocks: [5375652, 436432], keys: {AnotherUser: "dDH73s"}},
|
||||
# "2045634": "Anon"
|
||||
# }
|
||||
def read_db(file = 'db.json'):
|
||||
with open(file, "r", encoding="utf-8") as openfile:
|
||||
raw_db = json.load(openfile)
|
||||
return raw_db
|
||||
|
||||
def write_db(raw_db, file = 'db.json'):
|
||||
js = json.dumps(raw_db, indent=2, ensure_ascii=False)
|
||||
with open(file, "w", encoding="utf-8") as outfile:
|
||||
outfile.write(js)
|
||||
|
||||
|
||||
|
||||
from user import *
|
||||
# db = {
|
||||
# "Anon": user.*,
|
||||
# "2045634": "Anon"
|
||||
# }
|
||||
|
||||
def is_num(str):
|
||||
try:
|
||||
int(str)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def load():
|
||||
raw_db = read_db()
|
||||
db = {}
|
||||
for i in raw_db:
|
||||
if is_num(i) == True:
|
||||
db[i] = raw_db[i]
|
||||
elif "token" == i:
|
||||
db["token"] = raw_db["token"]
|
||||
else:
|
||||
id, pkey, channel, avatar, blocks, keys = raw_db[i]["id"], raw_db[i]["pkey"], raw_db[i]["channel"], raw_db[i]["avatar"], raw_db[i]["blocks"], raw_db[i]["keys"],
|
||||
user = user_(id, pkey, channel, avatar, blocks, keys)
|
||||
db[i] = user
|
||||
|
||||
return db
|
||||
|
||||
def save(db):
|
||||
raw_db = {}
|
||||
for i in db:
|
||||
if is_num(i) == True:
|
||||
raw_db[i] = db[i]
|
||||
elif "token" == i:
|
||||
raw_db["token"] = db["token"]
|
||||
else:
|
||||
raw_db[i] = {"id": db[i].id, "pkey": db[i].pkey, "channel": db[i].channel, "avatar": db[i].avatar, "blocks": db[i].blocks, "keys": db[i].keys}
|
||||
write_db(raw_db)
|
||||
97
func.py
97
func.py
@ -1,97 +0,0 @@
|
||||
#### Ловим ошибки ####
|
||||
from catch_err import *
|
||||
######### БД #########
|
||||
from db import *
|
||||
######################
|
||||
|
||||
# -> True/False
|
||||
def is_auth(bot, message):
|
||||
try:
|
||||
db = load()
|
||||
if str(message.chat.id) in db:
|
||||
return True
|
||||
else:
|
||||
bot.reply_to(message,"Извините, но Вы не авторизованы.\n\n/reg ник")
|
||||
return False
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
|
||||
# Регулярные выражения
|
||||
from re import sub, compile
|
||||
# -> True/False
|
||||
def nick_ok(bot, message, nick):
|
||||
try:
|
||||
if len(nick) > 30:
|
||||
bot.reply_to(message,"Слишком длинный ник, попробуйте короче.")
|
||||
return False
|
||||
if is_num(nick):
|
||||
bot.reply_to(message,"Ник должен содержать хоть 1 букву, попробуйте ещё раз.")
|
||||
return False
|
||||
|
||||
en = True
|
||||
ru = True
|
||||
# Если только английский
|
||||
regex = compile('[^a-zA-Z0-9]')
|
||||
check = regex.sub('', nick)
|
||||
if check != nick:
|
||||
en = False
|
||||
# Если только русский
|
||||
regex = compile('[^а-яА-ЯЁё0-9]')
|
||||
check = regex.sub('', nick)
|
||||
if check != nick:
|
||||
ru = False
|
||||
|
||||
if en == False and ru == False:
|
||||
bot.reply_to(message,"Нельзя смешивать алфавиты и ставить спец.-символы, попробуйте ещё раз")
|
||||
return False
|
||||
|
||||
return True
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
# Проверяем совпадение ключей при отправке сообщений
|
||||
# -> True/False
|
||||
def key_valid(bot, message, channel):
|
||||
try:
|
||||
db = load()
|
||||
our_nick = db[str(message.chat.id)]
|
||||
user = db[our_nick]
|
||||
# Добавляем ключ если его нету в нашей БД
|
||||
if channel not in user.keys:
|
||||
user.keys[channel] = db[channel].pkey
|
||||
save(db)
|
||||
return True
|
||||
|
||||
our_key = user.keys[channel]
|
||||
dest_key = db[channel].pkey
|
||||
|
||||
if our_key == dest_key:
|
||||
return True
|
||||
else:
|
||||
user.keys[channel] = dest_key
|
||||
save(db)
|
||||
|
||||
bot.reply_to(message, f"""⚠️ Публичные ключи не совпадают ⚠️
|
||||
Ожидаемый ключ: {telebot.formatting.hcode(our_key)}
|
||||
|
||||
Отправка сообщения отклонена.
|
||||
Если вы уверены - повторите отправку.
|
||||
""", parse_mode="HTML")
|
||||
return False
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
|
||||
|
||||
# Проверка на количество аргументов
|
||||
# ok_args(bot, message, 2, '/nick никнейм') + '/nick test' = True
|
||||
def ok_args(bot, message, count, mess):
|
||||
try:
|
||||
count_args = len(message.text.split())
|
||||
if not count_args == count:
|
||||
bot.reply_to(message, mess, parse_mode="Markdown")
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
except:
|
||||
catch_error(bot, message)
|
||||
6
hash.py
6
hash.py
@ -1,6 +0,0 @@
|
||||
import hashlib
|
||||
|
||||
def hash(string):
|
||||
string = str(string)
|
||||
hashed = hashlib.sha256(str.encode(string)).hexdigest()
|
||||
return hashed
|
||||
29
send.py
29
send.py
@ -1,29 +0,0 @@
|
||||
import json
|
||||
import telebot
|
||||
|
||||
def read_db():
|
||||
global db
|
||||
with open('db.json', 'r') as openfile:
|
||||
db = json.load(openfile)
|
||||
|
||||
read_db()
|
||||
bot = telebot.TeleBot(db['token'])
|
||||
|
||||
message = """
|
||||
Отправка сообщений была реализована, подписи тоже работают.
|
||||
"""
|
||||
|
||||
for i in db:
|
||||
if True:
|
||||
try:
|
||||
id=int(i)
|
||||
except:
|
||||
id=None
|
||||
|
||||
if id != None:
|
||||
try:
|
||||
# bot.send_message(id,message,parse_mode='Markdown')
|
||||
bot.send_message(id,message)
|
||||
print("Pass ", id)
|
||||
except:
|
||||
print("Error ",id)
|
||||
Loading…
Reference in New Issue
Block a user