-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
96 lines (69 loc) · 1.96 KB
/
bot.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
import config
import telebot
from cuba_weather_redcuba import CubaWeatherRedCuba
import bot_response as br
bot = telebot.TeleBot(config.token)
api = CubaWeatherRedCuba()
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(
message,
br.welcome_message(message.from_user.first_name)
)
from cuba_weather_owm import CubaWeatherOWM
import os
def extract_arg(text):
location = ''
text = text.split()[1:]
for word in text:
location += word + ' '
return location
@bot.message_handler(commands=['forecast', 'fc'])
def send_forecast(message):
location = extract_arg(message.text)
if location == '':
bot.reply_to(
message,
'Enviame /forecast localidad , ej: /forecast Santiago\nTambien funciona el atajo /fc localidad'
)
return
API_KEY = os.environ['OWM_API']
api = CubaWeatherOWM(API_KEY)
weath = api.get(location)
bot.reply_to(
message,
br.forecast_message(weath)
)
@bot.message_handler(content_types=['text'])
def send_response(message):
weather = api.get(message.text)
bot.reply_to(message, br.weather_message(weather), parse_mode='HTML')
## INLINE
from telebot import types
@bot.inline_handler(lambda query: len(query.query) > 2)
def query_text(inline_query):
weather = api.get(inline_query.query)
try:
r = types.InlineQueryResultArticle(
'1',
weather.cityName,
types.InputTextMessageContent(
br.weather_message(weather),
parse_mode='HTML'
)
)
bot.answer_inline_query(inline_query.id, [r])
except Exception as e:
print(e)
import time
import sys
def main_loop():
bot.polling(True)
while 1:
time.sleep(3)
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print('\nExiting by user request.\n')
sys.exit(0)