Answer the question
In order to leave comments, you need to log in
Python Telegram Bot. How to remove the user's user ID from a text file when encountering a 403 error (bot in chs)?
import telebot
import time
import schedule
import threading
from telebot import apihelper
@bot.message_handler(commands=['start'])
def welcome(message):
# База данных
user_id = message.from_user.id
print(user_id)
def add_id(filename: str, client_id: str) -> None:
ids = set()
try:
with open(filename, "r") as f_read:
ids.update([id_n.strip() for id_n in f_read.readlines()])
except FileNotFoundError:
pass
ids.update([client_id])
with open(filename, "w") as f_write:
f_write.writelines([new_id + "\n" for new_id in ids])
def check_id(filename: str, client_id: str) -> bool:
try:
with open(filename, "r") as f_read:
return client_id in [id_n.strip() for id_n in f_read.readlines()]
except FileNotFoundError:
return False
add_id("ids.txt", str(user_id))
def get_all_ids(filename: str) -> bool:
try:
with open(filename, "r") as f_read:
return [id_n.strip() for id_n in f_read.readlines()]
except FileNotFoundError:
return False
def thread(my_func):
def wrapper(*args, **kwargs):
my_thread = threading.Thread(target=my_func, args=args, kwargs=kwargs)
my_thread.start()
return wrapper
def job_utro():
for client_id in get_all_ids("ids.txt"):
try:
bot.send_message(int(client_id), "Доброе утро!")
except Exception as e:
print(repr(e))
print("Неудачка_утро\nПользователь добавил бота в Черный Список...\n")
@thread
def vremya_utro():
schedule.every().day.at("08:00").do(job_utro)
vremya_utro()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question