'Message' object has no attribute 'message'
Я использую PyTelegramBotAPI. Участок кода:
@bot.callback_query_handler(func=lambda
c: c.data == 'tf' or c.data == 'tv' or c.data == 'tt' or c.data == 'if' or c.data == 'iv' or c.data == 'pa' or c.data == 'n')
def callback1(call):
if call.message:
if call.data == 'tf':
def messjpg(message):
def start(message):
msg = bot.send_message(message.chat.id, 'Отправьте мне сообщение')
bot.register_next_step_handler(msg, start_2)
def start_2(message):
global txt
print('id пользователя', message.chat.id)
print('имя пользователя', message.from_user.first_name)
print('фамилия пользователя', message.from_user.last_name)
print('никнейм пользователя', message.from_user.username)
print('сообщение пользователя', message.text)
txt = message.text
txt = str(txt)
joinedFile = open(r'C:\Users\serge\Videos\dfgdfgdfg\joined.txt', 'r')
joinedUsers = set()
for line in joinedFile:
joinedUsers.add(line.strip())
if txt != 'None':
for user in joinedUsers:
bot.send_message(user, txt)
else:
pass
try:
for user in joinedUsers:
bot.send_photo(user, photo=open('photo.jpg', 'rb'))
except:
pass
joinedFile.close()
start(message)
elif call.data == 'tv':
def messmp4(message):
def start(message):
msg = bot.send_message(message.chat.id, 'Отправьте мне сообщение')
bot.register_next_step_handler(msg, start_2)
def start_2(message):
global txt
print('id пользователя', message.chat.id)
print('имя пользователя', message.from_user.first_name)
print('фамилия пользователя', message.from_user.last_name)
print('никнейм пользователя', message.from_user.username)
print('сообщение пользователя', message.text)
txt = message.text
txt = str(txt)
joinedFile = open(r'C:\Users\serge\Videos\dfgdfgdfg\joined.txt', 'r')
joinedUsers = set()
for line in joinedFile:
joinedUsers.add(line.strip())
if txt != 'None':
for user in joinedUsers:
bot.send_message(user, txt)
else:
pass
try:
for user in joinedUsers:
bot.send_video(user, video=open('video.mp4', 'rb'))
except:
pass
joinedFile.close()
start(message)
elif call.data == 'tt':
def mess(message):
def start(message):
msg = bot.send_message(message.chat.id, 'Отправьте мне сообщение')
bot.register_next_step_handler(msg, start_2)
def start_2(message):
global txt
print('id пользователя', message.chat.id)
print('имя пользователя', message.from_user.first_name)
print('фамилия пользователя', message.from_user.last_name)
print('никнейм пользователя', message.from_user.username)
print('сообщение пользователя', message.text)
txt = message.text
txt = str(txt)
joinedFile = open(r'C:\Users\serge\Videos\dfgdfgdfg\joined.txt', 'r')
joinedUsers = set()
for line in joinedFile:
joinedUsers.add(line.strip())
if txt != 'None':
for user in joinedUsers:
bot.send_message(user, txt)
joinedFile.close()
start(message)
elif call.data == 'if':
def photojpg(message):
def start(message):
msg = bot.send_message(message.chat.id, 'Отправьте мне фото')
bot.register_next_step_handler(msg, start_2)
def start_2(message):
try:
remove('photo.jpg')
file_id = message.photo[-1].file_id
file = bot.get_file(file_id)
downloaded_file = bot.download_file(file.file_path)
with open("photo.jpg", 'wb') as f:
f.write(downloaded_file)
except (FileNotFoundError, AttributeError, TypeError):
file_id = message.photo[-1].file_id
file = bot.get_file(file_id)
downloaded_file = bot.download_file(file.file_path)
with open("photo.jpg", 'wb') as f:
f.write(downloaded_file)
else:
file_id = message.photo[-1].file_id
file = bot.get_file(file_id)
downloaded_file = bot.download_file(file.file_path)
with open("photo.jpg", 'wb') as f:
f.write(downloaded_file)
start(message)
elif call.data == 'iv':
def videomp4(message):
def start(message):
msg = bot.send_message(message.chat.id, 'Отправьте мне видео')
bot.register_next_step_handler(msg, start_2)
def start_2(message):
try:
remove('video.mp4')
file_id = message.video.file_id
file = bot.get_file(file_id)
downloaded_file = bot.download_file(file.file_path)
with open("video.mp4", 'wb') as f:
f.write(downloaded_file)
except (TypeError, FileNotFoundError, AttributeError):
file_id = message.video.file_id
file = bot.get_file(file_id)
downloaded_file = bot.download_file(file.file_path)
with open("video.mp4", 'wb') as f:
f.write(downloaded_file)
else:
file_id = message.video.file_id
file = bot.get_file(file_id)
downloaded_file = bot.download_file(file.file_path)
with open("video.mp4", 'wb') as f:
f.write(downloaded_file)
start(message)
elif call.data == 'pa':
def anek(message):
joinedFile = open(r'C:\Users\serge\Videos\dfgdfgdfg\joined.txt', 'r')
joinedUsers = set()
for line in joinedFile:
joinedUsers.add(line.strip())
try:
response = requests.get('https://baneks.ru/random').content
html = BeautifulSoup(response, 'lxml')
anek = html.find(class_='anek-view')
anek = anek.p
anek = anek.text
for user in joinedUsers:
bot.send_message(user, anek)
except AttributeError:
try:
response = requests.get('https://baneks.ru/random').content
html = BeautifulSoup(response, 'lxml')
anek1 = html.find(class_='anek-view')
anek1 = anek1.p
anek1 = anek1.text
for user in joinedUsers:
bot.send_message(user, anek1)
except AttributeError:
try:
response = requests.get('https://baneks.ru/random').content
html = BeautifulSoup(response, 'lxml')
anek2 = html.find(class_='anek-view')
anek2 = anek2.p
anek2 = anek2.text
for user in joinedUsers:
bot.send_message(user, anek2)
except AttributeError:
try:
response = requests.get('https://baneks.ru/random').content
html = BeautifulSoup(response, 'lxml')
anek3 = html.find(class_='anek-view')
anek3 = anek3.p
anek3 = anek3.text
for user in joinedUsers:
bot.send_message(user, anek3)
except AttributeError:
try:
response = requests.get('https://baneks.ru/random').content
html = BeautifulSoup(response, 'lxml')
anek4 = html.find(class_='anek-view')
anek4 = anek4.p
anek4 = anek4.text
for user in joinedUsers:
bot.send_message(user, anek4)
except AttributeError:
response = requests.get('https://baneks.ru/random').content
html = BeautifulSoup(response, 'lxml')
anek5 = html.find(class_='anek-view')
anek5 = anek5.p
anek5 = anek5.text
for user in joinedUsers:
bot.send_message(user, anek5)
joinedFile.close()
elif call.data == 'n':
def welcome(message):
global joinedFile, joinedUsers
joinedFile = open(r'C:\Users\serge\Videos\dfgdfgdfg\joined.txt', 'r')
joinedUsers = set()
for line in joinedFile:
joinedUsers.add(line.strip())
text = message.text.lower()
chatId = message.chat.id
if not str(message.chat.id) in joinedFile:
joinedFile = open(r'C:\Users\serge\Videos\dfgdfgdfg\joined.txt', 'a')
joinedFile.write(f'{str(message.chat.id)} {message.from_user.username} \n')
joinedUsers.add(message.chat.id)
joinedFile.close()
else:
pass
joinedFile.close()
keyboard = telebot.types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True, one_time_keyboard=False)
b_1 = telebot.types.KeyboardButton('Посоветовать игру')
b_2 = telebot.types.KeyboardButton('Фактовый')
b_3 = telebot.types.KeyboardButton('Прислать Санбоя')
b_4 = telebot.types.KeyboardButton('Котовый \n (И не только...)')
b_5 = telebot.types.KeyboardButton('Я в своём познании настолько преисполнился...')
keyboard.add(b_1, b_2, b_3, b_4, b_5)
bot.send_message(message.chat.id, '...', reply_markup=keyboard)
print(text)
выдает ошибку 'Message' object has no attribute 'message' в call.message в
def callback1(call):
if call.message:
if call.data == 'tf':
либо 'Message' object has no attribute 'data' в call.data, если call.message заменить на call. Если заменить call.message и call.data на call, то ошибки не будет, но и кнопки работать не будут. Тут я добавляю кнопки:
@bot.message_handler(commands=['5ryyc6sgadmin'])
def admin1(message):
chatId = message.chat.id
text = message.text.lower()
keyboard = telebot.types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True, one_time_keyboard=False)
markup1 = telebot.types.InlineKeyboardMarkup(row_width = 2)
b_1 = telebot.types.InlineKeyboardButton('Текст и фото', callback_data='tf')
b_2 = telebot.types.InlineKeyboardButton('Текст и видео', callback_data='tv')
b_3 = telebot.types.InlineKeyboardButton('Только текст', callback_data='tt')
b_4 = telebot.types.InlineKeyboardButton('Изменить фото', callback_data='if')
b_5 = telebot.types.InlineKeyboardButton('Изменить видео', callback_data='iv')
b_6 = telebot.types.InlineKeyboardButton('Прислать анек', callback_data='pa')
b_7 = telebot.types.InlineKeyboardButton('Назад', callback_data='n')
markup1.add(b_1, b_2, b_3, b_4, b_5, b_6, b_7)
bot.send_message(chatId, 'Что сделать?', reply_markup=markup1)
print(text)
У меня есть еще один callback_query_handler, но в нем ошибок нет, просто перестали работать кнопки после добавления выше описанного. Тут я пароль проверяю:
@bot.message_handler(commands=['admin'])
def admin(message):
def start(message):
msg = bot.send_message(message.chat.id, 'Пароль:')
bot.register_next_step_handler(msg, start_2)
def start_2(message):
if message.text == 'ache':
admin1(message)
callback1(message)
elif message.text != 'ache':
welcome(message)
start(message)