import builtins
import logging
import ephem
import datetime
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

import settings

logging.basicConfig(filename='bot.log', level=logging.INFO)

PROXY = {'proxy_url': settings.PROXY_URL,
    'urllib3_proxy_kwargs': {'username': settings.PROXY_USERNAME, 'password': settings.PROXY_PASSWORD}}

def greet_user(update, context):
    print ('Вызван /start')
    update.message.reply_text('Вызов "/start"')

def talk_to_me(update, context):
    user_text = update.message.text
    print(user_text)
    update.message.reply_text(user_text)

def get_constellation(update, context):
    planet_list =[]
    for i in ephem._libastro.builtin_planets():
        planet_list.append(i[2])
    user_text = update.message.text
    planet = user_text.split(' ')[1].title()
    date_now = datetime.datetime.today()
    if planet in planet_list:
        ephem_planet = getattr(ephem, planet)(date_now)
        constellation = ephem.constellation(ephem_planet)
        update.message.reply_text(constellation)
    else:
        update.message.reply_text(f'Выберите планету из списка: {planet_list}')

def main():
    mybot = Updater(settings.API_KEY, use_context=True, request_kwargs=PROXY)

    dp = mybot.dispatcher

    dp.add_handler(CommandHandler('start', greet_user))
    dp.add_handler(CommandHandler('planet', get_constellation))
    dp.add_handler(MessageHandler(Filters.text, talk_to_me))
    
    logging.info('Бот стартовал')
    mybot.start_polling()
    mybot.idle()

if __name__ == '__main__':
    main()
