-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
255 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
API_KEY='' | ||
BOT_TOKEN='' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
images = { | ||
"clear sky": "https://te.legra.ph/file/59fb2206eef46231322fd.jpg", | ||
"smoke": "https://te.legra.ph/file/fb713f2fc775f2d410150.jpg", | ||
"scattered clouds": "https://te.legra.ph/file/fb713f2fc775f2d410150.jpg", | ||
"few clouds": "https://te.legra.ph/file/fb713f2fc775f2d410150.jpg" | ||
} | ||
|
||
welcome_gif = "https://x0.at/GfoN.mp4" | ||
send_welcome = """ | ||
🪄 <b>Heeey! I am a weather bot. Enter the name of your city.</b> | ||
<b>Enter this command:</b> /weather_city <b>and send the name of your city</b> | ||
<b>Then send this command to view the weather</b>: /weather | ||
""" | ||
|
||
city_is_not_set = """ | ||
<b>🚫 Default city is not set. Use the command</b> /weather_city <b>city to set a default city.</b> | ||
""" | ||
|
||
send_weather = """ | ||
<b>❔ Here is the weather in your city</b>: | ||
<pre><code class='language-weather'>🌤 <b>City</b>: {} | ||
{} <b>Temperature:</b> <u>{}°C</u> | ||
💧 <b>Humidity</b>: {}% | ||
💨 <b>Wind speed</b>: {} m/s | ||
☀️ <b>Description:</b> {}</code></pre> | ||
""" |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import sqlite3 | ||
|
||
class Database: | ||
def __init__(self, db_path='database/data.db'): | ||
self.conn = sqlite3.connect(db_path) | ||
self.cursor = self.conn.cursor() | ||
self.create_table() | ||
|
||
def create_table(self): | ||
self.cursor.execute(''' | ||
CREATE TABLE IF NOT EXISTS users ( | ||
user_id INTEGER PRIMARY KEY, | ||
username TEXT, | ||
nickname TEXT, | ||
city TEXT | ||
) | ||
''') | ||
self.conn.commit() | ||
|
||
def close(self): | ||
self.conn.close() | ||
|
||
def add_user(self, user_id, username, nickname): | ||
self.cursor.execute( | ||
"INSERT OR IGNORE INTO users (user_id, username, nickname) VALUES (?, ?, ?)", | ||
(user_id, username, nickname) | ||
) | ||
self.conn.commit() | ||
|
||
def update_user_city(self, user_id, city): | ||
self.cursor.execute( | ||
"UPDATE users SET city = ? WHERE user_id = ?", | ||
(city, user_id) | ||
) | ||
self.conn.commit() | ||
|
||
def get_user_city(self, user_id): | ||
self.cursor.execute( | ||
"SELECT city FROM users WHERE user_id = ?", | ||
(user_id,) | ||
) | ||
result = self.cursor.fetchone() | ||
return result[0] if result else None | ||
|
||
def get_all_users(self): | ||
self.cursor.execute("SELECT * FROM users") | ||
return self.cursor.fetchall() | ||
|
||
def get_user_by_id(self, user_id): | ||
self.cursor.execute( | ||
"SELECT * FROM users WHERE user_id = ?", | ||
(user_id,) | ||
) | ||
return self.cursor.fetchone() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import aiohttp | ||
|
||
from os import getenv | ||
from aiogram import Router | ||
from aiogram.filters import Command, CommandStart | ||
from aiogram.types import Message, ReplyKeyboardRemove | ||
from aiogram.fsm.context import FSMContext | ||
|
||
from database.db import Database | ||
from states.user_states import WeatherState | ||
from keyboards.keyboard import get_cancel_keyboard | ||
from config.config import city_is_not_set, send_weather, images, welcome_gif, send_welcome | ||
|
||
router = Router() | ||
db = Database() | ||
|
||
@router.message(CommandStart()) | ||
async def send_welcome_message(message: Message): | ||
await message.answer_video(video=welcome_gif, caption=send_welcome) | ||
|
||
@router.message(Command("weather_city")) | ||
async def prompt_for_city(message: Message, state: FSMContext): | ||
await message.answer("Please enter your city:", reply_markup=get_cancel_keyboard()) | ||
await state.set_state(WeatherState.waiting_for_city) | ||
|
||
@router.message(WeatherState.waiting_for_city) | ||
async def process_city_submission(message: Message, state: FSMContext): | ||
city = message.text | ||
|
||
user_id = message.from_user.id | ||
username = message.from_user.username | ||
nickname = message.from_user.first_name | ||
|
||
db.add_user(user_id, username, nickname) | ||
db.update_user_city(user_id, city) | ||
|
||
if city == "🚫 Cancel": | ||
await state.clear() | ||
await message.reply("OK") | ||
else: | ||
await message.answer(f"<b>🫶 Your current city</b>: <code>{city}</code>", reply_markup=ReplyKeyboardRemove()) | ||
await state.clear() | ||
|
||
@router.message(Command("weather")) | ||
async def send_weather_info(message: Message): | ||
user_id = message.from_user.id | ||
city = db.get_user_city(user_id) | ||
|
||
if not city: | ||
return await message.answer(city_is_not_set) | ||
|
||
api_key = getenv('API_KEY') | ||
api_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" | ||
|
||
try: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(api_url) as response: | ||
response.raise_for_status() | ||
weather_data = await response.json() | ||
|
||
temperature = weather_data["main"]["temp"] | ||
humidity = weather_data["main"]["humidity"] | ||
wind_speed = weather_data["wind"]["speed"] | ||
weather_description = weather_data["weather"][0]["description"] | ||
|
||
temperature_emoji = "🌡" if temperature > 0 else "❄️" | ||
photo_url = images.get( | ||
weather_description, | ||
"https://te.legra.ph/file/a370559984d0da124b97a.jpg", | ||
) | ||
|
||
await message.answer_photo( | ||
photo=photo_url, | ||
caption=send_weather.format( | ||
city, | ||
temperature_emoji, | ||
temperature, | ||
humidity, | ||
wind_speed, | ||
weather_description, | ||
), | ||
) | ||
|
||
except aiohttp.ClientError: | ||
await message.answer("<b>🚫 Error retrieving weather data. Please try again later.</b>") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton | ||
|
||
def get_cancel_keyboard(): | ||
keyboard = ReplyKeyboardMarkup( | ||
keyboard=[ | ||
[ | ||
KeyboardButton(text="🚫 Cancel"), | ||
], | ||
], | ||
one_time_keyboard=True, | ||
resize_keyboard=True, | ||
) | ||
return keyboard |
Oops, something went wrong.