Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (62 loc) · 2.76 KB
/
Copy pathmain.py
File metadata and controls
86 lines (62 loc) · 2.76 KB
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
import telebot
import threading
from telebot import types
from config import TOKEN
import importlib
bot = telebot.TeleBot(TOKEN)
remove_keyboard = types.ReplyKeyboardRemove()
import schedule
from utils.daily import daily_messages
from database.set_db import init_database
from state import get_active_user
from components.settings import *
from components.forecast import *
from components.weather import *
from keyboards import method_keyboard, main_keyboard, settings_keyboard
import database.db_service as ds
@bot.message_handler(func=lambda message: message.text == 'Back')
def back_handler(message):
bot.send_message(message.chat.id, 'Main Menu', reply_markup=main_keyboard())
@bot.message_handler(func=lambda message: message.text == 'Current')
def by_city_handler(message):
active_user = get_active_user(message.from_user.id)
bot.reply_to(message, 'Send your location or enter city name', reply_markup=method_keyboard(active_user.locations))
bot.register_next_step_handler(message, handle_weather)
@bot.message_handler(func=lambda message: message.text == 'Forecast')
def forecast_handler(message):
active_user = get_active_user(message.from_user.id)
bot.reply_to(message, 'Send your location or enter city name', reply_markup=method_keyboard(active_user.locations))
bot.register_next_step_handler(message, handle_forecast)
@bot.message_handler(func=lambda message: message.text == 'Settings')
def settings_handler(message):
bot.send_message(message.chat.id, 'Settings', reply_markup=settings_keyboard())
@bot.message_handler(commands=['start'])
def handle_start(message):
active_user = ds.find_user_by_id(message.from_user.id)
if not active_user:
# collect new user info
user_info = message.from_user
telegram_id = user_info.id
language = user_info.language_code
username = user_info.username
active_user = ds.create_user(telegram_id, username, language)
bot.send_message(message.chat.id, f"Hello {active_user.username}, welcome to cikpak-weather-bot")
bot.send_message(message.chat.id, 'By default you will receive weather in °C')
bot.send_message(message.chat.id, 'You can change it in settings')
else:
bot.send_message(message.chat.id, f"Hello, {active_user.username}!")
bot.send_message(message.chat.id, 'Choose a option:', reply_markup=main_keyboard())
def job():
return daily_messages(bot)
def daily_weather_worker(self):
schedule.run_pending()
if __name__ == '__main__':
schedule.every(3).seconds.do(job)
print('Starting database...')
init_database()
print('Succes!')
print('--------------------')
print('Bot Polling')
thread = threading.Thread(target=daily_weather_worker, args=(1,))
thread.start()
bot.polling()