-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.py
108 lines (76 loc) · 3.26 KB
/
callbacks.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
# -*- coding: utf-8 -*-
from telegram.ext import ConversationHandler
from logger import *
import weather_api
REQUEST_LOCATION, GET_LOCATION = range(2)
def start(bot, update):
user = update.message.from_user
update.message.reply_text('Weather bot alpha\nPrint /help to get help')
logger.info("User %s %s start conversation." % (user.first_name, user.last_name))
def help(bot, update):
update.message.reply_text(
'/help - show this message'
'\n/location - set your location'
'\n/now - get current weather forecast'
'\n/info - show information about you'
)
def cancel(bot, update):
user = update.message.from_user
update.message.reply_text('No problem😉')
return ConversationHandler.END
def wrong_location(bot, update):
update.message.reply_text('This is not a location🤔')
return ConversationHandler.END
def request_location(bot, update):
update.message.reply_text(
'Send me your location so I can send you the weather forecast'
'\nor /cancel to cancel')
return GET_LOCATION
def get_location(bot, update, chat_data):
chat_id = update.message.chat_id
user = update.message.from_user
user_location = update.message.location
logger.info("Location of %s %s: %f / %f"
% (user.first_name, user.last_name, user_location.latitude, user_location.longitude))
chat_data['loc'] = user_location
update.message.reply_text('got it😁')
return ConversationHandler.END
def info(bot, update, chat_data):
if 'loc' not in chat_data:
update.message.reply_text('I have no information about you😥')
else:
try:
user_location = chat_data['loc']
update.message.reply_text('your location is: {}, {}'.format(user_location.latitude, user_location.longitude))
except (IndexError, ValueError):
chat_id = update.message.chat_id
user = update.message.from_user
logger.info('{} in chat_id {} has problems with /info'.format(user.first_name, chat_id))
update.message.reply_text('I have no information about you😴')
def forecast_decorator(func):
def wrapper(bot, update, chat_data):
if 'loc' not in chat_data:
update.message.reply_text("I don't know where are you\nType /location to set your location")
else:
try:
user_location = chat_data['loc']
update.message.reply_text(func(user_location.latitude, user_location.longitude))
except (IndexError, ValueError):
chat_id = update.message.chat_id
user = update.message.from_user
logger.info('{} in chat_id {} has problems with forecast'.format(user.first_name, chat_id))
update.message.reply_text("Sorry, I can't😣")
return wrapper
@forecast_decorator
def now(lat, lon):
return weather_api.get_cur_forecast(lat, lon)
@forecast_decorator
def today(lat, lon):
return weather_api.get_today_forecast(lat, lon)
@forecast_decorator
def tomorrow(lat, lon):
return weather_api.get_tomorrow_forecast(lat, lon)
def echo(bot, update):
update.message.reply_text(update.message.text)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))