-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (99 loc) · 4.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.types import ReplyKeyboardMarkup
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram import Bot, types
from config import bot_token
from MessageText import START_COMMAND, HELP_COMMAND
from Weather import GetWeather
import logging
from create_connection import connection
from transliterate import translit
bot = Bot(token=bot_token)
dp = Dispatcher(bot, storage=MemoryStorage())
logger = logging.getLogger(__name__)
keyboard = ReplyKeyboardMarkup(resize_keyboard=True)
buttons = ['Помощь', 'Новости', 'Курсы валют', 'Погода']
keyboard.add(*buttons)
class WeatherForm(StatesGroup):
city = State()
class RateForm(StatesGroup):
rate_symbol = State()
@dp.message_handler(commands=['start'])
async def start_command(message: types.Message):
await bot.send_message(
reply_markup=keyboard,
chat_id=message.from_user.id,
text=START_COMMAND,
parse_mode='HTML'
)
@dp.message_handler(lambda message: message.text == 'Помощь')
async def process_help_command(message: types.Message):
await bot.send_message(
chat_id=message.from_user.id,
text=HELP_COMMAND,
parse_mode='HTML'
)
@dp.message_handler(lambda message: message.text == 'Новости')
async def news_command(message: types.Message):
conn = connection()
cur = conn.cursor()
try:
cur.execute('SELECT title, text, url FROM FinNews')
for news in cur.fetchall():
news_obj = f'\t{news[0]}\n{news[1]}\nИсточник: {news[2]}'
await bot.send_message(message.from_id, news_obj)
except:
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
logging.error(f'news_command {message.from_user.id}-{message.from_user.first_name}')
finally:
conn.close()
@dp.message_handler(lambda message: message.text == 'Курсы валют')
async def rate_handler(message: types.Message):
await RateForm.rate_symbol.set()
await message.answer('Введите символ валюты')
@dp.message_handler(lambda message: message.text == 'Погода')
async def weather_handler(message: types.Message):
await WeatherForm.city.set()
await message.answer('Введите название города')
@dp.message_handler(state='*', commands='cancel')
@dp.message_handler(Text(equals='отмена', ignore_case=True), state='*')
async def cancel_handler(message: types.Message, state: FSMContext):
current_state = await state.get_state()
if current_state is None:
return
await state.finish()
await message.reply('ОК')
@dp.message_handler(state=WeatherForm.city)
async def weather_command(message: types.Message, state: FSMContext):
en_city = translit(message.text, reversed=True)
try:
for obj in GetWeather(en_city).__call__():
await bot.send_message(message.from_id, obj)
except:
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
logging.error(f'weather_command {message.from_user.id}-{message.from_user.first_name} -> {en_city}')
finally:
await state.finish()
@dp.message_handler(state=RateForm.rate_symbol)
async def rate_command(message: types.Message, state: FSMContext):
symbol = message.text.replace(' ', '').upper()
conn = connection()
cur = conn.cursor()
try:
cur.execute(f"SELECT rate_text, sum_rub FROM Rates WHERE rate_symbol = '{symbol}'")
for rate in cur.fetchall():
await bot.send_message(message.from_id, f'{rate[0]} в рублях = {rate[1]}')
break
except:
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
logging.error(f'rate_command {message.from_user.id}-{message.from_user.first_name} -> {symbol}')
await message.answer('Простите но вы ввели несуществующий символ')
finally:
conn.close()
await state.finish()
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)