Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit with homework #170

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions 8_ephem_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,43 @@
бота отвечать, в каком созвездии сегодня находится планета.

"""
import logging
import logging, ephem, settings, datetime

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

logging.basicConfig(format='%(name)s - %(levelname)s - %(message)s',
level=logging.INFO,
filename='bot.log')


"""
PROXY = {
'proxy_url': 'socks5://t1.learn.python.ru:1080',
'urllib3_proxy_kwargs': {
'username': 'learn',
'password': 'python'
}
}
"""

def get_constellation(update, context):
try:
user_planet = update.message.text.split()[1].lower()
Copy link

@maxmokry maxmokry Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот тут ты обрабатываешь исключение IndexError. Это хорошо

И тут вижу приведение к нижнему регистру. Это хорошо

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а я сделал исключение вот так:
except IndexError:
update.message.reply_text('ERROR! You do not determine the planet')
плохое решение?

current_date = datetime.datetime.now().strftime('%Y/%m/%d')
planets = {
'mercury': ephem.Mercury(), 'venus':ephem.Venus(), 'mars':ephem.Mars(),'jupiter':ephem.Jupiter(),
'saturn':ephem.Saturn(), 'uranus':ephem.Uranus(), 'neptun':ephem.Neptune()
}

if user_planet != '':
planet = planets.get(user_planet)
compute = planet.compute(current_date)
update.message.reply_text(ephem.constellation(planet)[1])
except IndexError:
update.message.reply_text('ERROR! You do not determine the planet')

u = ephem.planet
print()
ephem.constellation

def greet_user(update, context):
text = 'Вызван /start'
Expand All @@ -39,14 +59,15 @@ def greet_user(update, context):
def talk_to_me(update, context):
user_text = update.message.text
print(user_text)
update.message.reply_text(text)
update.message.reply_text(user_text)


def main():
mybot = Updater("КЛЮЧ, КОТОРЫЙ НАМ ВЫДАЛ BotFather", request_kwargs=PROXY, use_context=True)
mybot = Updater(settings.API_KEY)

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))

mybot.start_polling()
Expand Down
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_KEY = "7475700629:AAHvNdrcCzOdt2odHBippgfS7rFns1wAGQc"