Я пытаюсь писать бота и при нажатии на инлайн клавиатуре 'news_settings_handler' все проходит хорошо и появляется еще одна инлайн клавиатура с выбором настроек, но если я захожу в раздел "Язык", выбираю нужный язык и нажимаю 'Назад', то появляется ошибка:
line 25, in news_settings_handler
bot.send_message(call.message.chat.id, text='*Настройки:*', reply_markup=settings_markup, parse_mode="Markdown")
AttributeError: 'Message' object has no attribute 'message'
Вот мой код:
import telebot
from newsapi import NewsApiClient
from telebot import types
bot = telebot.TeleBot('токен')
newsapi = NewsApiClient(api_key='токен')
users={}
@bot.message_handler(commands=["start"])
def starting(message):
users[message.chat.id]={'category':'sports','language':'ru','country':''}
print(users)
bot.send_message(message.chat.id, text='Привет! Я новостной бот пропиши команду /menu , чтобы начать!')
@bot.message_handler(commands=["menu"])
def main_menu(message):
markup=types.InlineKeyboardMarkup(row_width=1)
markup.add(types.InlineKeyboardButton('Смотреть новости',callback_data='CheckNews'),
types.InlineKeyboardButton('Настройки', callback_data='Settings'),
)
bot.send_message(message.chat.id, text='*Меню:*', reply_markup=markup,parse_mode="Markdown")
@bot.callback_query_handler(func=lambda call:call.data=='Settings')
def news_settings_handler(call: types.CallbackQuery):
settings_markup = types.InlineKeyboardMarkup(row_width=1)
settings_markup.add(types.InlineKeyboardButton('Язык', callback_data='Language_settings'),
types.InlineKeyboardButton('Категория', callback_data='Category_settings'),
types.InlineKeyboardButton('Страна', callback_data='Country_settings'))
bot.send_message(call.message.chat.id, text='*Настройки:*', reply_markup=settings_markup, parse_mode="Markdown")
@bot.callback_query_handler(func=lambda call: call.data == "CheckNews")
def news_handler(call: types.CallbackQuery):
titles=[]
descriptions=[]
var_id=users.get(call.message.chat.id)
print(var_id)
category_var =var_id.get('category','')
language_var = var_id.get('language','')
country_var = var_id.get('country','')
if category_var=='' and country_var=='':
top_headlines = newsapi.get_top_headlines(language=language_var)
for article in top_headlines['articles']:
titles.append(article['title'])
descriptions.append(article['description'])
for i in range(len(titles)):
bot.send_message(call.message.chat.id, text='*' + titles[i] + '*', parse_mode="Markdown")
bot.send_message(call.message.chat.id, text=descriptions[i] )
elif category_var == '' and country_var!='':
top_headlines = newsapi.get_top_headlines(language=language_var,country=country_var)
for article in top_headlines['articles']:
titles.append(article['title'])
descriptions.append(article['description'])
for i in range(len(titles)):
bot.send_message(call.message.chat.id, text='*' + titles[i] + '*', parse_mode="Markdown")
bot.send_message(call.message.chat.id, text=descriptions[i] )
elif category_var != '' and country_var=='':
top_headlines = newsapi.get_top_headlines(language=language_var,category=category_var)
for article in top_headlines['articles']:
titles.append(article['title'])
descriptions.append(article['description'])
for i in range(len(titles)):
bot.send_message(call.message.chat.id, text='*' + titles[i] + '*', parse_mode="Markdown")
bot.send_message(call.message.chat.id, text=descriptions[i] )
elif category_var != '' and country_var != '':
top_headlines = newsapi.get_top_headlines(language=language_var,category=category_var,country=country_var)
for article in top_headlines['articles']:
titles.append(article['title'])
descriptions.append(article['description'])
for i in range(len(titles)):
bot.send_message(call.message.chat.id, text='*' + titles[i] + '*', parse_mode="Markdown")
bot.send_message(call.message.chat.id, text=descriptions[i] )
@bot.callback_query_handler(func=lambda call: call.data == "Language_settings")
def settings_handler(call: types.CallbackQuery):
language_markup = types.InlineKeyboardMarkup(row_width=1)
language_markup.add(types.InlineKeyboardButton('Русский', callback_data='choise_language:ru'),
types.InlineKeyboardButton('Английский', callback_data='choise_language:en'),
types.InlineKeyboardButton('Немецкий', callback_data='choise_language:de'),
types.InlineKeyboardButton('Назад', callback_data='choise_language:back'))
bot.send_message(call.message.chat.id,
text='*На каком языке вы хотите читать новости?*',
reply_markup=language_markup,
parse_mode="Markdown")
@bot.callback_query_handler(func=lambda call: call.data.startswith("choise_language"))
def language_handler(call: types.CallbackQuery):
result = call.data.split(":")[1]
if result == "back":
news_settings_handler(call.message)
return
users[call.message.chat.id]['language'] = result
print(users)
@bot.callback_query_handler(func=lambda call: call.data == "Category_settings")
def settings_handler(call: types.CallbackQuery):
category_markup = types.InlineKeyboardMarkup(row_width=2)
category_markup.add(types.InlineKeyboardButton('Спорт', callback_data='choise_category:sport'),
types.InlineKeyboardButton('Бизнес', callback_data='choise_category:business'),
types.InlineKeyboardButton('Развлечения', callback_data='choise_category:entertainment'),
types.InlineKeyboardButton('Общее', callback_data='choise_category:general'),
types.InlineKeyboardButton('Медицина', callback_data='choise_category:health'),
types.InlineKeyboardButton('Наука', callback_data='choise_category:science'),
types.InlineKeyboardButton('Технологии', callback_data='choise_category:technology'),
types.InlineKeyboardButton('Все категории', callback_data='choise_category:Default'),
types.InlineKeyboardButton('Назад', callback_data='choise_category:back'))
bot.send_message(call.message.chat.id,
text='*Какие новости вы хотите читать?*',
reply_markup=category_markup,
parse_mode="Markdown")
@bot.callback_query_handler(func=lambda call: call.data.startswith("choise_category"))
def category_handler(call: types.CallbackQuery):
result = call.data.split(":")[1]
if result == "Default":
users[call.message.from_user.id]['category'] = ''
return
elif result == "back":
news_settings_handler(call.message)
return
users[call.message.chat.id]['category'] = result
@bot.callback_query_handler(func=lambda call: call.data == "Country_settings")
def settings_handler(call: types.CallbackQuery):
country_markup = types.InlineKeyboardMarkup(row_width=2)
country_markup.add(types.InlineKeyboardButton('Китай', callback_data='choise_country:ch'),
types.InlineKeyboardButton('США', callback_data='choise_country:us'),
types.InlineKeyboardButton('Россия', callback_data='choise_country:ru'),
types.InlineKeyboardButton('Польша', callback_data='choise_country:pl'),
types.InlineKeyboardButton('Украина', callback_data='choise_country:ua'),
types.InlineKeyboardButton('Германия', callback_data='choise_country:de'),
types.InlineKeyboardButton('Назад', callback_data='choise_country:back'))
bot.send_message(call.message.chat.id,
text='*Новости из какой страны вы хотите читать?*',
reply_markup=country_markup,
parse_mode="Markdown")
@bot.callback_query_handler(func=lambda call: call.data.startswith("choise_country"))
def country_handler(call: types.CallbackQuery):
result = call.data.split(":")[1]
users[call.message.chat.id]['country'] = result
if result == "back":
news_settings_handler(call.message)
return
if __name__ == '__main__':
bot.polling(none_stop=True)