1
|
|
|
from functools import partial |
2
|
|
|
|
3
|
|
|
import requests |
4
|
|
|
import telegram |
5
|
|
|
|
6
|
|
|
from . import BotCommand |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class SettingsCommand(BotCommand): |
10
|
|
|
command = '/settings' |
11
|
|
|
|
12
|
|
|
def default(self, message): |
13
|
|
|
keyboard = [['Set timezone']] |
14
|
|
|
reply_markup = telegram.ReplyKeyboardMarkup( |
15
|
|
|
keyboard, resize_keyboard=True, |
16
|
|
|
one_time_keyboard=True, selective=True) |
17
|
|
|
msg = self.bot.say(message, 'Settings', reply_markup=reply_markup) |
18
|
|
|
self.queue(msg, partial(self.process_settings)) |
19
|
|
|
|
20
|
|
|
def process_settings(self, message): |
21
|
|
|
if message.text and message.text == 'Set timezone': |
22
|
|
|
msg = self.bot.say(message, "Send me your location and I'll do the rest", |
23
|
|
|
telegram.ForceReply(selective=True)) |
24
|
|
|
self.queue(msg, partial(self.set_timezone)) |
25
|
|
|
|
26
|
|
|
else: |
27
|
|
|
self.bot.say(message, 'Nope') |
28
|
|
|
|
29
|
|
|
def set_timezone(self, message): |
30
|
|
|
if not message.location: |
31
|
|
|
self.bot.say(message, "Nope that didn't work, try again.") |
32
|
|
|
return |
33
|
|
|
|
34
|
|
|
tab, created = self._db.get_or_create_tab(message.chat.id) |
35
|
|
|
|
36
|
|
|
geonames_url = 'http://api.geonames.org/timezoneJSON?lat={lat}&lng={lon}&username=demo' |
37
|
|
|
res = requests.get(geonames_url.format(lat=message.location.latitude, |
38
|
|
|
lon=message.location.longitude)).json() |
39
|
|
|
|
40
|
|
|
if 'timezoneId' not in res: |
41
|
|
|
self.bot.say(message, 'Cannot set timezone') |
42
|
|
|
return |
43
|
|
|
|
44
|
|
|
tab.set_timezone(res['timezoneId']) |
45
|
|
|
self.bot.say(message, 'Timezone set to {}'.format(res['timezoneId'])) |
46
|
|
|
|