-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.py
executable file
·168 lines (119 loc) · 5.66 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
# -*- coding: utf-8 -*-
import StringIO
import json
import logging
import random
import urllib
import urllib2
# Custom imports
from inputModel import inputModel
import responseController
# for sending images
from PIL import Image
import multipart
# standard app engine imports
from google.appengine.api import urlfetch
from google.appengine.ext import ndb
import webapp2
######## GLOBAL VARIABLES ########
TOKEN = 'BOT_TOKEN_COMES_HERE'
BASE_URL = 'https://api.telegram.org/bot' + TOKEN + '/'
WEATHER_BASE_URL = 'http://api.openweathermap.org/data/2.5/weather?'
WEATHER_API_KEY = 'OPENWEATHERMAP_APIKEY_COMES_HERE'
WEATHER_CITY_NAME = 'q='
WEATHER_CITY_LAT = 'lat='
WEATHER_CITY_LNG = 'lon='
WEATHER_UNIT = 'metric'
WEATHER_DAY_CNT = 1
degree_sign= u'\N{DEGREE SIGN}'
# ================================
######## HELPER CLASSES AND FUNCTIONS ########
class EnableStatus(ndb.Model):
# key name: str(chat_id)
enabled = ndb.BooleanProperty(indexed=False, default=False)
# ================================
def setEnabled(chat_id, yes):
es = EnableStatus.get_or_insert(str(chat_id))
es.enabled = yes
es.put()
def getEnabled(chat_id):
es = EnableStatus.get_by_id(str(chat_id))
if es:
return es.enabled
return False
# ================================
class MeHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getMe'))))
class GetUpdatesHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
class SetWebhookHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
url = self.request.get('url')
if url:
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'setWebhook', urllib.urlencode({'url': url})))))
######## MAIN BOT WORK DONE HERE ########
class WebhookHandler(webapp2.RequestHandler):
def post(self):
urlfetch.set_default_fetch_deadline(60)
body = json.loads(self.request.body)
# Console Log input message from user
logging.info('request body:')
logging.info(json.dumps(body)) # escape " u' " from input
"""
Fetch user input
"""
self.response.write(json.dumps(body)) # QUESTION: What does response() do exactly?
newInput = inputModel(body) # Initialize input model with user input
update_id = newInput.getUpdateID()
message_id = newInput.getMessageID()
text = newInput.getText()
fromID = newInput.getFromID()
fromUserName = newInput.getFromName()
chat_id = newInput.getChatID()
location = newInput.getLocation()
lat = newInput.getLat()
lng = newInput.getLng()
if text or location: # check if text or location is entered
if text: # for text inputs
if text.startswith('/'): # check if command
if text.lower() == '/start':
responseController.sendTextMessage(chat_id, '@Weathercast_Bot started\nPlease enter the city name as \'text\' or send as \'location\'\nComing features:\n Forecast for next day(s)\n Daily notification \n\n->City,Country\n->Location')
setEnabled(chat_id, True)
elif text.lower() == '/stop':
responseController.sendTextMessage(chat_id, 'Bot disabled')
setEnabled(chat_id, False)
elif text.lower() == '/help':
responseController.sendTextMessage(chat_id, 'Write as city,country or just send your location easily')
elif text.lower().startswith('/weather'):
responseController.sendTextMessage(chat_id, 'Please enter city name or send location coordinates directly')
else:
responseController.sendTextMessage(chat_id, 'Enter from available commands')
return
else: # text is not command, it is brief text
if getEnabled(chat_id):
weatherResponse = responseController.textInputRequest(text) # Make request to weather API and get results
responseController.textInputHandler(chat_id, weatherResponse) # Handle response
return # finish process
else:
logging.info('not enabled for chat_id {}'.format(chat_id))
responseController.sendTextMessage(chat_id, 'Please enable bot by writing /start')
return
else: # for location inputs
weatherResponse = responseController.locationInputRequest(lat, lng) # Make request to weather API and get results
responseController.locationInputHandler(chat_id, weatherResponse) # Handle response
return # finish process
else: # no meaningful input, EXIT!
logging.info('no text or location from user')
responseController.sendTextMessage(chat_id, 'Enter your location by text or map')
return
app = webapp2.WSGIApplication([
('/me', MeHandler),
('/updates', GetUpdatesHandler),
('/set_webhook', SetWebhookHandler),
('/webhook', WebhookHandler),
], debug=True)