import telebot
from telebot import types
import os
API_TOKEN = ''
admin_id =
channel_id =
bot = telebot.TeleBot(API_TOKEN)
p_inform = ''
v_inform = ''
def start_kb():
kb_start = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True, row_width=5)
btn1 = types.KeyboardButton("Послать фото")
btn2 = types.KeyboardButton("Послать видео")
kb_start.add(btn1, btn2)
return kb_start
def wait_kb():
kb_start = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True, row_width=5)
btn1 = types.KeyboardButton("/start")
kb_start.add(btn1)
return kb_start
def a_d_kb():
kb = types.ReplyKeyboardMarkup()
a = types.KeyboardButton('Одобрить')
d = types.KeyboardButton('Проклясть')
kb.add(a,d)
return kb
def terminate(cur):
f=open('logs.txt','r').readlines()
for i in f:
if i == cur:
f.pop(i)
with open('logs.txt','w') as F:
F.writelines(f)
return 'Успешно'
def first():
with open('logs.txt', 'r', encoding='utf-8') as f:
return f.readline()
current_file, current_mess_author, current_mess_info, current_mess_name = '', '', '', ''
@bot.message_handler(commands=['start'])
def start(mes):
if mes.chat.id != admin_id:
bot.send_message(mes.chat.id, text='Приветствую тебя! Друг)', reply_markup=start_kb())
else:
kb = types.ReplyKeyboardMarkup()
a = types.KeyboardButton('Поработать')
kb.add(a)
bot.send_message(mes.chat.id, text='ПРивет, ПАпОЧКа', reply_markup=kb)
def log(content, id, info):
with open('logs.txt', 'a', encoding='utf-8') as file:
file.write('\n'+ content + '|' + info + '|@' + id)
@bot.message_handler(content_types=['text'])
def text(mes):
if mes.text == 'Послать фото':
bot.send_message(mes.chat.id, text=f'Подпиши фото')
bot.register_next_step_handler(mes, p_info)
if mes.text == 'Послать видео':
bot.send_message(mes.chat.id, text=f'Подпиши видео')
bot.register_next_step_handler(mes, v_info)
if mes.text == "Поработать":
if os.path.getsize(r"C:\Users\temai\test2\logs.txt") == 0:
bot.send_message(admin_id, text='Новых публикаций нет', reply_markup=[wait_kb])
else:
global current_file, current_mess_author, current_mess_info, current_mess_name
current_file = first()
current_mess_name = str(current_file.split('|')[0])
current_mess_info = str(current_file.split('|')[1])
current_mess_author = str(current_file.split('|')[2])
if current_mess_name[0] == 'p':
photo = open(r"C:\Users\temai\test2\images" + f"\{current_mess_name}", 'rb')
bot.send_photo(admin_id, photo, caption=f'текст к фото:{current_mess_info}, создатель: {current_mess_author}', reply_markup=a_d_kb)
if current_mess_name[0] == 'v':
video = open(r"C:\Users\temai\test2\video" + f"\{current_mess_name}", 'rb')
bot.send_video(admin_id, video, caption=f'текст к видео:{current_mess_info}, создатель: {current_mess_author}', reply_markup=a_d_kb)
if mes.text == 'Одобрить':
if current_file[0] == 'p':
photo = open(r"C:\Users\temai\test2\images" + f"\{current_mess_name}", 'rb')
bot.send_photo(channel_id, photo, caption=f'{current_mess_info}\nCоздатель: {current_mess_author}')
os.remove(r"C:\Users\temai\test2\images" + f"\{current_mess_name}")
terminate(current_file)
if current_file[0] == 'v':
video = open(r"C:\Users\temai\test2\video" + f"\{current_mess_name}", 'rb')
bot.send_video(channel_id, video, caption=f'{current_mess_info}\nCоздатель: {current_mess_author}')
os.remove(r"C:\Users\temai\test2\video" + f"\{current_mess_name}")
terminate(current_file)
if mes.text == 'Проклясть':
os.remove(r"C:\Users\temai\test2\images" + f"\{current_mess_name}")
terminate(current_file)
def p_info(mes):
global p_inform
p_inform = mes.text
bot.send_message(mes.chat.id, 'Пришли свое фото')
bot.register_next_step_handler(mes, reg_photo)
def v_info(mes):
global v_inform
v_inform = mes.text
bot.send_message(mes.chat.id, text='Пришли свое видео')
bot.register_next_step_handler(mes, reg_video)
@bot.message_handler(content_types=['photo'])
def reg_photo(mes):
file_info = bot.get_file(mes.photo[len(mes.photo) - 1].file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = r"C:\Users\temai\test2\images\photo_" + mes.photo[1].file_id
with open(src, 'wb') as new_file:
new_file.write(downloaded_file)
log('photo_'+mes.photo[1].file_id, mes.chat.username, p_inform)
bot.send_message(mes.chat.id, text='Ваше фото в очереди на одобрение',reply_markup=start_kb())
@bot.message_handler(content_types=['video'])
def reg_video(mes):
file_info = bot.get_file(mes.video.file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = r"C:\Users\temai\test2\video\vid_" + mes.video.file_id
with open(src, 'wb') as new_file:
new_file.write(downloaded_file)
log('vid_'+ mes.video.file_id, mes.chat.username, v_inform)
bot.send_message(mes.chat.id, text='Ваше видео в очереди на одобрение',reply_markup=start_kb())
bot.infinity_polling()
错误:
2023-11-24 20:38:53,130 (__init__.py:1083 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: can't parse reply keyboard markup JSON object"
2023-11-24 20:38:53,135 (__init__.py:1085 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\__init__.py", line 1074, in __threaded_polling
self.worker_pool.raise_exceptions()
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\util.py", line 147, in raise_exceptions
raise self.exception_info
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\util.py", line 90, in run
task(*args, **kwargs)
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\__init__.py", line 6801, in _run_middlewares_and_handler
result = handler['function'](message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\temai\test2\main.py", line 75, in text
bot.send_message(admin_id, text='Новых публикаций нет', reply_markup=[wait_kb])
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\__init__.py", line 1549, in send_message
apihelper.send_message(
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\apihelper.py", line 264, in send_message
return _make_request(token, method_url, params=payload, method='post')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\apihelper.py", line 162, in _make_request
json_result = _check_result(method_name, result)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\temai\test2\env\Lib\site-packages\telebot\apihelper.py", line 189, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: can't parse reply keyboard markup JSON object
"
大家好,我正在创建一个机器人,看起来快完成了,但是这里有一些疯狂的事情,请帮助我,告诉我可能导致错误的原因是什么?