-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
173 lines (133 loc) · 5.55 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Version bot for deploy to heroku
# Import all need libraries for the bot
import os
import pyowm
import asyncio
import logging
import datetime
from aiogram import Bot, types
from aiogram.utils import executor
from aiogram.types import ParseMode
from aiogram.dispatcher import Dispatcher
from aiogram.types.message import ContentType
from aiogram.utils.executor import start_webhook
from aiogram.dispatcher.storage import FSMContext
from aiogram.dispatcher.filters.state import State
from aiogram.dispatcher.filters.state import StatesGroup
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from apscheduler.schedulers.asyncio import AsyncIOScheduler
# Create log string
logging.basicConfig(level=logging.INFO)
# Config for bot
TOKEN = os.environ['TOKEN']
WEBHOOK_HOST = 'https://zark-weather-bot.herokuapp.com'
WEBHOOK_PATH = '/webhook/'
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"
WEBAPP_HOST = '0.0.0.0'
WEBAPP_PORT = os.environ.get('PORT')
# ARR_ID = [-1001116481457, -1001341422770]
# Create main config for bot
loop = asyncio.get_event_loop()
bot = Bot(token=TOKEN, parse_mode='HTML')
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage, loop=loop)
# Array of cities
arr = ['Москва', 'Екатеринбург', 'Омск', 'Новосибирск', 'Шерегеш']
arr_dict = {
# -304358952: ['Москва', 'Екатеринбург', 'Омск'], # Group_for_test
-1001341422770: ['Симферополь', 'Москва', 'Екатеринбург', 'Омск', 'Новосибирск', 'Шерегеш'], # Gesh
-1001116481457: ['Москва', 'Екатеринбург', 'Омск', 'Новосибирск', 'Шерегеш'] # Traktir_u_zaitca
}
# Function for get weather
def get_weather(arr_towns):
answer = ''
for town in arr_towns:
owm = pyowm.OWM('70732ac514bf006244ac74c5f31de5aa', language='ru')
town = town
obs = owm.weather_at_place(town)
weather = obs.get_weather()
temp = round(weather.get_temperature('celsius')['temp'])
wind = round(weather.get_wind()['speed'])
# status = weather.get_status()
status = weather.get_detailed_status()
answer += f'<b>{town.capitalize()}</b>\n<code>t: {temp} °C, w: {wind} м/с, {status}.</code>\n'
return answer
# Create function which process command /start
@dp.message_handler(commands=['start'])
async def process_start_command(message: types.Message):
msg = """Hello! I'm a bot that will help you find out the weather.
If you want to know weather in a city tap to command /city and then enter name of the town.
Else you might try to tap /help command."""
await message.reply(msg)
# Create function which process command /help
@dp.message_handler(commands=['help'])
async def process_help_command(message: types.Message):
msg = 'Available the next commands:\n/start\n/weather\n/city\n/goodbye'
await message.reply(msg)
# Create function which process command /weather
@dp.message_handler(commands=['weather'])
async def process_weather_command(message: types.Message):
if message.chat.id in arr_dict:
msg = get_weather(arr_dict[message.chat.id])
else:
msg = get_weather(arr)
await bot.send_message(message.chat.id, msg, reply_to_message_id=message.message_id)
# Create function which process command /goodbye
@dp.message_handler(commands=['goodbye'])
async def process_goodbye_command(message: types.Message):
message_text = 'Давай покеда!'
await bot.send_message(message.chat.id, message_text)
# State machine, get state city
class Form(StatesGroup):
City = State()
# Create function which process command /city
@dp.message_handler(commands=["city"])
async def town(message: types.Message, state: FSMContext):
# Conversation's entry point
await Form.City.set()
await message.answer('Hi bro, enter name of the city where you want to know weather:')
# Create function which process entered message
@dp.message_handler(state=Form.City)
async def get_city(message: types.Message, state: FSMContext):
logging.info('Start entering city')
# Process name city
city = message.text
await message.answer(f'You are entered city: {city.capitalize()}')
await state.update_data(city=city)
try:
logging.info('Try entering city to pyowm')
msg = get_weather([city])
await bot.send_message(message.chat.id, msg, reply_to_message_id=message.message_id)
await state.finish()
except:
await state.finish()
msg = "What the fuck is this? Such city doesn't exist!!!"
await bot.send_message(message.chat.id, msg, reply_to_message_id=message.message_id)
# Define the function that sends weather to the chat on a schedule
@dp.message_handler()
async def sched(msg=None):
for id, arr in arr_dict.items():
msg = get_weather(arr)
await bot.send_message(chat_id=id, text=msg)
# Create scheduler with interval 1 day
scheduler = AsyncIOScheduler()
scheduler.add_job(sched, 'cron', day_of_week='mon-sun', hour=2, minute=00)
scheduler.start()
# Create the function to startup my bot
async def on_startup(dp):
await bot.set_webhook(WEBHOOK_URL)
# Create the function to shutdown my bot
async def on_shutdown(dp):
await bot.close()
# Main script
if __name__ == '__main__':
start_webhook(dispatcher=dp, webhook_path=WEBHOOK_PATH,
on_startup=on_startup, on_shutdown=on_shutdown,
host=WEBAPP_HOST, port=WEBAPP_PORT)
'''
start - start command
help - help command
weather - get weather
city - get city weather
goodbye - bye
'''